Documentation ¶
Overview ¶
Package gpool is a goroutine pool, it is concurrency safed, it can using a few goroutine to run a huge tasks. a worker is a goroutine, a task is a function, gpool using a few workers to run a huage tasks.
Index ¶
- Variables
- type Option
- type Pool
- func (s *Pool) Blocking() bool
- func (s *Pool) Decrease(workerCount int)
- func (s *Pool) IdleDuration() time.Duration
- func (s *Pool) IdleWorkerCount() (workerCount int)
- func (s *Pool) Increase(workerCount int)
- func (s *Pool) IsDebug() bool
- func (s *Pool) MaxJobCount() int
- func (s *Pool) QueuedJobCount() (jobCount int)
- func (s *Pool) ResetTo(workerCount int)
- func (s *Pool) RunningWorkerCount() (workerCount int)
- func (s *Pool) SetBlocking(blocking bool)
- func (s *Pool) SetDebug(debug bool)
- func (s *Pool) SetIdleDuration(idleDuration time.Duration)
- func (s *Pool) SetLogger(l gcore.Logger)
- func (s *Pool) SetMaxJobCount(maxJobCount int)
- func (s *Pool) Stop()
- func (s *Pool) Submit(job func()) error
- func (s *Pool) WaitDone()
- func (s *Pool) WorkerCount() int
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrMaxQueuedJobCountReached = errors.New("max queued job count reached")
)
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option struct { //limits the max queued job count, 0 no limit MaxJobCount int // block the Submit call after the count of queued job to run reach the max, only worked on MaxJobCount is greater 0 Blocking bool // output the debug logging, only worked on the pool Logger is not nil Debug bool // the logger to output debug logging Logger gcore.Logger // if IdleDuration nonzero, the worker will exited after idle duration when complete the job IdleDuration time.Duration // start the worker when the pool created PreAlloc bool // PanicHandler is used to handle panics from each job function. PanicHandler func(e interface{}) }
Option sets the pool
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool is a goroutine pool, you can increase or decrease pool size in runtime.
func New ¶
New create a gpool object to using
Example ¶
//we create a poll named "p" with 3 workers p := New(3) //after New, you can submit a function as a task, you can repeat Submit() many times anywhere as you need. a := make(chan bool) p.Submit(func() { a <- true }) fmt.Println(<-a)
Output:
func NewWithOption ¶
func NewWithPreAlloc ¶
func (*Pool) Blocking ¶
Blocking the count of queued job to run reach the max, if blocking Submit call
func (*Pool) IdleDuration ¶
IdleDuration is the idle time duration before the worker exit, duration 0 means the work will not exit.
func (*Pool) IdleWorkerCount ¶
IdleWorkerCount returns the count of idle workers
func (*Pool) MaxJobCount ¶
MaxJobCount returns the max queued job count.
func (*Pool) QueuedJobCount ¶
QueuedJobCount returns the count of queued job
func (*Pool) RunningWorkerCount ¶
RunningWorkerCount returns the count of running workers
func (*Pool) SetBlocking ¶
SetBlocking sets the count of queued job to run reach the max, if blocking Submit call
func (*Pool) SetIdleDuration ¶
SetIdleDuration set the idle time duration before the worker exit, duration 0 means the work will not exit.
Notice: if idle duration changed from zero, only the new worker will support the idle.
func (*Pool) SetLogger ¶
SetLogger set the logger to logging, you can SetLogger(nil) to disable logging
default is log.New(os.Stdout, "", log.LstdFlags),
func (*Pool) SetMaxJobCount ¶
SetMaxJobCount sets the max queued job count.
func (*Pool) WaitDone ¶
func (s *Pool) WaitDone()
WaitDone wait all the jobs submitted executed done, if no job, return immediately.
func (*Pool) WorkerCount ¶
WorkerCount returns the count of workers