threadpool

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrInvalidWorkerCount = utils.Error("Invalid workerCount value")
	ErrInvalidQueueSize   = utils.Error("Invalid queueSize value")
	ErrPoolNotStarted     = utils.Error("ThreadPool not started")
	ErrPoolAlreadyStarted = utils.Error("ThreadPool already started")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job interface {
	Run(ctx context.Context)
}

type Pool

type Pool interface {
	Stop() error
	Dispatch(j Job)
	Start() error
}

type ThreadPool

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

func NewThreadPool

func NewThreadPool(workerCount int, queueSize int) (*ThreadPool, error)

NewThreadPool is a constructor function that creates a new ThreadPool instance. It takes in two parameters: - workerCount: the number of workers to be created in the ThreadPool. Must be greater than 0. If it's less than 1, it returns ErrInvalidWorkerCount. - queueSize: the size of the job queue in the ThreadPool. Must be greater than 0. If it's less than 1, it returns ErrInvalidQueueSize. It returns a pointer to the created ThreadPool and an error.

Example usage: pool, err := NewThreadPool(5, 10)

if err != nil {
  // handle error
}

pool.Dispatch(job) ...

func (*ThreadPool) Dispatch

func (t *ThreadPool) Dispatch(j Job)

Dispatch adds a new job to the jobQueue of the ThreadPool. The job will be executed by one of the worker goroutines in the ThreadPool. The job must implement the Job interface with a Run method that takes a context.Context parameter.

Example usage:

job := MyJob{}
threadPool.Dispatch(job)

Note: This function is blocking if jobQueue is full

func (*ThreadPool) GetQueueCapacity

func (t *ThreadPool) GetQueueCapacity() int

GetQueueCapacity returns the capacity of the job queue in the ThreadPool.

func (*ThreadPool) GetQueueLen

func (t *ThreadPool) GetQueueLen() int

GetQueueLen returns the number of jobs currently in the jobQueue of the ThreadPool. It calculates the size of the jobQueue by using the len() function on the jobQueue slice. The returned value represents the number of pending jobs waiting to be processed by the workers.

func (*ThreadPool) GetRequestCount

func (t *ThreadPool) GetRequestCount() uint64

GetRequestCount returns the total number of requests handled by the ThreadPool. If the ThreadPool has not been started, it returns 0. It internally calls the RequestCount method of the workers in the ThreadGroup to calculate the total number of requests.

func (*ThreadPool) GetWorkerCount

func (t *ThreadPool) GetWorkerCount() int

GetWorkerCount returns the current number of workers in the ThreadPool. It retrieves the value of workerCount from the ThreadPool and returns it. This count represents the number of workers that are actively processing jobs. Note that this count does not include idle or terminated workers.

func (*ThreadPool) Start

func (t *ThreadPool) Start(ctx context.Context) error

Start starts the execution of the ThreadPool. It returns an error if the ThreadPool has already been started. If the given context is nil, it will default to the background context. It creates a new WorkerGroup with the specified workerCount

func (*ThreadPool) Stop

func (t *ThreadPool) Stop() error

Stop stops the execution of the ThreadPool. It returns an error if the ThreadPool has not been started yet. It cancels the context and waits for all workers to finish their current jobs. After that, it cleans the worker list and sets the started flag to false. Note: this function is blocking

type Worker

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

func NewWorker

func NewWorker(jobQueue chan Job, ctx context.Context) *Worker

func (*Worker) RequestCounter

func (w *Worker) RequestCounter() uint64

func (*Worker) Start

func (w *Worker) Start(wg *sync.WaitGroup)

type WorkerGroup

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

func NewWorkerGroup

func NewWorkerGroup(workerCount int, jobQueue chan Job, parentCtx context.Context) (*WorkerGroup, error)

func (*WorkerGroup) RequestCount

func (w *WorkerGroup) RequestCount() uint64

func (*WorkerGroup) Stop

func (w *WorkerGroup) Stop()

Jump to

Keyboard shortcuts

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