pool

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package pool contains a thread pool implementation.

Index

Constants

View Source
const (
	StatusRunning  = "Running"
	StatusStopping = "Stopping"
	StatusStopped  = "Stopped"
)

Different states of a thread pool.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultTaskQueue

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

DefaultTaskQueue implements a simple (FIFO) task queue for a thread pool.

func (*DefaultTaskQueue) Clear

func (tq *DefaultTaskQueue) Clear()

Clear the queue of all pending tasks

func (*DefaultTaskQueue) Pop

func (tq *DefaultTaskQueue) Pop() Task

Pop returns the next task from the queue.

func (*DefaultTaskQueue) Push

func (tq *DefaultTaskQueue) Push(t Task)

Push adds another task to the queue.

func (*DefaultTaskQueue) Size

func (tq *DefaultTaskQueue) Size() int

Size returns the size of the queue.

type Task

type Task interface {

	/*
		Run the task. The function gets the unique thread ID of the worker
		which executes the task.
	*/
	Run(tid uint64) error

	/*
		HandleError handles an error which occurred during the run method.
	*/
	HandleError(e error)
}

Task is a task which should be run in a thread.

type TaskQueue

type TaskQueue interface {

	/*
		Clear the queue of all pending tasks
	*/
	Clear()

	/*
		Pop returns the next task from the queue.
	*/
	Pop() Task
	/*
		Push adds another task to the queue.
	*/
	Push(t Task)

	/*
		Size returns the size of the queue.
	*/
	Size() int
}

TaskQueue is a queue of tasks for a thread pool.

type ThreadPool

type ThreadPool struct {
	RegulationLock *sync.Mutex // Lock for regulation variables

	TooManyThreshold int    // Threshold for too many tasks
	TooManyCallback  func() // Callback for too many tasks

	TooFewThreshold int    // Threshold for too few tasks
	TooFewCallback  func() // Callback for too few tasks
	// contains filtered or unexported fields
}

ThreadPool creates a pool of threads which process tasks according to a given task queue. The threads are kept in an idle state if no more tasks are available. They resume immediately once a new task is added.

func NewThreadPool

func NewThreadPool() *ThreadPool

NewThreadPool creates a new thread pool.

func NewThreadPoolWithQueue

func NewThreadPoolWithQueue(q TaskQueue) *ThreadPool

NewThreadPoolWithQueue creates a new thread pool with a specific task queue.

func (*ThreadPool) AddTask

func (tp *ThreadPool) AddTask(t Task)

AddTask adds a task to the thread pool.

func (*ThreadPool) JoinAll

func (tp *ThreadPool) JoinAll()

JoinAll processes all remaining tasks and kills off all workers afterwards.

func (*ThreadPool) NewThreadID added in v1.0.0

func (tp *ThreadPool) NewThreadID() uint64

NewThreadID creates a new thread ID unique to this pool.

func (*ThreadPool) SetWorkerCount

func (tp *ThreadPool) SetWorkerCount(count int, wait bool)

SetWorkerCount sets the worker count of this pool. If the wait flag is true then this call will return after the pool has reached the requested worker count.

func (*ThreadPool) Status

func (tp *ThreadPool) Status() string

Status returns the current status of the thread pool.

func (*ThreadPool) WaitAll

func (tp *ThreadPool) WaitAll()

WaitAll waits for all workers to become idle.

func (*ThreadPool) WorkerCount

func (tp *ThreadPool) WorkerCount() int

WorkerCount returns the current count of workers.

type ThreadPoolWorker

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

ThreadPoolWorker models a worker in the thread pool.

Jump to

Keyboard shortcuts

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