Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/go-redis/redis_rate
Rate limiting for go-redis
https://github.com/go-redis/redis_rate
gcra leaky-bucket rate-limiting redis
Last synced: 5 days ago
JSON representation
Rate limiting for go-redis
- Host: GitHub
- URL: https://github.com/go-redis/redis_rate
- Owner: go-redis
- License: bsd-2-clause
- Created: 2015-12-15T09:29:52.000Z (almost 9 years ago)
- Default Branch: v10
- Last Pushed: 2024-10-17T12:42:54.000Z (24 days ago)
- Last Synced: 2024-10-23T15:53:02.170Z (18 days ago)
- Topics: gcra, leaky-bucket, rate-limiting, redis
- Language: Go
- Homepage: https://redis.uptrace.dev/guide/go-redis-rate-limiting.html
- Size: 79.1 KB
- Stars: 837
- Watchers: 13
- Forks: 101
- Open Issues: 37
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Rate limiting for go-redis
[![Build Status](https://travis-ci.org/go-redis/redis_rate.svg?branch=master)](https://travis-ci.org/go-redis/redis_rate)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/redis/go-redis/v8)](https://pkg.go.dev/github.com/go-redis/redis_rate/v9)> :heart: [**Uptrace.dev** - distributed traces, logs, and errors in one place](https://uptrace.dev)
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.## Installation
redis_rate supports 2 last Go versions and requires a Go version with
[modules](https://github.com/golang/go/wiki/Modules) support. So make sure to initialize a Go
module:```shell
go mod init github.com/my/repo
```And then install redis*rate/v10 (note \*\*\_v10*\*\* in the import; omitting it is a popular
mistake):```shell
go get github.com/go-redis/redis_rate/v10
```## Example
```go
package redis_rate_testimport (
"context"
"fmt""github.com/redis/go-redis/v9"
"github.com/go-redis/redis_rate/v10"
)func ExampleNewLimiter() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
_ = rdb.FlushDB(ctx).Err()limiter := redis_rate.NewLimiter(rdb)
res, err := limiter.Allow(ctx, "project:123", redis_rate.PerSecond(10))
if err != nil {
panic(err)
}
fmt.Println("allowed", res.Allowed, "remaining", res.Remaining)
// Output: allowed 1 remaining 9
}
```