throttle

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: LGPL-2.1 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TimeoutError error

Functions

This section is empty.

Types

type Throttle

type Throttle[T comparable, V any] struct {
	// GlobalSpawn is a channel that spawns tokens at GlobalRate.
	GlobalSpawn *time.Ticker

	// EntitySpawnMux is a mutex over EntitySpawn.
	EntitySpawnMux sync.RWMutex

	// EntitySpawn maps entities to their individual channel that spawns tokens at EntityRate.
	EntitySpawn map[T]*time.Ticker

	// EntityRate is the rate at which entity-specific tokens are spanwed.
	EntityRate time.Duration
}

Throttle allows to throttles the usage of values of type V identified by entities of type T.

func New

func New[T comparable, V any](globalRate, entityRate time.Duration) *Throttle[T, V]

New creates a new Throttle by defining its global and entity token spawn rate.

func (*Throttle[T, V]) Await

func (t *Throttle[T, V]) Await(entity T, value *V, timeout time.Duration) (*V, error)

Await waits for the throttle object to spawn both a token for the individual entity and globally. If those tokens are spawned within the given timeout, the given value is returned back. Otherwise, TimeoutError is returned. The first token (globally, per entity) is spawned immediately.

Jump to

Keyboard shortcuts

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