workerpool

package
v1.2.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package workerpool implements the thread-pool paradigm in Go. The benefits of it in Go however, can be quite different from any other language able to schedule itself on system threads.

By using a workpool model, the main focus and intention is to limit the number of go routines that can do busy-work and get scheduled concurrenly at any point in time.

Too many go routines being scheduled at the same time will cause other go routines (maybe more critical ones) to be scheduled less often, thus incurring in resource starvation on those and potentially triggering other issues.

By being able to queue up work, we should be able to run a more deterministic runtime (despite Go's nature, this we will not be able to help), less dependant on the scheduler and more accurate in terms of time, as now the number of routines doing busy work can remain constant as opposed have O(N) routines attempting to run at the same time.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job interface {
	// JobName returns the name of the job.
	JobName() string

	// Run executes the job.
	Run()

	// GetDoneCh returns the channel, which when closed, indicates that the job was finished.
	GetDoneCh() <-chan struct{}
}

Job is a runnable interface to queue jobs on a WorkerPool

type WorkerPool

type WorkerPool struct {
	// contains filtered or unexported fields
}

WorkerPool object representation

func NewWorkerPool

func NewWorkerPool(nWorkers int) *WorkerPool

NewWorkerPool creates a new work group. If nWorkers is 0, will poll goMaxProcs to get the number of routines to spawn. Reminder: routines are never pinned to system threads, it's up to the go scheduler to decide when and where these will be scheduled.

func (*WorkerPool) AddJob

func (wp *WorkerPool) AddJob(job Job) <-chan struct{}

AddJob posts the job on a worker queue Uses Hash underneath to choose worker to post the job to

func (*WorkerPool) GetWorkerNumber

func (wp *WorkerPool) GetWorkerNumber() int

GetWorkerNumber get number of queues/workers

func (*WorkerPool) Stop

func (wp *WorkerPool) Stop()

Stop stops the workerpool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL