|
1 | 1 | # Rate limiting for go-redis
|
2 | 2 |
|
3 | 3 | [](https://travis-ci.org/go-redis/redis_rate)
|
4 |
| -[](https://godoc.org/github.com/go-redis/redis_rate) |
| 4 | +[](https://pkg.go.dev/github.com/go-redis/redis_rate/v9) |
5 | 5 |
|
6 |
| -This package is based on [rwz/redis-gcra](https://github.com/rwz/redis-gcra) and implements [GCRA](https://en.wikipedia.org/wiki/Generic_cell_rate_algorithm) (aka leaky bucket) for rate limiting based on Redis. The code requires Redis version 3.2 or newer since it relies on [replicate_commands](https://redis.io/commands/eval#replicating-commands-instead-of-scripts) feature. |
| 6 | +This package is based on [rwz/redis-gcra](https://github.com/rwz/redis-gcra) and implements |
| 7 | +[GCRA](https://en.wikipedia.org/wiki/Generic_cell_rate_algorithm) (aka leaky bucket) for rate |
| 8 | +limiting based on Redis. The code requires Redis version 3.2 or newer since it relies on |
| 9 | +[replicate_commands](https://redis.io/commands/eval#replicating-commands-instead-of-scripts) |
| 10 | +feature. |
7 | 11 |
|
8 | 12 | ## Installation
|
9 | 13 |
|
10 |
| -redis_rate requires a Go version with [Modules](https://github.com/golang/go/wiki/Modules) support and uses import versioning. So please make sure to initialize a Go module before installing redis_rate: |
| 14 | +redis_rate requires a Go version with [Modules](https://github.com/golang/go/wiki/Modules) support |
| 15 | +and uses import versioning. So please make sure to initialize a Go module before installing |
| 16 | +redis_rate: |
11 | 17 |
|
12 |
| -``` shell |
| 18 | +```shell |
13 | 19 | go mod init github.com/my/repo
|
14 | 20 | go get github.com/go-redis/redis_rate/v8
|
15 | 21 | ```
|
16 | 22 |
|
17 | 23 | Import:
|
18 | 24 |
|
19 |
| -``` go |
| 25 | +```go |
20 | 26 | import "github.com/go-redis/redis_rate/v8"
|
21 | 27 | ```
|
22 | 28 |
|
23 | 29 | ## Example
|
24 | 30 |
|
25 |
| -``` go |
| 31 | +```go |
26 | 32 | package redis_rate_test
|
27 | 33 |
|
28 | 34 | import (
|
| 35 | + "context" |
29 | 36 | "fmt"
|
30 | 37 |
|
31 |
| - "github.com/go-redis/redis/v7" |
32 |
| - "github.com/go-redis/redis_rate/v8" |
| 38 | + "github.com/go-redis/redis/v8" |
| 39 | + "github.com/go-redis/redis_rate/v9" |
33 | 40 | )
|
34 | 41 |
|
35 | 42 | func ExampleNewLimiter() {
|
| 43 | + ctx := context.Background() |
36 | 44 | rdb := redis.NewClient(&redis.Options{
|
37 | 45 | Addr: "localhost:6379",
|
38 | 46 | })
|
39 |
| - _ = rdb.FlushDB().Err() |
| 47 | + _ = rdb.FlushDB(ctx).Err() |
40 | 48 |
|
41 | 49 | limiter := redis_rate.NewLimiter(rdb)
|
42 |
| - res, err := limiter.Allow("project:123", redis_rate.PerSecond(10)) |
| 50 | + res, err := limiter.Allow(ctx, "project:123", redis_rate.PerSecond(10)) |
43 | 51 | if err != nil {
|
44 | 52 | panic(err)
|
45 | 53 | }
|
46 |
| - fmt.Println(res.Allowed, res.Remaining) |
47 |
| - // Output: true 9 |
| 54 | + fmt.Println("allowed", res.Allowed, "remaining", res.Remaining) |
| 55 | + // Output: allowed 1 remaining 9 |
48 | 56 | }
|
49 | 57 | ```
|
0 commit comments