• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

internal/clock/H16-Mar-2018-

scripts/H16-Mar-2018-

.gitignoreH A D16-Mar-201819

LICENSEH A D16-Mar-20181.1 KiB

MakefileH A D16-Mar-201890

README.mdH A D16-Mar-2018834

glide.lockH A D16-Mar-2018341

glide.yamlH A D16-Mar-2018156

ratelimit.goH A D16-Mar-20184 KiB

ratelimit_test.goH A D16-Mar-20182.6 KiB

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