mirror of https://github.com/panjf2000/ants.git
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
|
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
|
||
|
}
|
||
|
|
||
|
// Options contains all options which will be applied when instantiating a ants pool.
|
||
|
type Options struct {
|
||
|
// ExpiryDuration sets the expired time of every worker.
|
||
|
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{})
|
||
|
|
||
|
// Logger is the customized logger for logging info, if it is not set, default standard logger from log package is used.
|
||
|
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
|
||
|
}
|
||
|
}
|