mirror of
https://github.com/tailscale/tailscale.git
synced 2025-12-05 12:22:04 +00:00
tstime/rate: add Value (#7491)
Add Value, which measures the rate at which an event occurs, exponentially weighted towards recent activity. It is guaranteed to occupy O(1) memory, operate in O(1) runtime, and is safe for concurrent use. Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
@@ -31,12 +31,14 @@ func Every(interval time.Duration) Limit {
|
||||
}
|
||||
|
||||
// A Limiter controls how frequently events are allowed to happen.
|
||||
// It implements a "token bucket" of size b, initially full and refilled
|
||||
// at rate r tokens per second.
|
||||
// Informally, in any large enough time interval, the Limiter limits the
|
||||
// rate to r tokens per second, with a maximum burst size of b events.
|
||||
// See https://en.wikipedia.org/wiki/Token_bucket for more about token buckets.
|
||||
// It implements a [token bucket] of a particular size b,
|
||||
// initially full and refilled at rate r tokens per second.
|
||||
// Informally, in any large enough time interval,
|
||||
// the Limiter limits the rate to r tokens per second,
|
||||
// with a maximum burst size of b events.
|
||||
// Use NewLimiter to create non-zero Limiters.
|
||||
//
|
||||
// [token bucket]: https://en.wikipedia.org/wiki/Token_bucket
|
||||
type Limiter struct {
|
||||
limit Limit
|
||||
burst float64
|
||||
@@ -54,7 +56,7 @@ func NewLimiter(r Limit, b int) *Limiter {
|
||||
return &Limiter{limit: r, burst: float64(b)}
|
||||
}
|
||||
|
||||
// AllowN reports whether an event may happen now.
|
||||
// Allow reports whether an event may happen now.
|
||||
func (lim *Limiter) Allow() bool {
|
||||
return lim.allow(mono.Now())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user