pacer

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2022 License: MIT Imports: 1 Imported by: 0

README

pacer

GoDoc

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.

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 NewPacer

func NewPacer(delay time.Duration) *Pacer

NewPacer creates and runs a new Pacer.

func (*Pacer) IsPaused

func (p *Pacer) IsPaused() bool

IsPaused returns true if execution is paused.

func (*Pacer) Next

func (p *Pacer) Next()

Next submits a run request to the gate and returns when it is time to run.

func (*Pacer) Pace

func (p *Pacer) Pace(task func()) func()

Pace wraps a function in a paced function. The returned paced function can then be submitted to the workerpool, using Submit or SubmitWait, and starting the tasks is paced according to the pacer's delay.

func (*Pacer) Pause

func (p *Pacer) Pause()

Pause suspends execution of any tasks by the pacer.

func (*Pacer) Resume

func (p *Pacer) Resume()

Resume continues execution after Pause.

func (*Pacer) Stop

func (p *Pacer) Stop()

Stop stops the Pacer from running. Do not call until all paced tasks have completed, or paced tasks will hang waiting for pacer to unblock them.

Jump to

Keyboard shortcuts

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