Documentation ¶
Index ¶
- Constants
- type Limit
- type Limiter
- type RateCollector
- func (r *RateCollector) Add(label string, value float64)
- func (r *RateCollector) Deregister(label string)
- func (r *RateCollector) Max(label string, now time.Time) (float64, error)
- func (r *RateCollector) Min(label string, now time.Time) (float64, error)
- func (r *RateCollector) Rate(label string, duration time.Duration) (float64, error)
- func (r *RateCollector) Register(label string)
Constants ¶
const ( DefaultWindow = 10 * time.Second DefaultGranularity = 1 * time.Second DefaultAvgDuration = 3 * time.Second )
const Inf = Limit(math.MaxFloat64)
Inf is the infinite rate limit; it allows all events.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Limit ¶
type Limit float64
Limit defines the maximum frequency of some events. Limit is represented as number of events per second. A zero Limit allows no events.
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
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. See https://en.wikipedia.org/wiki/Token_bucket for more about token buckets.
However, Limiter is a little different from token-bucket. It is based on a special punishment mechanism, a very large number of events are allowed as long as the number of tokens in bucket is greater or equal to 0. After these large number of events toke tokens from bucket, the number of tokens in bucket may be negative, and the latter events would be "punished", any event should wait for the tokens to be filled to greater or equal to 0.
func NewLimiter ¶
NewLimiter returns a new Limiter that allows events up to rate r.
type RateCollector ¶
RateCollector helps to collect and calculate values (like throughput, QPS, TPS, etc...), It implements a sliding window with custom size and granularity to store values.
func NewRateCollector ¶
NewRateCollector is shorthand for newRateCollector(window, granularity, time.Now()).
func (*RateCollector) Add ¶
func (r *RateCollector) Add(label string, value float64)
Add is shorthand for add(label, value, time.Now()).
func (*RateCollector) Deregister ¶
func (r *RateCollector) Deregister(label string)
Deregister remove values of RateCollector for specified label.
func (*RateCollector) Register ¶
func (r *RateCollector) Register(label string)
Register init values of RateCollector for specified label.