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 ¶
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.
Click to show internal directories.
Click to hide internal directories.