throttle

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Throttle

type Throttle struct {
	// contains filtered or unexported fields
}

Throttle is a delay timer with a [Sleep] method for pausing loops. It is controlled by [SlowDown] and [Reset]. It is safe for use across multiple goroutines. It is lock-free. It has a linear back-off algorithm implemented by [SlowDown] and [Speedup].

All methods in a nil *Throttle are no-op.

func New

func New(minimum, initialStep, extraStep time.Duration) *Throttle

New returns a new Throttle with the minimum, initial and extra values specified.

  • If minimum is less than zero it is set to zero.
  • If initialStep is less than or equal to minimum, it will be set to minimum+1.
  • If extraStep is less than zero, with will be set to zero.

func (*Throttle) Delay

func (t *Throttle) Delay() time.Duration

Delay gets the current delay duration.

func (*Throttle) IsNormal

func (t *Throttle) IsNormal() bool

IsNormal returns true when the throttle is at its minimum.

func (*Throttle) Reset

func (t *Throttle) Reset()

Reset reverts the loop delay to its minimum value.

func (*Throttle) Sleep

func (t *Throttle) Sleep()

Sleep pauses this goroutine for the current loop delay. If the delay is zero, Sleep behaves as a no-op.

func (*Throttle) SlowDown

func (t *Throttle) SlowDown()

SlowDown increases the pause imposed when [Sleep] is called. The first time this is used, the throttle increases its delay to the initial step. Subsequently, it adds the extra step. This provides a linear back-off (n.b. not exponential).

func (*Throttle) SpeedUp

func (t *Throttle) SpeedUp()

SpeedUp decreases the pause imposed when [Sleep] is called by subtracting the extra step from the delay. It has no effect after the minimum delay is reached.

Jump to

Keyboard shortcuts

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