Documentation
¶
Index ¶
- func NewLogger(t logger) *zap.Logger
- type Key
- type KeyConf
- type KeyConfFn
- type Limiter
- func (l *Limiter[K]) Allow(ctx context.Context, key K) (allowed bool)
- func (l *Limiter[K]) Close()
- func (l *Limiter[K]) Refresh(ctx context.Context)
- func (l *Limiter[K]) RefreshKey(ctx context.Context, key K)
- func (l *Limiter[K]) SetKeyConfFn(ctx context.Context, keyConfFn KeyConfFn[K])
- func (l *Limiter[K]) Wait(ctx context.Context, key K)
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Key ¶
type Key interface {
comparable
fmt.Stringer
}
type KeyConf ¶
KeyConf is a configuration for a key. It's set lazily per-key by calling Options.KeyConfFn
Rate is the number of allowed calls per interval.
Interval is the duration of the sliding window for the rate limit. Larger intervals will decrease the requests to redis.
type KeyConfFn ¶
KeyConfFn is a function that returns a KeyConf for a given key. It will be called lazily and cached forever, unless explicitly evicted.
type Limiter ¶
type Limiter[K Key] struct { // contains filtered or unexported fields }
func New ¶
func New[K Key](ctx context.Context, rdb redis.Cmdable, keyConfFn KeyConfFn[K], options ...Option[K]) *Limiter[K]
New creates a new Limiter with the provided Redis client and options
Note that rdb needs to be carefully configured for timeouts if you expect redis outages to not have severe impacts on latency.
KeyConfFn returns a KeyConf for a given key. This will be called lazily, once per key, until `Limiter.Refresh()` or `Limiter.RefreshKey(key string)` is called.
func (*Limiter[K]) Close ¶
func (l *Limiter[K]) Close()
Close stops all goroutines, flushes logging, and waits for completion.
func (*Limiter[K]) Refresh ¶
Refresh causes the KeyConfFn to be called again for all keys (lazily).
If your rate limit is changing globally, you should call this once the keyConfFn is returning the new result
func (*Limiter[K]) RefreshKey ¶
RefreshKey causes the KeyConfFn to be called again for the specified key (lazily).
If your rate limit is changing for a specific key, you should call this once the keyConfFn is returning the new result
func (*Limiter[K]) SetKeyConfFn ¶
SetKeyConfFn sets the KeyConfFn on the limiter and clears all mitigations
type Option ¶
func OptionWithBatchDuration ¶
OptionWithBatchDuration sets how long to wait maximum before flushing all waiting increments.
This is proportional to both how much over the limit a key can go before being mitigated, and inversely proportional to how many writes to the remote cache are made.
It is important that this is set to a value that is smaller thant the smallest KeyConf interval, or a mitigation will never be triggered. Ideally at least 4x smaller, if you want smooth rates.
The default is set with the assumption that per second limits are going to happen.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
fifo
Package fifo provides a linked-list based fifo queue optimized for pushing and shifting
|
Package fifo provides a linked-list based fifo queue optimized for pushing and shifting |
|
mitigation
Package mitigation provides a way to fast-path allows for keys that are not actively hitting their rate limit.
|
Package mitigation provides a way to fast-path allows for keys that are not actively hitting their rate limit. |