Documentation ¶
Overview ¶
Package ratelimit provides a verry simple, token bucket based request rate limiter.
Index ¶
- type Limiter
- func (l *Limiter) Allow() bool
- func (l *Limiter) AllowN(n int) bool
- func (l *Limiter) Burst() int
- func (l *Limiter) Limit() time.Duration
- func (l *Limiter) Reserve() (bool, Reservation)
- func (l *Limiter) ReserveN(n int) (bool, Reservation)
- func (l *Limiter) Reset()
- func (l *Limiter) SetBurst(newB int)
- func (l *Limiter) SetLimit(newL time.Duration)
- func (l *Limiter) Tokens() int
- type Reservation
- type ResetTime
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
A Limiter controls how frequently accesses should be allowed to happen. It implements the principle of the token bucket, which defines a bucket with an exact size of tokens. Also, a rate is defined after exactly 1 token will be added to the buckets volume, if the bucket is not "full" (nVolume == nSize). The amount of tokens in the bucket are defined as ability to perform an action, which then reduces the volume of the bucket by n tickets.
func NewLimiter ¶
NewLimiter returns a new instance of Limiter with a burst rate of b and a limit time of l until a new token will be generated.
func (*Limiter) AllowN ¶
AllowN is shorthand for reserveN(n) but only returning a boolean which exposes the succeed of the reservation.
func (*Limiter) Burst ¶
Burst returns the defined burst value.
This function does not consume tokens.
func (*Limiter) Limit ¶
Limit returns the defined limit duration after which a new token will be generated.
This function does not consume tokens.
func (*Limiter) Reserve ¶
func (l *Limiter) Reserve() (bool, Reservation)
Reserve is shorthand for ReserveN(1).
func (*Limiter) ReserveN ¶
func (l *Limiter) ReserveN(n int) (bool, Reservation)
ReserveN checks if an amount of n tickets are currently available. If this is the case, true will be returned with a Reservation object as status information of the Limiter and n tokens will be consumed. If there are not enough tokens available to satisfy the reservation, false will be returned with a Reservation object containing the Limiters status containing the time until next token generation.
func (*Limiter) Reset ¶
func (l *Limiter) Reset()
Reset sets the state of the limiter to the initial state with b tokens available and last set to 0.
func (*Limiter) SetBurst ¶
SetBurst sets a new value for the rate limiters burst value withour resetting the state of the limiter.
func (*Limiter) SetLimit ¶
SetLimit sets a new value for the rate limiters limit duration withour resetting the state of the limiter.
func (*Limiter) Tokens ¶
Tokens returns the current available tokens. This value is unequal to the actual value of tokens, because this value is only refreshed after token consumption. So the returned value is the actial value of tokens plus the calculated amount of tokens which are virtually generated after last consumption.
This function does not consume tokens.
type Reservation ¶
type Reservation struct { Burst int `json:"burst"` Remaining int `json:"remaining"` Reset ResetTime `json:"reset"` }
Reservation contains the pre-defined burst rate of the Limiter, the amount of remaining tickets and the time until a new token will be added to the bucket if Remaining == 0. Else, reset will be time.Time{} (0001-01-01 00:00:00 +0000 UTC).
This struct contains JSON tags, so it can be easily parsed to JSON.
type ResetTime ¶
ResetTime inherits from time.Time and contains an extra-vaule 'isNil', which identifies if Time == time.Time{}. Also, the functions Unix, UnixNano and Format are overwritten to take notice of this extra-value.
func (*ResetTime) Format ¶
Format overwrites time.Time#Format() so it will return the value of def (if not defined it will return "") if IsNil is true. Else, it will behave like default.
func (*ResetTime) IsNil ¶
IsNil returns the boolean value if the inner Time value is equal an empty time object (time.Time{}).