gconcurrent

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTimeout = errors.New("add to job queue timeout")
)

Functions

This section is empty.

Types

type JobFunc

type JobFunc func(context.Context)

JobFunc is a job function will execute by worker goroutine

type PanicFunc

type PanicFunc func(recovered interface{}, funcName string)

PanicFunc is a function will process panic which throw in worker function

type TimeoutCond

type TimeoutCond struct {
	L sync.Locker
	// contains filtered or unexported fields
}

TimeoutCond is a sync.Cond improve for support wait timeout.

func NewTimeoutCond

func NewTimeoutCond(l sync.Locker) *TimeoutCond

NewTimeoutCond return a new TimeoutCond

func (*TimeoutCond) HasWaiters

func (cond *TimeoutCond) HasWaiters() bool

HasWaiters queries whether any goroutine are waiting on this condition

func (*TimeoutCond) Interrupt

func (cond *TimeoutCond) Interrupt()

Interrupt goroutine wait on this TimeoutCond

func (*TimeoutCond) Signal

func (cond *TimeoutCond) Signal()

Signal wakes one goroutine waiting on c, if there is any.

func (*TimeoutCond) Wait

func (cond *TimeoutCond) Wait(ctx context.Context) bool

Wait waits for a signal, or for the context do be done. Returns true if signaled.

type WorkerPool

type WorkerPool interface {
	// Execute add worker function to queue to wait for executing by worker goroutine
	Execute(f JobFunc)

	// Execute summit worker function to queue  with a timeout timer,
	// if add queue success it will wait for executing by worker goroutine
	Submit(f JobFunc, timeout time.Duration) error

	// Stop cancel all goroutines started by this pool and wait
	Shutdown(ctx context.Context)

	// Stats return the statistics of worker pool
	Stats() WpStats

	// Option return the configuration of worker pool
	Option() WpOption
}

func NewWorkerPool

func NewWorkerPool(opt ...WpOption) WorkerPool

NewWorkerPool creates a instance of WorkerPool with given option default parameters:

queueSize: 100
initWorkerNum: 2
maxWorkerNum: 50

type WpOption

type WpOption struct {
	InitWorkerNum int       `json:"init_worker_num" yaml:"init_worker_num"`
	MaxWorkerNum  int       `json:"max_worker_num" yaml:"max_worker_num"`
	QueueSize     int       `json:"queue_size" yaml:"queue_size"`
	PanicFunc     PanicFunc `json:"-"`
}

WpOption is the worker pool parameter

type WpStats

type WpStats struct {
	ActiveNum     int32
	WorkerNum     int32
	ExecuteNum    int32
	SubmitFailNum int32
	PanicNum      int32
}

WpStats is the statistics of worker pool

Jump to

Keyboard shortcuts

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