gopool

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2020 License: BSD-2-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClosed = run.ErrClosed
)
View Source
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 Event

type Event int

Event enum of a runner

const (
	Start Event = iota // runner starts
	Exit               // runner exits
)

Event constants

func (Event) String

func (e Event) String() string

String representation of int enum

type Func

type Func = run.Func

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

func (g *Group) Cancel()

Cancel cancels the group

func (*Group) Go

func (g *Group) Go(runner Runner) error

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

func (*Group) Wait

func (g *Group) Wait() error

Wait waits for all goroutines exit and returns the first returned 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 GroupPool

type GroupPool interface {
	Go(ctx context.Context, fn func()) error
}

GroupPool is an interface for a goroutine pool used by Group

type LogInfo

type LogInfo struct {
	Runner Runner
	Event  Event
	Err    error
}

LogInfo is a logging event of a runner

func (*LogInfo) RunnerName

func (li *LogInfo) RunnerName() string

RunnerName returns a meaningful name of the runner for logging

func (*LogInfo) String

func (li *LogInfo) String() string

LogInfo provides a default string representation of the LogInfo

type PanicError

type PanicError struct {
	Err   interface{}
	Stack []byte
}

PanicError represents recovered panic info

func (*PanicError) Error

func (e *PanicError) Error() string

Error satisifies error interface

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

type Runner

type Runner = run.Runner

Jump to

Keyboard shortcuts

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