Documentation
¶
Overview ¶
Package mitigation provides a way to fast-path allows for keys that are not actively hitting their rate limit.
While a key is not mitigated, `Allow()` and `Wait()` will only do a `xsync.Map` lookup and then return immediately.
While a key is mitigated, a single goroutine will be responsible for checking the allowFn and distributing the the allowed requests to all callers evenly.
Index ¶
- type Key
- type Mitigation
- type MitigationCache
- func (mc *MitigationCache[K]) Allow(ctx context.Context, key K) bool
- func (mc *MitigationCache[K]) Close(key K)
- func (mc *MitigationCache[K]) CloseAll()
- func (mc *MitigationCache[K]) Contains(ctx context.Context, key K) bool
- func (mc *MitigationCache[K]) Trigger(ctx context.Context, key K, period time.Duration) bool
- func (mc *MitigationCache[K]) Wait(ctx context.Context, key K) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Key ¶
type Key interface {
comparable
fmt.Stringer
}
type Mitigation ¶
type Mitigation struct {
// contains filtered or unexported fields
}
Mitigation exists to allow caching of a mitigated state, and cooperative sharing of requests as the Mitigation expires and is refreshed
type MitigationCache ¶
type MitigationCache[K Key] struct { // contains filtered or unexported fields }
func (*MitigationCache[K]) Allow ¶
func (mc *MitigationCache[K]) Allow(ctx context.Context, key K) bool
Allow reports whether a request is allowed for the given key.
func (*MitigationCache[K]) Close ¶
func (mc *MitigationCache[K]) Close(key K)
Close removes a specific mitigation from the cache and stops its goroutine.
func (*MitigationCache[K]) CloseAll ¶
func (mc *MitigationCache[K]) CloseAll()
CloseAll cleans up all active mitigations, stopping their goroutines.
func (*MitigationCache[K]) Contains ¶
func (mc *MitigationCache[K]) Contains(ctx context.Context, key K) bool
Contains returns true if a mitigation exists for the given key.
func (*MitigationCache[K]) Trigger ¶
Trigger creates a new mitigation or refreshes an existing one's ttl
ctx is specifically for cancellation of the mitigation's garbage collector goroutine, which happens automatically if the mitigation expires.
key is the key to mitigate. Note that all keys are global
period is how often to retry the allowFn, which will be the maximum rate requests are allowed
allowFn is a function to call that will be the final gatekeeper for whether requests are allowed. The mitigation cache is specifically designed to call this as little as possible, as the allowFn is expected to be expensive. allowFn must be thread safe.
It returns true if the mitigation was freshly triggered, and false if it was already active and just had its ttl extended.
func (*MitigationCache[K]) Wait ¶
func (mc *MitigationCache[K]) Wait(ctx context.Context, key K) error
Wait blocks until the mitigation fires, is cancelled via context, or is done.
Note that currently, Wait() unsubscribe/resubscribes to the ringbuffer every time it's called which is pretty non-ideal.