forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/time/rate/rate.go

744 lines
13 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2015 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
// Package rate provides a rate limiter.
2024-02-18 10:42:21 +00:00
package rate
import (
"context"
"fmt"
"math"
"sync"
"time"
)
// Limit defines the maximum frequency of some events.
2024-02-18 10:42:21 +00:00
// Limit is represented as number of events per second.
2024-02-18 10:42:21 +00:00
// A zero Limit allows no events.
2024-02-18 10:42:21 +00:00
type Limit float64
// Inf is the infinite rate limit; it allows all events (even if burst is zero).
2024-02-18 10:42:21 +00:00
const Inf = Limit(math.MaxFloat64)
// Every converts a minimum time interval between events to a Limit.
2024-02-18 10:42:21 +00:00
func Every(interval time.Duration) Limit {
2024-02-18 10:42:21 +00:00
if interval <= 0 {
2024-02-18 10:42:21 +00:00
return Inf
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return 1 / Limit(interval.Seconds())
2024-02-18 10:42:21 +00:00
}
// A Limiter controls how frequently events are allowed to happen.
2024-02-18 10:42:21 +00:00
// It implements a "token bucket" of size b, initially full and refilled
2024-02-18 10:42:21 +00:00
// at rate r tokens per second.
2024-02-18 10:42:21 +00:00
// Informally, in any large enough time interval, the Limiter limits the
2024-02-18 10:42:21 +00:00
// rate to r tokens per second, with a maximum burst size of b events.
2024-02-18 10:42:21 +00:00
// As a special case, if r == Inf (the infinite rate), b is ignored.
2024-02-18 10:42:21 +00:00
// See https://en.wikipedia.org/wiki/Token_bucket for more about token buckets.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The zero value is a valid Limiter, but it will reject all events.
2024-02-18 10:42:21 +00:00
// Use NewLimiter to create non-zero Limiters.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Limiter has three main methods, Allow, Reserve, and Wait.
2024-02-18 10:42:21 +00:00
// Most callers should use Wait.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Each of the three methods consumes a single token.
2024-02-18 10:42:21 +00:00
// They differ in their behavior when no token is available.
2024-02-18 10:42:21 +00:00
// If no token is available, Allow returns false.
2024-02-18 10:42:21 +00:00
// If no token is available, Reserve returns a reservation for a future token
2024-02-18 10:42:21 +00:00
// and the amount of time the caller must wait before using it.
2024-02-18 10:42:21 +00:00
// If no token is available, Wait blocks until one can be obtained
2024-02-18 10:42:21 +00:00
// or its associated context.Context is canceled.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The methods AllowN, ReserveN, and WaitN consume n tokens.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Limiter is safe for simultaneous use by multiple goroutines.
2024-02-18 10:42:21 +00:00
type Limiter struct {
mu sync.Mutex
limit Limit
burst int
2024-02-18 10:42:21 +00:00
tokens float64
2024-02-18 10:42:21 +00:00
// last is the last time the limiter's tokens field was updated
2024-02-18 10:42:21 +00:00
last time.Time
2024-02-18 10:42:21 +00:00
// lastEvent is the latest time of a rate-limited event (past or future)
2024-02-18 10:42:21 +00:00
lastEvent time.Time
}
// Limit returns the maximum overall event rate.
2024-02-18 10:42:21 +00:00
func (lim *Limiter) Limit() Limit {
2024-02-18 10:42:21 +00:00
lim.mu.Lock()
2024-02-18 10:42:21 +00:00
defer lim.mu.Unlock()
2024-02-18 10:42:21 +00:00
return lim.limit
2024-02-18 10:42:21 +00:00
}
// Burst returns the maximum burst size. Burst is the maximum number of tokens
2024-02-18 10:42:21 +00:00
// that can be consumed in a single call to Allow, Reserve, or Wait, so higher
2024-02-18 10:42:21 +00:00
// Burst values allow more events to happen at once.
2024-02-18 10:42:21 +00:00
// A zero Burst allows no events, unless limit == Inf.
2024-02-18 10:42:21 +00:00
func (lim *Limiter) Burst() int {
2024-02-18 10:42:21 +00:00
lim.mu.Lock()
2024-02-18 10:42:21 +00:00
defer lim.mu.Unlock()
2024-02-18 10:42:21 +00:00
return lim.burst
2024-02-18 10:42:21 +00:00
}
// TokensAt returns the number of tokens available at time t.
2024-02-18 10:42:21 +00:00
func (lim *Limiter) TokensAt(t time.Time) float64 {
2024-02-18 10:42:21 +00:00
lim.mu.Lock()
2024-02-18 10:42:21 +00:00
_, tokens := lim.advance(t) // does not mutate lim
2024-02-18 10:42:21 +00:00
lim.mu.Unlock()
2024-02-18 10:42:21 +00:00
return tokens
2024-02-18 10:42:21 +00:00
}
// Tokens returns the number of tokens available now.
2024-02-18 10:42:21 +00:00
func (lim *Limiter) Tokens() float64 {
2024-02-18 10:42:21 +00:00
return lim.TokensAt(time.Now())
2024-02-18 10:42:21 +00:00
}
// NewLimiter returns a new Limiter that allows events up to rate r and permits
2024-02-18 10:42:21 +00:00
// bursts of at most b tokens.
2024-02-18 10:42:21 +00:00
func NewLimiter(r Limit, b int) *Limiter {
2024-02-18 10:42:21 +00:00
return &Limiter{
2024-02-18 10:42:21 +00:00
limit: r,
2024-02-18 10:42:21 +00:00
burst: b,
}
2024-02-18 10:42:21 +00:00
}
// Allow reports whether an event may happen now.
2024-02-18 10:42:21 +00:00
func (lim *Limiter) Allow() bool {
2024-02-18 10:42:21 +00:00
return lim.AllowN(time.Now(), 1)
2024-02-18 10:42:21 +00:00
}
// AllowN reports whether n events may happen at time t.
2024-02-18 10:42:21 +00:00
// Use this method if you intend to drop / skip events that exceed the rate limit.
2024-02-18 10:42:21 +00:00
// Otherwise use Reserve or Wait.
2024-02-18 10:42:21 +00:00
func (lim *Limiter) AllowN(t time.Time, n int) bool {
2024-02-18 10:42:21 +00:00
return lim.reserveN(t, n, 0).ok
2024-02-18 10:42:21 +00:00
}
// A Reservation holds information about events that are permitted by a Limiter to happen after a delay.
2024-02-18 10:42:21 +00:00
// A Reservation may be canceled, which may enable the Limiter to permit additional events.
2024-02-18 10:42:21 +00:00
type Reservation struct {
ok bool
lim *Limiter
tokens int
2024-02-18 10:42:21 +00:00
timeToAct time.Time
2024-02-18 10:42:21 +00:00
// This is the Limit at reservation time, it can change later.
2024-02-18 10:42:21 +00:00
limit Limit
}
// OK returns whether the limiter can provide the requested number of tokens
2024-02-18 10:42:21 +00:00
// within the maximum wait time. If OK is false, Delay returns InfDuration, and
2024-02-18 10:42:21 +00:00
// Cancel does nothing.
2024-02-18 10:42:21 +00:00
func (r *Reservation) OK() bool {
2024-02-18 10:42:21 +00:00
return r.ok
2024-02-18 10:42:21 +00:00
}
// Delay is shorthand for DelayFrom(time.Now()).
2024-02-18 10:42:21 +00:00
func (r *Reservation) Delay() time.Duration {
2024-02-18 10:42:21 +00:00
return r.DelayFrom(time.Now())
2024-02-18 10:42:21 +00:00
}
// InfDuration is the duration returned by Delay when a Reservation is not OK.
2024-02-18 10:42:21 +00:00
const InfDuration = time.Duration(math.MaxInt64)
// DelayFrom returns the duration for which the reservation holder must wait
2024-02-18 10:42:21 +00:00
// before taking the reserved action. Zero duration means act immediately.
2024-02-18 10:42:21 +00:00
// InfDuration means the limiter cannot grant the tokens requested in this
2024-02-18 10:42:21 +00:00
// Reservation within the maximum wait time.
2024-02-18 10:42:21 +00:00
func (r *Reservation) DelayFrom(t time.Time) time.Duration {
2024-02-18 10:42:21 +00:00
if !r.ok {
2024-02-18 10:42:21 +00:00
return InfDuration
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
delay := r.timeToAct.Sub(t)
2024-02-18 10:42:21 +00:00
if delay < 0 {
2024-02-18 10:42:21 +00:00
return 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return delay
2024-02-18 10:42:21 +00:00
}
// Cancel is shorthand for CancelAt(time.Now()).
2024-02-18 10:42:21 +00:00
func (r *Reservation) Cancel() {
2024-02-18 10:42:21 +00:00
r.CancelAt(time.Now())
2024-02-18 10:42:21 +00:00
}
// CancelAt indicates that the reservation holder will not perform the reserved action
2024-02-18 10:42:21 +00:00
// and reverses the effects of this Reservation on the rate limit as much as possible,
2024-02-18 10:42:21 +00:00
// considering that other reservations may have already been made.
2024-02-18 10:42:21 +00:00
func (r *Reservation) CancelAt(t time.Time) {
2024-02-18 10:42:21 +00:00
if !r.ok {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
r.lim.mu.Lock()
2024-02-18 10:42:21 +00:00
defer r.lim.mu.Unlock()
if r.lim.limit == Inf || r.tokens == 0 || r.timeToAct.Before(t) {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// calculate tokens to restore
2024-02-18 10:42:21 +00:00
// The duration between lim.lastEvent and r.timeToAct tells us how many tokens were reserved
2024-02-18 10:42:21 +00:00
// after r was obtained. These tokens should not be restored.
2024-02-18 10:42:21 +00:00
restoreTokens := float64(r.tokens) - r.limit.tokensFromDuration(r.lim.lastEvent.Sub(r.timeToAct))
2024-02-18 10:42:21 +00:00
if restoreTokens <= 0 {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// advance time to now
2024-02-18 10:42:21 +00:00
t, tokens := r.lim.advance(t)
2024-02-18 10:42:21 +00:00
// calculate new number of tokens
2024-02-18 10:42:21 +00:00
tokens += restoreTokens
2024-02-18 10:42:21 +00:00
if burst := float64(r.lim.burst); tokens > burst {
2024-02-18 10:42:21 +00:00
tokens = burst
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// update state
2024-02-18 10:42:21 +00:00
r.lim.last = t
2024-02-18 10:42:21 +00:00
r.lim.tokens = tokens
2024-02-18 10:42:21 +00:00
if r.timeToAct == r.lim.lastEvent {
2024-02-18 10:42:21 +00:00
prevEvent := r.timeToAct.Add(r.limit.durationFromTokens(float64(-r.tokens)))
2024-02-18 10:42:21 +00:00
if !prevEvent.Before(t) {
2024-02-18 10:42:21 +00:00
r.lim.lastEvent = prevEvent
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Reserve is shorthand for ReserveN(time.Now(), 1).
2024-02-18 10:42:21 +00:00
func (lim *Limiter) Reserve() *Reservation {
2024-02-18 10:42:21 +00:00
return lim.ReserveN(time.Now(), 1)
2024-02-18 10:42:21 +00:00
}
// ReserveN returns a Reservation that indicates how long the caller must wait before n events happen.
2024-02-18 10:42:21 +00:00
// The Limiter takes this Reservation into account when allowing future events.
2024-02-18 10:42:21 +00:00
// The returned Reservations OK() method returns false if n exceeds the Limiter's burst size.
2024-02-18 10:42:21 +00:00
// Usage example:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// r := lim.ReserveN(time.Now(), 1)
2024-02-18 10:42:21 +00:00
// if !r.OK() {
2024-02-18 10:42:21 +00:00
// // Not allowed to act! Did you remember to set lim.burst to be > 0 ?
2024-02-18 10:42:21 +00:00
// return
2024-02-18 10:42:21 +00:00
// }
2024-02-18 10:42:21 +00:00
// time.Sleep(r.Delay())
2024-02-18 10:42:21 +00:00
// Act()
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Use this method if you wish to wait and slow down in accordance with the rate limit without dropping events.
2024-02-18 10:42:21 +00:00
// If you need to respect a deadline or cancel the delay, use Wait instead.
2024-02-18 10:42:21 +00:00
// To drop or skip events exceeding rate limit, use Allow instead.
2024-02-18 10:42:21 +00:00
func (lim *Limiter) ReserveN(t time.Time, n int) *Reservation {
2024-02-18 10:42:21 +00:00
r := lim.reserveN(t, n, InfDuration)
2024-02-18 10:42:21 +00:00
return &r
2024-02-18 10:42:21 +00:00
}
// Wait is shorthand for WaitN(ctx, 1).
2024-02-18 10:42:21 +00:00
func (lim *Limiter) Wait(ctx context.Context) (err error) {
2024-02-18 10:42:21 +00:00
return lim.WaitN(ctx, 1)
2024-02-18 10:42:21 +00:00
}
// WaitN blocks until lim permits n events to happen.
2024-02-18 10:42:21 +00:00
// It returns an error if n exceeds the Limiter's burst size, the Context is
2024-02-18 10:42:21 +00:00
// canceled, or the expected wait time exceeds the Context's Deadline.
2024-02-18 10:42:21 +00:00
// The burst limit is ignored if the rate limit is Inf.
2024-02-18 10:42:21 +00:00
func (lim *Limiter) WaitN(ctx context.Context, n int) (err error) {
2024-02-18 10:42:21 +00:00
// The test code calls lim.wait with a fake timer generator.
2024-02-18 10:42:21 +00:00
// This is the real timer generator.
2024-02-18 10:42:21 +00:00
newTimer := func(d time.Duration) (<-chan time.Time, func() bool, func()) {
2024-02-18 10:42:21 +00:00
timer := time.NewTimer(d)
2024-02-18 10:42:21 +00:00
return timer.C, timer.Stop, func() {}
2024-02-18 10:42:21 +00:00
}
return lim.wait(ctx, n, time.Now(), newTimer)
2024-02-18 10:42:21 +00:00
}
// wait is the internal implementation of WaitN.
2024-02-18 10:42:21 +00:00
func (lim *Limiter) wait(ctx context.Context, n int, t time.Time, newTimer func(d time.Duration) (<-chan time.Time, func() bool, func())) error {
2024-02-18 10:42:21 +00:00
lim.mu.Lock()
2024-02-18 10:42:21 +00:00
burst := lim.burst
2024-02-18 10:42:21 +00:00
limit := lim.limit
2024-02-18 10:42:21 +00:00
lim.mu.Unlock()
if n > burst && limit != Inf {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("rate: Wait(n=%d) exceeds limiter's burst %d", n, burst)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Check if ctx is already cancelled
2024-02-18 10:42:21 +00:00
select {
2024-02-18 10:42:21 +00:00
case <-ctx.Done():
2024-02-18 10:42:21 +00:00
return ctx.Err()
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Determine wait limit
2024-02-18 10:42:21 +00:00
waitLimit := InfDuration
2024-02-18 10:42:21 +00:00
if deadline, ok := ctx.Deadline(); ok {
2024-02-18 10:42:21 +00:00
waitLimit = deadline.Sub(t)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Reserve
2024-02-18 10:42:21 +00:00
r := lim.reserveN(t, n, waitLimit)
2024-02-18 10:42:21 +00:00
if !r.ok {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("rate: Wait(n=%d) would exceed context deadline", n)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Wait if necessary
2024-02-18 10:42:21 +00:00
delay := r.DelayFrom(t)
2024-02-18 10:42:21 +00:00
if delay == 0 {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
ch, stop, advance := newTimer(delay)
2024-02-18 10:42:21 +00:00
defer stop()
2024-02-18 10:42:21 +00:00
advance() // only has an effect when testing
2024-02-18 10:42:21 +00:00
select {
2024-02-18 10:42:21 +00:00
case <-ch:
2024-02-18 10:42:21 +00:00
// We can proceed.
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case <-ctx.Done():
2024-02-18 10:42:21 +00:00
// Context was canceled before we could proceed. Cancel the
2024-02-18 10:42:21 +00:00
// reservation, which may permit other events to proceed sooner.
2024-02-18 10:42:21 +00:00
r.Cancel()
2024-02-18 10:42:21 +00:00
return ctx.Err()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// SetLimit is shorthand for SetLimitAt(time.Now(), newLimit).
2024-02-18 10:42:21 +00:00
func (lim *Limiter) SetLimit(newLimit Limit) {
2024-02-18 10:42:21 +00:00
lim.SetLimitAt(time.Now(), newLimit)
2024-02-18 10:42:21 +00:00
}
// SetLimitAt sets a new Limit for the limiter. The new Limit, and Burst, may be violated
2024-02-18 10:42:21 +00:00
// or underutilized by those which reserved (using Reserve or Wait) but did not yet act
2024-02-18 10:42:21 +00:00
// before SetLimitAt was called.
2024-02-18 10:42:21 +00:00
func (lim *Limiter) SetLimitAt(t time.Time, newLimit Limit) {
2024-02-18 10:42:21 +00:00
lim.mu.Lock()
2024-02-18 10:42:21 +00:00
defer lim.mu.Unlock()
t, tokens := lim.advance(t)
lim.last = t
2024-02-18 10:42:21 +00:00
lim.tokens = tokens
2024-02-18 10:42:21 +00:00
lim.limit = newLimit
2024-02-18 10:42:21 +00:00
}
// SetBurst is shorthand for SetBurstAt(time.Now(), newBurst).
2024-02-18 10:42:21 +00:00
func (lim *Limiter) SetBurst(newBurst int) {
2024-02-18 10:42:21 +00:00
lim.SetBurstAt(time.Now(), newBurst)
2024-02-18 10:42:21 +00:00
}
// SetBurstAt sets a new burst size for the limiter.
2024-02-18 10:42:21 +00:00
func (lim *Limiter) SetBurstAt(t time.Time, newBurst int) {
2024-02-18 10:42:21 +00:00
lim.mu.Lock()
2024-02-18 10:42:21 +00:00
defer lim.mu.Unlock()
t, tokens := lim.advance(t)
lim.last = t
2024-02-18 10:42:21 +00:00
lim.tokens = tokens
2024-02-18 10:42:21 +00:00
lim.burst = newBurst
2024-02-18 10:42:21 +00:00
}
// reserveN is a helper method for AllowN, ReserveN, and WaitN.
2024-02-18 10:42:21 +00:00
// maxFutureReserve specifies the maximum reservation wait duration allowed.
2024-02-18 10:42:21 +00:00
// reserveN returns Reservation, not *Reservation, to avoid allocation in AllowN and WaitN.
2024-02-18 10:42:21 +00:00
func (lim *Limiter) reserveN(t time.Time, n int, maxFutureReserve time.Duration) Reservation {
2024-02-18 10:42:21 +00:00
lim.mu.Lock()
2024-02-18 10:42:21 +00:00
defer lim.mu.Unlock()
if lim.limit == Inf {
2024-02-18 10:42:21 +00:00
return Reservation{
ok: true,
lim: lim,
tokens: n,
2024-02-18 10:42:21 +00:00
timeToAct: t,
}
2024-02-18 10:42:21 +00:00
} else if lim.limit == 0 {
2024-02-18 10:42:21 +00:00
var ok bool
2024-02-18 10:42:21 +00:00
if lim.burst >= n {
2024-02-18 10:42:21 +00:00
ok = true
2024-02-18 10:42:21 +00:00
lim.burst -= n
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return Reservation{
ok: ok,
lim: lim,
tokens: lim.burst,
2024-02-18 10:42:21 +00:00
timeToAct: t,
}
2024-02-18 10:42:21 +00:00
}
t, tokens := lim.advance(t)
// Calculate the remaining number of tokens resulting from the request.
2024-02-18 10:42:21 +00:00
tokens -= float64(n)
// Calculate the wait duration
2024-02-18 10:42:21 +00:00
var waitDuration time.Duration
2024-02-18 10:42:21 +00:00
if tokens < 0 {
2024-02-18 10:42:21 +00:00
waitDuration = lim.limit.durationFromTokens(-tokens)
2024-02-18 10:42:21 +00:00
}
// Decide result
2024-02-18 10:42:21 +00:00
ok := n <= lim.burst && waitDuration <= maxFutureReserve
// Prepare reservation
2024-02-18 10:42:21 +00:00
r := Reservation{
ok: ok,
lim: lim,
2024-02-18 10:42:21 +00:00
limit: lim.limit,
}
2024-02-18 10:42:21 +00:00
if ok {
2024-02-18 10:42:21 +00:00
r.tokens = n
2024-02-18 10:42:21 +00:00
r.timeToAct = t.Add(waitDuration)
// Update state
2024-02-18 10:42:21 +00:00
lim.last = t
2024-02-18 10:42:21 +00:00
lim.tokens = tokens
2024-02-18 10:42:21 +00:00
lim.lastEvent = r.timeToAct
2024-02-18 10:42:21 +00:00
}
return r
2024-02-18 10:42:21 +00:00
}
// advance calculates and returns an updated state for lim resulting from the passage of time.
2024-02-18 10:42:21 +00:00
// lim is not changed.
2024-02-18 10:42:21 +00:00
// advance requires that lim.mu is held.
2024-02-18 10:42:21 +00:00
func (lim *Limiter) advance(t time.Time) (newT time.Time, newTokens float64) {
2024-02-18 10:42:21 +00:00
last := lim.last
2024-02-18 10:42:21 +00:00
if t.Before(last) {
2024-02-18 10:42:21 +00:00
last = t
2024-02-18 10:42:21 +00:00
}
// Calculate the new number of tokens, due to time that passed.
2024-02-18 10:42:21 +00:00
elapsed := t.Sub(last)
2024-02-18 10:42:21 +00:00
delta := lim.limit.tokensFromDuration(elapsed)
2024-02-18 10:42:21 +00:00
tokens := lim.tokens + delta
2024-02-18 10:42:21 +00:00
if burst := float64(lim.burst); tokens > burst {
2024-02-18 10:42:21 +00:00
tokens = burst
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return t, tokens
2024-02-18 10:42:21 +00:00
}
// durationFromTokens is a unit conversion function from the number of tokens to the duration
2024-02-18 10:42:21 +00:00
// of time it takes to accumulate them at a rate of limit tokens per second.
2024-02-18 10:42:21 +00:00
func (limit Limit) durationFromTokens(tokens float64) time.Duration {
2024-02-18 10:42:21 +00:00
if limit <= 0 {
2024-02-18 10:42:21 +00:00
return InfDuration
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
seconds := tokens / float64(limit)
2024-02-18 10:42:21 +00:00
return time.Duration(float64(time.Second) * seconds)
2024-02-18 10:42:21 +00:00
}
// tokensFromDuration is a unit conversion function from a time duration to the number of tokens
2024-02-18 10:42:21 +00:00
// which could be accumulated during that duration at a rate of limit tokens per second.
2024-02-18 10:42:21 +00:00
func (limit Limit) tokensFromDuration(d time.Duration) float64 {
2024-02-18 10:42:21 +00:00
if limit <= 0 {
2024-02-18 10:42:21 +00:00
return 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return d.Seconds() * float64(limit)
2024-02-18 10:42:21 +00:00
}