Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrClosed = run.ErrClosed
)
var ErrDispatchTimeout = errors.New("failed to dispatch the goroutine due to timeout")
ErrDispatchTimeout is returned when the context is cancelled when waiting for a task to be dispatched
Functions ¶
This section is empty.
Types ¶
type GoroutinePool ¶
type GoroutinePool struct {
// contains filtered or unexported fields
}
GoroutinePool provides a goroutine pool, see the documentation for method Go for more information
func NewGoroutinePool ¶
func NewGoroutinePool(options ...PoolOption) *GoroutinePool
NewGoroutinePool creates a new GoroutinePool based on the options provided
func (*GoroutinePool) Close ¶
func (p *GoroutinePool) Close() error
Close stops the pool from accepting new tasks, waits for existing tasks complete and return nil. All subsequent calls will return ErrClosed
func (*GoroutinePool) Go ¶
func (p *GoroutinePool) Go(ctx context.Context, fn func()) error
Go tries to dispatch function fn onto its own goroutine. It returns nil if fn is successfully dispatched. It returns ErrClosed if the pool is already closed. It returns ErrDispatchTimeout if the context is cancelled when waiting for an idle goroutine to be available.
A gouroutine will stay idle and be reused for a period specified by IdleTime option (default 1s).
If Max option is specified, there will be a maximum limit on the goroutines that the pool can hold in total, and Go will block and wait for an idle goroutine is available. Otherwise, there is no limit on the goroutine number.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group combines multiple concurrent tasks into one
func NewGroup ¶
func NewGroup(ctx context.Context, options ...GroupOption) *Group
NewGroup creates a new Group
func (*Group) Go ¶
Go runs the given runner in the internal goroutine pool. It returns nil when the goroutine is dispatched successfully. It returns ErrDispatchTimeout if the context of the group is cancelled when waiting for an idle goroutine to be available. The first error return from a runner cancels the group, and all subsequent calls to Go as well as Wait will return the error
type GroupOption ¶
type GroupOption func(*Group)
GroupOption is used to specify an option for Group
func Log ¶
func Log(logFunc func(info *LogInfo)) GroupOption
Log specifies the logging function for a group, if not set, the LogInfo is not generated
func Pool ¶
func Pool(p *GoroutinePool) GroupOption
Pool specifies the goroutine pool for a group, if not set, a dummy implementation is used (always starting new goroutines)
func Recover ¶
func Recover(yes bool) GroupOption
Recover specifies if a panic in the runner goroutine should be recovered or not, if not set, the default behavior is recovered.
type LogInfo ¶
LogInfo is a logging event of a runner
func (*LogInfo) RunnerName ¶
RunnerName returns a meaningful name of the runner for logging
type PanicError ¶
type PanicError struct { Err interface{} Stack []byte }
PanicError represents recovered panic info
type PoolOption ¶
type PoolOption func(*GoroutinePool)
PoolOption is used to specify an option for GoroutinePool
func IdleTime ¶
func IdleTime(idleTime time.Duration) PoolOption
IdleTime returns the option to specify the idle time before a goroutine exits and releases the resource if no task is submitted, if not specified, the default idle time is 1s
func Max ¶
func Max(n int) PoolOption
Max returns the option to specifiy the maximum number of goroutines that a GoroutinePool can hold, if not specified, there is no upper limit