Documentation
¶
Overview ¶
Package limiter provides a distributed rate limiting abstraction built on a Maker/Limiter pair.
A Maker creates Limiter instances for named scenes, and a Limiter decides whether requests identified by a dimension key are allowed. The package holds a global maker so that limiters can be created through Make without passing the maker around explicitly.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type FailurePolicy ¶
type FailurePolicy int
FailurePolicy determines how a backend behaves when it cannot make a decision.
const ( // FailOpen allows the request when the backend is unavailable. FailOpen FailurePolicy = iota + 1 // FailClosed rejects the request when the backend is unavailable. FailClosed )
type Limiter ¶
type Limiter interface {
// Allow takes n tokens from the bucket identified by key and reports the decision. n defaults
// to 1. A denied request is not an error: it is reported by Result.Allowed.
Allow(ctx context.Context, key string, n ...int) (Result, error)
}
Limiter decides whether requests identified by key are allowed for a scene.
type Maker ¶
type Maker interface {
// Make creates a limiter for the named scene and rule.
Make(name string, rule Rule) (Limiter, error)
// Close closes the maker.
Close() error
}
Maker creates and closes limiters.
type Result ¶
type Result struct {
Allowed bool // Allowed reports whether the request was allowed.
Limit int // Limit is the bucket capacity.
Remaining int // Remaining is the number of tokens left after the decision.
RetryAfter time.Duration // RetryAfter is the suggested wait time when the request is denied.
ResetAfter time.Duration // ResetAfter is the time needed to refill the bucket.
Degraded bool // Degraded reports whether the request was allowed by fail-open.
}
Result reports the outcome of a rate limiting decision.
type Rule ¶
type Rule struct {
Quota float64 // Quota is the number of requests allowed per Period.
Period time.Duration // Period is the quota period; it defaults to one second.
Burst int // Burst is the bucket capacity; it defaults to ceil(Quota).
}
Rule describes a rate limiting rule.
Quota is the number of requests allowed per Period. Period defaults to one second and Burst defaults to ceil(Quota), so a zero Burst allows a burst equal to the quota.