Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ofw/go-redis-ratelimiter
Simple thread-safe go ratelimiter library with redis backend
https://github.com/ofw/go-redis-ratelimiter
Last synced: 2 months ago
JSON representation
Simple thread-safe go ratelimiter library with redis backend
- Host: GitHub
- URL: https://github.com/ofw/go-redis-ratelimiter
- Owner: ofw
- License: mit
- Created: 2016-03-25T07:24:12.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-04-20T13:43:46.000Z (almost 9 years ago)
- Last Synced: 2024-06-20T14:26:16.851Z (7 months ago)
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-redis-ratelimiter
Simple go ratelimiter library with redis backend# Docs
https://godoc.org/github.com/oeegor/go-redis-ratelimiter
# Usage
```go
import (
"github.com/garyburd/redigo/redis"
"github.com/oeegor/go-redis-ratelimiter"
)func main() {
// initialize redis pool
redisPool := redis.NewPool(func() (redis.Conn, error) {
c, err := redis.Dial("tcp", "your-redis-address")
if err != nil {
return nil, err
}
return c, err
}, 100) // also set max connections to 100// increment rate limit usage for given key that is allowed 10 requests per second with one try
// Ratelimiter will try to acquire limit N tries using time.Sleep between tries
limitCtx, err := ratelimiter.Incr(redisPool, "mykey", 10, time.Second, 1)if err != nil {
// do something
}// limitCtx contains all necessary data for ratelimiter state
if limitCtx.Reached() {
// code to handle over the limit logic
}
}
```