Documentation
¶
Overview ¶
Package rate defines Rate and provides basic interfaces for rate limiter driver implementations.
Index ¶
- type BypassLimiter
- type LeakyBucket
- type Limiter
- type Rate
- func (r *Rate) Burst() float64
- func (r *Rate) FasterThan(a *Rate) bool
- func (r *Rate) Interval() time.Duration
- func (r *Rate) LogValue() slog.Value
- func (r *Rate) PerNanosecond() float64
- func (r *Rate) ReplenishedTokens(from, to time.Time) float64
- func (r *Rate) SlowerThan(a *Rate) bool
- func (r *Rate) String() string
- func (r *Rate) Validate() error
- type TagFilter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BypassLimiter ¶
type BypassLimiter struct { Limiter // contains filtered or unexported fields }
BypassLimiter uses a TagFilter to selectively apply a Limiter.
type LeakyBucket ¶
type LeakyBucket struct {
// contains filtered or unexported fields
}
LeakyBucket keeps track of available tokens according to a given Rate.
func NewLeakyBucket ¶
func NewLeakyBucket(at time.Time, r *Rate, burstLimit float64) *LeakyBucket
NewLeakyBucket returns a full LeakyBucket.
func (*LeakyBucket) Refill ¶
func (l *LeakyBucket) Refill(at time.Time, r *Rate, burstLimit float64)
Refill calculates and returns tokens since last update to given time at a Rate. Restored tokens will not exceed the burst limit.
func (*LeakyBucket) Remaining ¶
func (l *LeakyBucket) Remaining() float64
Remaining returns the number of tokens in the bucket. Use only after running LeakyBucket.Refill.
func (*LeakyBucket) Take ¶
func (l *LeakyBucket) Take(tokens float64) (remaining float64, ok bool)
Take removes tokens from the bucket, if that many are available. Use only after running LeakyBucket.Refill.
func (*LeakyBucket) Touched ¶
func (l *LeakyBucket) Touched() time.Time
Touched returns the last time the bucket was updated.
type Limiter ¶
type Limiter interface { Rate() *Rate Remaining( ctx context.Context, tag string, ) (float64, error) Take( ctx context.Context, tag string, tokens float64, ) ( remaining float64, ok bool, err error, ) }
Limiter contrains the number of consumed tokens to a certain Rate.
func NewBypassLimiter ¶
func NewListBypassLimiter ¶
NewListBypassLimiter creates a BypassLimiter that never limits request tags from a given list.
type Rate ¶
type Rate struct {
// contains filtered or unexported fields
}
Rate represents replenishment of tokens per interval of time. The original interval is preserved in order to calculate default burst.
func (*Rate) FasterThan ¶
FasterThan returns true if this Rate replenishes more tokens per nanosecond than the other.
func (*Rate) LogValue ¶
LogValue satisfies slog.Valuer in order to provide more information when logging.
func (*Rate) PerNanosecond ¶
PerNanosecond returns the amount of tokens replenished per nanosecond.
func (*Rate) ReplenishedTokens ¶
ReplenishedTokens returns fractional token amount based on time passed.
func (*Rate) SlowerThan ¶
SlowerThan returns true if this Rate replenishes less tokens per nanosecond than the other.
type TagFilter ¶
TagFilter directs a BypassLimiter to drop tags that return false.