Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Job ¶
type Job struct { Ctx context.Context Task interface{} Args []interface{} RecoverFn func(r interface{}) }
Job is the struct that represents the smallest unit of worker tasks
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool is a struct that manages a collection of workers, each with their own goroutine. The Pool can initialize, expand, compress and close the workers, as well as asynchronously process submitted tasks.
func New ¶
New creates a new Pool of workers that starts with a number of workers. You must provide a constructor function that creates new Worker types and when you change the capacity of the pool the constructor will be called to create a new Worker. capacity - how many workers will be created for this pool and size of the pool. ctor - constructor function that creates new Worker types
func NewCallback ¶
NewCallback creates a new Pool of workers where workers cast the Job payload into a func() and runs it, or returns ErrNotFunc if the cast failed.
func (*Pool) Release ¶
func (p *Pool) Release()
Release will terminate all workers and close the channel of this Pool.
func (*Pool) SetCapacity ¶
SetCapacity changes the capacity of the pool and the total number of workers in the Pool. This can be called by any goroutine at any time unless the Pool has been stopped, in which case a panic will occur.
func (*Pool) Submit ¶
func (p *Pool) Submit(pl payLoad, opts ...TaskOption)
Submit will submit a task to the task queue of the goroutine pool.
func (*Pool) SubmitWithContext ¶
func (p *Pool) SubmitWithContext(ctx context.Context, pl payLoad, opts ...TaskOption)
SubmitWithContext will submit a task to the task queue of the coroutine pool, accompanied by a context. Before the task is executed, if the context is canceled, the task will not be executed.
type PoolOption ¶
type PoolOption interface {
// contains filtered or unexported methods
}
PoolOption configures how we set up the connection.
type Stats ¶
type Stats struct { Cap int // goroutine pool capacity IdleWorker int // Number of work goroutines in idle state WaitingTask int // Number of tasks waiting to be processed }
Stats contains running pool Infos.
type TaskOption ¶
type TaskOption interface {
// contains filtered or unexported methods
}
TaskOption configures how we set up the connection.
func WithArgs ¶
func WithArgs(args ...interface{}) TaskOption
WithArgs returns task option for callback func args
func WithRecoverFn ¶
func WithRecoverFn(f func(r interface{})) TaskOption
WithRecoverFn returns task option for recover to catch panic
type Worker ¶
type Worker interface { // Do will process a job. Do(job *Job) // return job channel of worker JobChan() chan *Job // return stop channel of worker StopChan() chan struct{} }
Worker is an interface representing a wasps working agent. It will be used to process a job of own job channel, and clean up its resources when being removed from the pool.