Documentation ¶
Overview ¶
Package ratelimiter implements function for helping rate limiting.
Example ¶
package main import ( "fmt" "github.com/polomsky/ratelimiter" "time" ) func main() { const MB = 1024 * 1024 limiter := ratelimiter.Limiter{ Capacity: 5 * MB, // 5MB burst Decrement: 1 * MB, // 1MB per interval - 1MB/s Interval: time.Second, } for i := 0; i < 6; i++ { fmt.Println(limiter.Add(MB)) } }
Output: true true true true true false
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Limiter ¶
type Limiter struct { // size of the max burst, must be greater than zero, or only Add zero will be allowed Capacity uint // number of units allowed in each interval, must be greater than zero or it's average output will be zero Decrement uint // interval in which decrement is subtracted from the current usage, Decrement/Interval defines average output Interval time.Duration // contains filtered or unexported fields }
Limiter represents rate limiter object. Supports Bursts up to max uint.
Click to show internal directories.
Click to hide internal directories.