Documentation ¶
Index ¶
- Constants
- Variables
- func Cap() int
- func Free() int
- func Reboot()
- func Release()
- func ReleaseTimeout(timeout time.Duration) error
- func Running() int
- func SetDefaultAntsPool(size int, options ...Option)
- func Submit(task func()) error
- type LoadBalancingStrategy
- type Logger
- type MultiPool
- func (mp *MultiPool) Cap() (n int)
- func (mp *MultiPool) Free() (n int)
- func (mp *MultiPool) FreeByIndex(idx int) (int, error)
- func (mp *MultiPool) IsClosed() bool
- func (mp *MultiPool) Reboot()
- func (mp *MultiPool) ReleaseTimeout(timeout time.Duration) error
- func (mp *MultiPool) Running() (n int)
- func (mp *MultiPool) RunningByIndex(idx int) (int, error)
- func (mp *MultiPool) Submit(task func()) (err error)
- func (mp *MultiPool) Tune(size int)
- func (mp *MultiPool) TuneMaxBlockingTasks(size int)
- func (mp *MultiPool) Waiting() (n int)
- func (mp *MultiPool) WaitingByIndex(idx int) (int, error)
- type MultiPoolWithFunc
- func (mp *MultiPoolWithFunc) Cap() (n int)
- func (mp *MultiPoolWithFunc) Free() (n int)
- func (mp *MultiPoolWithFunc) FreeByIndex(idx int) (int, error)
- func (mp *MultiPoolWithFunc) Invoke(args interface{}) (err error)
- func (mp *MultiPoolWithFunc) IsClosed() bool
- func (mp *MultiPoolWithFunc) Reboot()
- func (mp *MultiPoolWithFunc) ReleaseTimeout(timeout time.Duration) error
- func (mp *MultiPoolWithFunc) Running() (n int)
- func (mp *MultiPoolWithFunc) RunningByIndex(idx int) (int, error)
- func (mp *MultiPoolWithFunc) Tune(size int)
- func (mp *MultiPoolWithFunc) TuneMaxBlockingTasks(size int)
- func (mp *MultiPoolWithFunc) Waiting() (n int)
- func (mp *MultiPoolWithFunc) WaitingByIndex(idx int) (int, error)
- type Option
- func WithDisablePurge(disable bool) Option
- func WithExpiryDuration(expiryDuration time.Duration) Option
- func WithLogger(logger Logger) Option
- func WithMaxBlockingTasks(maxBlockingTasks int) Option
- func WithNonblocking(nonblocking bool) Option
- func WithOptions(options Options) Option
- func WithPanicHandler(panicHandler func(interface{})) Option
- func WithPreAlloc(preAlloc bool) Option
- type Options
- type Pool
- func (p *Pool) Cap() int
- func (p *Pool) Free() int
- func (p *Pool) IsClosed() bool
- func (p *Pool) MaxBlockingTasks() int
- func (p *Pool) Reboot()
- func (p *Pool) Release()
- func (p *Pool) ReleaseTimeout(timeout time.Duration) error
- func (p *Pool) Running() int
- func (p *Pool) Submit(task func()) error
- func (p *Pool) Tune(size int)
- func (p *Pool) TuneMaxBlockingTasks(size int)
- func (p *Pool) Waiting() int
- type PoolWithFunc
- func (p *PoolWithFunc) Cap() int
- func (p *PoolWithFunc) Free() int
- func (p *PoolWithFunc) Invoke(args interface{}) error
- func (p *PoolWithFunc) IsClosed() bool
- func (p *PoolWithFunc) MaxBlockingTasks() int
- func (p *PoolWithFunc) Reboot()
- func (p *PoolWithFunc) Release()
- func (p *PoolWithFunc) ReleaseTimeout(timeout time.Duration) error
- func (p *PoolWithFunc) Running() int
- func (p *PoolWithFunc) Tune(size int)
- func (p *PoolWithFunc) TuneMaxBlockingTasks(size int)
- func (p *PoolWithFunc) Waiting() int
Constants ¶
const ( // DefaultAntsPoolSize is the default capacity for a default goroutine pool. DefaultAntsPoolSize = math.MaxInt32 // DefaultCleanIntervalTime is the interval time to clean up goroutines. DefaultCleanIntervalTime = time.Second )
const ( // OPENED represents that the pool is opened. OPENED = iota // CLOSED represents that the pool is closed. CLOSED )
Variables ¶
var ( // ErrLackPoolFunc will be returned when invokers don't provide function for pool. ErrLackPoolFunc = errors.New("must provide function for pool") // ErrInvalidPoolExpiry will be returned when setting a negative number as the periodic duration to purge goroutines. ErrInvalidPoolExpiry = errors.New("invalid expiry for pool") // ErrPoolClosed will be returned when submitting task to a closed pool. ErrPoolClosed = errors.New("this pool has been closed") // ErrPoolOverload will be returned when the pool is full and no workers available. ErrPoolOverload = errors.New("too many goroutines blocked on submit or Nonblocking is set") // ErrInvalidPreAllocSize will be returned when trying to set up a negative capacity under PreAlloc mode. ErrInvalidPreAllocSize = errors.New("can not set up a negative capacity under PreAlloc mode") // ErrTimeout will be returned after the operations timed out. ErrTimeout = errors.New("operation timed out") // ErrInvalidPoolIndex will be returned when trying to retrieve a pool with an invalid index. ErrInvalidPoolIndex = errors.New("invalid pool index") // ErrInvalidLoadBalancingStrategy will be returned when trying to create a MultiPool with an invalid load-balancing strategy. ErrInvalidLoadBalancingStrategy = errors.New("invalid load-balancing strategy") )
Functions ¶
func ReleaseTimeout ¶ added in v1.8.0
ReleaseTimeout is like Release but with a timeout, it waits all workers to exit before timing out.
func SetDefaultAntsPool ¶ added in v1.6.1
SetDefaultAntsPool initialize to the default pool.
Types ¶
type LoadBalancingStrategy ¶ added in v1.9.0
type LoadBalancingStrategy int
LoadBalancingStrategy represents the type of load-balancing algorithm.
const ( // RoundRobin distributes task to a list of pools in rotation. RoundRobin LoadBalancingStrategy = 1 << (iota + 1) // LeastTasks always selects the pool with the least number of pending tasks. LeastTasks )
type Logger ¶ added in v1.5.0
type Logger interface { // Printf must have the same semantics as log.Printf. Printf(format string, args ...interface{}) }
Logger is used for logging formatted messages.
type MultiPool ¶ added in v1.9.0
type MultiPool struct {
// contains filtered or unexported fields
}
MultiPool consists of multiple pools, from which you will benefit the performance improvement on basis of the fine-grained locking that reduces the lock contention. MultiPool is a good fit for the scenario where you have a large number of tasks to submit, and you don't want the single pool to be the bottleneck.
func NewMultiPool ¶ added in v1.9.0
func NewMultiPool(size, sizePerPool int, lbs LoadBalancingStrategy, options ...Option) (*MultiPool, error)
NewMultiPool instantiates a MultiPool with a size of the pool list and a size per pool, and the load-balancing strategy.
func (*MultiPool) Free ¶ added in v1.9.0
Free returns the number of available workers across all pools.
func (*MultiPool) FreeByIndex ¶ added in v1.9.0
FreeByIndex returns the number of available workers in the specific pool.
func (*MultiPool) Reboot ¶ added in v1.9.0
func (mp *MultiPool) Reboot()
Reboot reboots a released multi-pool.
func (*MultiPool) ReleaseTimeout ¶ added in v1.9.0
ReleaseTimeout closes the multi-pool with a timeout, it waits all pools to be closed before timing out.
func (*MultiPool) Running ¶ added in v1.9.0
Running returns the number of the currently running workers across all pools.
func (*MultiPool) RunningByIndex ¶ added in v1.9.0
RunningByIndex returns the number of the currently running workers in the specific pool.
func (*MultiPool) Submit ¶ added in v1.9.0
Submit submits a task to a pool selected by the load-balancing strategy.
func (*MultiPool) Tune ¶ added in v1.9.0
Tune resizes each pool in multi-pool.
Note that this method doesn't resize the overall capacity of multi-pool.
func (*MultiPool) TuneMaxBlockingTasks ¶ added in v1.9.0
TuneMaxBlockingTasks changes the maximum number of goroutines that are blocked each pool in multi-pool.
type MultiPoolWithFunc ¶ added in v1.9.0
type MultiPoolWithFunc struct {
// contains filtered or unexported fields
}
MultiPoolWithFunc consists of multiple pools, from which you will benefit the performance improvement on basis of the fine-grained locking that reduces the lock contention. MultiPoolWithFunc is a good fit for the scenario where you have a large number of tasks to submit, and you don't want the single pool to be the bottleneck.
func NewMultiPoolWithFunc ¶ added in v1.9.0
func NewMultiPoolWithFunc(size, sizePerPool int, fn func(interface{}), lbs LoadBalancingStrategy, options ...Option) (*MultiPoolWithFunc, error)
NewMultiPoolWithFunc instantiates a MultiPoolWithFunc with a size of the pool list and a size per pool, and the load-balancing strategy.
func (*MultiPoolWithFunc) Cap ¶ added in v1.9.0
func (mp *MultiPoolWithFunc) Cap() (n int)
Cap returns the capacity of this multi-pool.
func (*MultiPoolWithFunc) Free ¶ added in v1.9.0
func (mp *MultiPoolWithFunc) Free() (n int)
Free returns the number of available workers across all pools.
func (*MultiPoolWithFunc) FreeByIndex ¶ added in v1.9.0
func (mp *MultiPoolWithFunc) FreeByIndex(idx int) (int, error)
FreeByIndex returns the number of available workers in the specific pool.
func (*MultiPoolWithFunc) Invoke ¶ added in v1.9.0
func (mp *MultiPoolWithFunc) Invoke(args interface{}) (err error)
Invoke submits a task to a pool selected by the load-balancing strategy.
func (*MultiPoolWithFunc) IsClosed ¶ added in v1.9.0
func (mp *MultiPoolWithFunc) IsClosed() bool
IsClosed indicates whether the multi-pool is closed.
func (*MultiPoolWithFunc) Reboot ¶ added in v1.9.0
func (mp *MultiPoolWithFunc) Reboot()
Reboot reboots a released multi-pool.
func (*MultiPoolWithFunc) ReleaseTimeout ¶ added in v1.9.0
func (mp *MultiPoolWithFunc) ReleaseTimeout(timeout time.Duration) error
ReleaseTimeout closes the multi-pool with a timeout, it waits all pools to be closed before timing out.
func (*MultiPoolWithFunc) Running ¶ added in v1.9.0
func (mp *MultiPoolWithFunc) Running() (n int)
Running returns the number of the currently running workers across all pools.
func (*MultiPoolWithFunc) RunningByIndex ¶ added in v1.9.0
func (mp *MultiPoolWithFunc) RunningByIndex(idx int) (int, error)
RunningByIndex returns the number of the currently running workers in the specific pool.
func (*MultiPoolWithFunc) Tune ¶ added in v1.9.0
func (mp *MultiPoolWithFunc) Tune(size int)
Tune resizes each pool in multi-pool.
Note that this method doesn't resize the overall capacity of multi-pool.
func (*MultiPoolWithFunc) TuneMaxBlockingTasks ¶ added in v1.9.0
func (mp *MultiPoolWithFunc) TuneMaxBlockingTasks(size int)
TuneMaxBlockingTasks changes the maximum number of goroutines that are blocked each pool in multi-pool.
func (*MultiPoolWithFunc) Waiting ¶ added in v1.9.0
func (mp *MultiPoolWithFunc) Waiting() (n int)
Waiting returns the number of the currently waiting tasks across all pools.
func (*MultiPoolWithFunc) WaitingByIndex ¶ added in v1.9.0
func (mp *MultiPoolWithFunc) WaitingByIndex(idx int) (int, error)
WaitingByIndex returns the number of the currently waiting tasks in the specific pool.
type Option ¶ added in v1.5.0
type Option func(opts *Options)
Option represents the optional function.
func WithDisablePurge ¶ added in v1.6.0
WithDisablePurge indicates whether we turn off automatically purge.
func WithExpiryDuration ¶ added in v1.5.0
WithExpiryDuration sets up the interval time of cleaning up goroutines.
func WithLogger ¶ added in v1.5.0
WithLogger sets up a customized logger.
func WithMaxBlockingTasks ¶ added in v1.5.0
WithMaxBlockingTasks sets up the maximum number of goroutines that are blocked when it reaches the capacity of pool.
func WithNonblocking ¶ added in v1.5.0
WithNonblocking indicates that pool will return nil when there is no available workers.
func WithOptions ¶ added in v1.5.0
WithOptions accepts the whole options config.
func WithPanicHandler ¶ added in v1.5.0
func WithPanicHandler(panicHandler func(interface{})) Option
WithPanicHandler sets up panic handler.
func WithPreAlloc ¶ added in v1.5.0
WithPreAlloc indicates whether it should malloc for workers.
type Options ¶ added in v1.5.0
type Options struct { // 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`. 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 int32 // 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 // When DisablePurge is true, workers are not purged and are resident. DisablePurge bool }
Options contains all options which will be applied when instantiating an ants pool.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool accepts the tasks and process them concurrently, it limits the total of goroutines to a given number by recycling goroutines.
func (*Pool) Free ¶
Free returns the number of available workers, -1 indicates this pool is unlimited.
func (*Pool) MaxBlockingTasks ¶ added in v1.5.0
MaxBlockingTasks returns the maximum number of goroutines that are blocked when it reaches the capacity of pool.
func (*Pool) Release ¶
func (p *Pool) Release()
Release closes this pool and releases the worker queue.
func (*Pool) ReleaseTimeout ¶ added in v1.5.0
ReleaseTimeout is like Release but with a timeout, it waits all workers to exit before timing out.
func (*Pool) Submit ¶
Submit submits a task to this pool.
Note that you are allowed to call Pool.Submit() from the current Pool.Submit(), but what calls for special attention is that you will get blocked with the last Pool.Submit() call once the current Pool runs out of its capacity, and to avoid this, you should instantiate a Pool with ants.WithNonblocking(true).
func (*Pool) Tune ¶
Tune changes the capacity of this pool, note that it is noneffective to the infinite or pre-allocation pool.
func (*Pool) TuneMaxBlockingTasks ¶ added in v1.5.0
TuneMaxBlockingTasks changes the maximum number of goroutines that are blocked when it reaches the capacity of pool.
type PoolWithFunc ¶
type PoolWithFunc struct {
// contains filtered or unexported fields
}
PoolWithFunc accepts the tasks and process them concurrently, it limits the total of goroutines to a given number by recycling goroutines.
func NewPoolWithFunc ¶
func NewPoolWithFunc(size int, pf func(interface{}), options ...Option) (*PoolWithFunc, error)
NewPoolWithFunc instantiates a PoolWithFunc with customized options.
func (*PoolWithFunc) Free ¶
func (p *PoolWithFunc) Free() int
Free returns the number of available workers, -1 indicates this pool is unlimited.
func (*PoolWithFunc) Invoke ¶
func (p *PoolWithFunc) Invoke(args interface{}) error
Invoke submits a task to pool.
Note that you are allowed to call Pool.Invoke() from the current Pool.Invoke(), but what calls for special attention is that you will get blocked with the last Pool.Invoke() call once the current Pool runs out of its capacity, and to avoid this, you should instantiate a PoolWithFunc with ants.WithNonblocking(true).
func (*PoolWithFunc) IsClosed ¶ added in v1.5.0
func (p *PoolWithFunc) IsClosed() bool
IsClosed indicates whether the pool is closed.
func (*PoolWithFunc) MaxBlockingTasks ¶ added in v1.5.0
func (p *PoolWithFunc) MaxBlockingTasks() int
MaxBlockingTasks returns the maximum number of goroutines that are blocked when it reaches the capacity of pool.
func (*PoolWithFunc) Reboot ¶ added in v1.5.0
func (p *PoolWithFunc) Reboot()
Reboot reboots a closed pool.
func (*PoolWithFunc) Release ¶
func (p *PoolWithFunc) Release()
Release closes this pool and releases the worker queue.
func (*PoolWithFunc) ReleaseTimeout ¶ added in v1.5.0
func (p *PoolWithFunc) ReleaseTimeout(timeout time.Duration) error
ReleaseTimeout is like Release but with a timeout, it waits all workers to exit before timing out.
func (*PoolWithFunc) Running ¶
func (p *PoolWithFunc) Running() int
Running returns the number of workers currently running.
func (*PoolWithFunc) Tune ¶
func (p *PoolWithFunc) Tune(size int)
Tune changes the capacity of this pool, note that it is noneffective to the infinite or pre-allocation pool.
func (*PoolWithFunc) TuneMaxBlockingTasks ¶ added in v1.5.0
func (p *PoolWithFunc) TuneMaxBlockingTasks(size int)
TuneMaxBlockingTasks changes the maximum number of goroutines that are blocked when it reaches the capacity of pool.
func (*PoolWithFunc) Waiting ¶ added in v1.5.0
func (p *PoolWithFunc) Waiting() int
Waiting returns the number of tasks waiting to be executed.