README.md
1# Go rate limiter
2
3This package provides a Golang implementation of the leaky-bucket rate limit algorithm.
4This implementation refills the bucket based on the time elapsed between
5requests instead of requiring an interval clock to fill the bucket discretely.
6
7Create a rate limiter with a maximum number of operations to perform per second.
8Call Take() before each operation. Take will sleep until you can continue.
9
10```go
11import (
12 "fmt"
13 "time"
14
15 "go.uber.org/ratelimit"
16)
17
18func main() {
19 rl := ratelimit.New(100) // per second
20
21 prev := time.Now()
22 for i := 0; i < 10; i++ {
23 now := rl.Take()
24 fmt.Println(i, now.Sub(prev))
25 prev = now
26 }
27
28 // Output:
29 // 0 0
30 // 1 10ms
31 // 2 10ms
32 // 3 10ms
33 // 4 10ms
34 // 5 10ms
35 // 6 10ms
36 // 7 10ms
37 // 8 10ms
38 // 9 10ms
39}
40```
41