2020-03-12 19:02:19 +03:00
|
|
|
package ants
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
// Option represents the optional function.
|
|
|
|
type Option func(opts *Options)
|
|
|
|
|
|
|
|
func loadOptions(options ...Option) *Options {
|
|
|
|
opts := new(Options)
|
|
|
|
for _, option := range options {
|
|
|
|
option(opts)
|
|
|
|
}
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2021-09-13 06:57:25 +03:00
|
|
|
// Options contains all options which will be applied when instantiating an ants pool.
|
2020-03-12 19:02:19 +03:00
|
|
|
type Options struct {
|
2020-08-12 07:11:51 +03:00
|
|
|
// ExpiryDuration is a period for the scavenger goroutine to clean up those expired workers,
|
|
|
|
// the scavenger scans all workers every `ExpiryDuration` and clean up those workers that haven't been
|
|
|
|
// used for more than `ExpiryDuration`.
|
2020-03-12 19:02:19 +03:00
|
|
|
ExpiryDuration time.Duration
|
|
|
|
|
|
|
|
// PreAlloc indicates whether to make memory pre-allocation when initializing Pool.
|
|
|
|
PreAlloc bool
|
|
|
|
|
|
|
|
// Max number of goroutine blocking on pool.Submit.
|
|
|
|
// 0 (default value) means no such limit.
|
|
|
|
MaxBlockingTasks int
|
|
|
|
|
|
|
|
// When Nonblocking is true, Pool.Submit will never be blocked.
|
|
|
|
// ErrPoolOverload will be returned when Pool.Submit cannot be done at once.
|
|
|
|
// When Nonblocking is true, MaxBlockingTasks is inoperative.
|
|
|
|
Nonblocking bool
|
|
|
|
|
|
|
|
// PanicHandler is used to handle panics from each worker goroutine.
|
|
|
|
// if nil, panics will be thrown out again from worker goroutines.
|
|
|
|
PanicHandler func(interface{})
|
|
|
|
|
2020-04-08 08:16:14 +03:00
|
|
|
// Logger is the customized logger for logging info, if it is not set,
|
|
|
|
// default standard logger from log package is used.
|
2020-03-12 19:02:19 +03:00
|
|
|
Logger Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithOptions accepts the whole options config.
|
|
|
|
func WithOptions(options Options) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
*opts = options
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithExpiryDuration sets up the interval time of cleaning up goroutines.
|
|
|
|
func WithExpiryDuration(expiryDuration time.Duration) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.ExpiryDuration = expiryDuration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPreAlloc indicates whether it should malloc for workers.
|
|
|
|
func WithPreAlloc(preAlloc bool) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.PreAlloc = preAlloc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMaxBlockingTasks sets up the maximum number of goroutines that are blocked when it reaches the capacity of pool.
|
|
|
|
func WithMaxBlockingTasks(maxBlockingTasks int) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.MaxBlockingTasks = maxBlockingTasks
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithNonblocking indicates that pool will return nil when there is no available workers.
|
|
|
|
func WithNonblocking(nonblocking bool) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.Nonblocking = nonblocking
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPanicHandler sets up panic handler.
|
|
|
|
func WithPanicHandler(panicHandler func(interface{})) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.PanicHandler = panicHandler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLogger sets up a customized logger.
|
|
|
|
func WithLogger(logger Logger) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.Logger = logger
|
|
|
|
}
|
|
|
|
}
|