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 JobItem
- type OptimizedJobItem
- type OptimizedPool
- func (p *OptimizedPool) Decrease(count int)
- func (p *OptimizedPool) IdleWorkerCount() int
- func (p *OptimizedPool) Increase(count int)
- func (p *OptimizedPool) QueuedJobCount() int
- func (p *OptimizedPool) RunningWorkerCount() int
- func (p *OptimizedPool) Stop()
- func (p *OptimizedPool) Submit(job func()) error
- func (p *OptimizedPool) WaitDone()
- func (p *OptimizedPool) WorkerCount() int
- 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")
)
var (
ErrMaxQueuedJobCountReachedOptimized = errors.New("max queued job count reached")
)
Functions ¶
This section is empty.
Types ¶
type OptimizedJobItem ¶
type OptimizedJobItem struct {
Stack string
Job func()
}
type OptimizedPool ¶
type OptimizedPool struct {
// contains filtered or unexported fields
}
OptimizedPool is an optimized goroutine pool with better performance
func NewOptimized ¶
func NewOptimized(workerCount int) *OptimizedPool
NewOptimized creates an optimized pool with better performance
func NewOptimizedWithOption ¶
func NewOptimizedWithOption(workerCount int, opt *Option) *OptimizedPool
NewOptimizedWithOption creates an optimized pool with options
func (*OptimizedPool) Decrease ¶
func (p *OptimizedPool) Decrease(count int)
Decrease removes workers (optimized version)
func (*OptimizedPool) IdleWorkerCount ¶
func (p *OptimizedPool) IdleWorkerCount() int
IdleWorkerCount returns the count of idle workers
func (*OptimizedPool) Increase ¶
func (p *OptimizedPool) Increase(count int)
Increase adds workers (optimized version)
func (*OptimizedPool) QueuedJobCount ¶
func (p *OptimizedPool) QueuedJobCount() int
QueuedJobCount returns the count of queued jobs
func (*OptimizedPool) RunningWorkerCount ¶
func (p *OptimizedPool) RunningWorkerCount() int
RunningWorkerCount returns the count of running workers
func (*OptimizedPool) Submit ¶
func (p *OptimizedPool) Submit(job func()) error
Submit adds a job to the queue (optimized version with no global lock)
func (*OptimizedPool) WaitDone ¶
func (p *OptimizedPool) WaitDone()
WaitDone waits for all jobs to complete
func (*OptimizedPool) WorkerCount ¶
func (p *OptimizedPool) WorkerCount() int
WorkerCount returns the count of workers
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{})
//WithStack sets if fill stack info with submitted job
WithStack bool
}
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)
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