Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rueian/redlimiter
A golang library based on go-redis connection pool that can be used to implement rate limit function
https://github.com/rueian/redlimiter
atomic golang race-conditions rate-limiter rate-limiting redis
Last synced: about 1 month ago
JSON representation
A golang library based on go-redis connection pool that can be used to implement rate limit function
- Host: GitHub
- URL: https://github.com/rueian/redlimiter
- Owner: rueian
- Created: 2019-04-14T17:58:16.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-04-23T03:30:14.000Z (over 5 years ago)
- Last Synced: 2024-10-14T07:11:47.658Z (3 months ago)
- Topics: atomic, golang, race-conditions, rate-limiter, rate-limiting, redis
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# redlimiter
[![CircleCI](https://circleci.com/gh/rueian/redlimiter.svg?style=svg)](https://circleci.com/gh/rueian/redlimiter)
[![codecov](https://codecov.io/gh/rueian/redlimiter/branch/master/graph/badge.svg)](https://codecov.io/gh/rueian/redlimiter)A golang library based on go-redis connection pool that can be used to implement rate limit function.
## Counter
The `counter` package provides 2 `Incrementer` implementations
that both `INCR` a key in redis and set expiration atomically when the key created without race condition.```go
package mainimport (
"github.com/go-redis/redis"
"github.com/rueian/redlimiter/counter"
)func main() {
client := redis.NewUniversalClient(&redis.UniversalOptions{Addrs: []string{"redis:6379"}})
incrementer := counter.NewLuaIncrementer(client) // or counter.NewTxIncrementer(client)
count, err := incrementer.Incr("client_ip", 1) // expire in 1 sec
if err != nil {
rejectWithErr(err)
}
if count > 50 {
rejectWithLimitExceeded()
}
}
```### LuaIncrementer
Executing a lua script which issues a `INCR` command and a conditional `EXPIRE` command on a key atomically on redis.
### TxIncrementer
Issuing `MULTI` `SET EX NX` `INCR` `EXEC` commands on a key atomically on redis.
### Benchmark
```bash
▶ docker-compose run --rm bench
Starting redlimiter_redis_1 ... done
goos: linux
goarch: amd64
pkg: github.com/rueian/redlimiter/counter
BenchmarkLuaIncrementer-6 5000 279119 ns/op
BenchmarkTxIncrementer-6 5000 307964 ns/op
PASS
ok github.com/rueian/redlimiter/counter 5.138s
```