Documentation ¶
Overview ¶
Package pacer provides a utility to limit the rate at which concurrent goroutines begin execution. This addresses situations where running the concurrent goroutines is OK, as long as their execution does not start at the same time.
The pacer package is independent of the workerpool package. Paced functions can be submitted to a workerpool or can be run as goroutines, and execution will be paced in both cases.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Pacer ¶
type Pacer struct {
// contains filtered or unexported fields
}
Pacer is a goroutine rate limiter. When concurrent goroutines call Pacer.Next(), the call returns in a single goroutine at a time, at a rate no faster than one per delay time.
To use Pacer, create a new Pacer giving the interval that must elapse between the start of one task the start of the next task. Then call Pace(), passing your task function. A new paced task function is returned that can then be passed to WorkerPool's Submit() or SubmitWait(), or called as a go routine. Paced functions, that are run as goroutines, are also paced. For example:
pacer := pacer.NewPacer(time.Second) pacedTask := pacer.Pace(func() { fmt.Println("Hello World") }) wp := workerpool.New(5) wp.Submit(pacedTask) go pacedTask()
NOTE: Do not call Pacer.Stop() until all paced tasks have completed, or paced tasks will hang waiting for pacer to unblock them.
func (*Pacer) Next ¶
func (p *Pacer) Next()
Next submits a run request to the gate and returns when it is time to run.