pool

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
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
)
View Source
const (
	// OPENED represents that the pool is opened.
	OPENED = iota

	// CLOSED represents that the pool is closed.
	CLOSED
)

Variables

View Source
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 Cap

func Cap() int

Cap returns the capacity of this default pool.

func Free

func Free() int

Free returns the available goroutines to work.

func NewSpinLock

func NewSpinLock() sync.Locker

NewSpinLock instantiates a spin-lock.

func Reboot

func Reboot()

Reboot reboots the default pool.

func Release

func Release()

Release Closes the default pool.

func ReleaseTimeout

func ReleaseTimeout(timeout time.Duration) error

ReleaseTimeout is like Release but with a timeout, it waits all workers to exit before timing out.

func Running

func Running() int

Running returns the number of the currently running goroutines.

func Submit

func Submit(task func()) error

Submit submits a task to pool.

Types

type LoadBalancingStrategy

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

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

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

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) Cap

func (mp *MultiPool) Cap() (n int)

Cap returns the capacity of this multi-pool.

func (*MultiPool) Free

func (mp *MultiPool) Free() (n int)

Free returns the number of available workers across all pools.

func (*MultiPool) FreeByIndex

func (mp *MultiPool) FreeByIndex(idx int) (int, error)

FreeByIndex returns the number of available workers in the specific pool.

func (*MultiPool) IsClosed

func (mp *MultiPool) IsClosed() bool

IsClosed indicates whether the multi-pool is closed.

func (*MultiPool) Reboot

func (mp *MultiPool) Reboot()

Reboot reboots a released multi-pool.

func (*MultiPool) ReleaseTimeout

func (mp *MultiPool) ReleaseTimeout(timeout time.Duration) error

ReleaseTimeout closes the multi-pool with a timeout, it waits all pools to be closed before timing out.

func (*MultiPool) Running

func (mp *MultiPool) Running() (n int)

Running returns the number of the currently running workers across all pools.

func (*MultiPool) RunningByIndex

func (mp *MultiPool) RunningByIndex(idx int) (int, error)

RunningByIndex returns the number of the currently running workers in the specific pool.

func (*MultiPool) Submit

func (mp *MultiPool) Submit(task func()) (err error)

Submit submits a task to a pool selected by the load-balancing strategy.

func (*MultiPool) Tune

func (mp *MultiPool) Tune(size int)

Tune resizes each pool in multi-pool.

Note that this method doesn't resize the overall capacity of multi-pool.

func (*MultiPool) Waiting

func (mp *MultiPool) Waiting() (n int)

Waiting returns the number of the currently waiting tasks across all pools.

func (*MultiPool) WaitingByIndex

func (mp *MultiPool) WaitingByIndex(idx int) (int, error)

WaitingByIndex returns the number of the currently waiting tasks in the specific pool.

type MultiPoolWithFunc

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

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

func (mp *MultiPoolWithFunc) Cap() (n int)

Cap returns the capacity of this multi-pool.

func (*MultiPoolWithFunc) Free

func (mp *MultiPoolWithFunc) Free() (n int)

Free returns the number of available workers across all pools.

func (*MultiPoolWithFunc) FreeByIndex

func (mp *MultiPoolWithFunc) FreeByIndex(idx int) (int, error)

FreeByIndex returns the number of available workers in the specific pool.

func (*MultiPoolWithFunc) Invoke

func (mp *MultiPoolWithFunc) Invoke(args interface{}) (err error)

Invoke submits a task to a pool selected by the load-balancing strategy.

func (*MultiPoolWithFunc) IsClosed

func (mp *MultiPoolWithFunc) IsClosed() bool

IsClosed indicates whether the multi-pool is closed.

func (*MultiPoolWithFunc) Reboot

func (mp *MultiPoolWithFunc) Reboot()

Reboot reboots a released multi-pool.

func (*MultiPoolWithFunc) ReleaseTimeout

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

func (mp *MultiPoolWithFunc) Running() (n int)

Running returns the number of the currently running workers across all pools.

func (*MultiPoolWithFunc) RunningByIndex

func (mp *MultiPoolWithFunc) RunningByIndex(idx int) (int, error)

RunningByIndex returns the number of the currently running workers in the specific pool.

func (*MultiPoolWithFunc) Tune

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) Waiting

func (mp *MultiPoolWithFunc) Waiting() (n int)

Waiting returns the number of the currently waiting tasks across all pools.

func (*MultiPoolWithFunc) WaitingByIndex

func (mp *MultiPoolWithFunc) WaitingByIndex(idx int) (int, error)

WaitingByIndex returns the number of the currently waiting tasks in the specific pool.

type Option

type Option func(opts *Options)

Option represents the optional function.

func WithDisablePurge

func WithDisablePurge(disable bool) Option

WithDisablePurge indicates whether we turn off automatically purge.

func WithExpiryDuration

func WithExpiryDuration(expiryDuration time.Duration) Option

WithExpiryDuration sets up the interval time of cleaning up goroutines.

func WithLogger

func WithLogger(logger Logger) Option

WithLogger sets up a customized logger.

func WithMaxBlockingTasks

func WithMaxBlockingTasks(maxBlockingTasks int) Option

WithMaxBlockingTasks sets up the maximum number of goroutines that are blocked when it reaches the capacity of pool.

func WithNonblocking

func WithNonblocking(nonblocking bool) Option

WithNonblocking indicates that pool will return nil when there is no available workers.

func WithOptions

func WithOptions(options Options) Option

WithOptions accepts the whole options config.

func WithPanicHandler

func WithPanicHandler(panicHandler func(interface{})) Option

WithPanicHandler sets up panic handler.

func WithPreAlloc

func WithPreAlloc(preAlloc bool) Option

WithPreAlloc indicates whether it should malloc for workers.

type Options

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 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

	// 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 NewPool

func NewPool(size int, options ...Option) (*Pool, error)

NewPool instantiates a Pool with customized options.

func (*Pool) Cap

func (p *Pool) Cap() int

Cap returns the capacity of this pool.

func (*Pool) Free

func (p *Pool) Free() int

Free returns the number of available workers, -1 indicates this pool is unlimited.

func (*Pool) IsClosed

func (p *Pool) IsClosed() bool

IsClosed indicates whether the pool is closed.

func (*Pool) Reboot

func (p *Pool) Reboot()

Reboot reboots a closed pool, it does nothing if the pool is not closed. If you intend to reboot a closed pool, use ReleaseTimeout() instead of Release() to ensure that all workers are stopped and resource are released before rebooting, otherwise you may run into data race.

func (*Pool) Release

func (p *Pool) Release()

Release closes this pool and releases the worker queue.

func (*Pool) ReleaseTimeout

func (p *Pool) ReleaseTimeout(timeout time.Duration) error

ReleaseTimeout is like Release but with a timeout, it waits all workers to exit before timing out.

func (*Pool) Running

func (p *Pool) Running() int

Running returns the number of workers currently running.

func (*Pool) Submit

func (p *Pool) Submit(task func()) error

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

func (p *Pool) Tune(size int)

Tune changes the capacity of this pool, note that it is noneffective to the infinite or pre-allocation pool.

func (*Pool) Waiting

func (p *Pool) Waiting() int

Waiting returns the number of tasks waiting to be executed.

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) Cap

func (p *PoolWithFunc) Cap() int

Cap returns the capacity of this pool.

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

func (p *PoolWithFunc) IsClosed() bool

IsClosed indicates whether the pool is closed.

func (*PoolWithFunc) Reboot

func (p *PoolWithFunc) Reboot()

Reboot reboots a closed pool, it does nothing if the pool is not closed. If you intend to reboot a closed pool, use ReleaseTimeout() instead of Release() to ensure that all workers are stopped and resource are released before rebooting, otherwise you may run into data race.

func (*PoolWithFunc) Release

func (p *PoolWithFunc) Release()

Release closes this pool and releases the worker queue.

func (*PoolWithFunc) ReleaseTimeout

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) Waiting

func (p *PoolWithFunc) Waiting() int

Waiting returns the number of tasks waiting to be executed.

Jump to

Keyboard shortcuts

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