go-ratelimit
is a Go library for rate limiting using various synchronization mechanisms. It provides different implementations of rate limiters using sync.Mutex
, sync.RWMutex
, and sync.Map
.
- All
limiter.Limiter
implementations follow theRatelimit
interface in domain.go which closely followsgolang.org/x/time/rate
interface as much as possible.
To install the library, use go get
:
go get -u github.com/yesyoukenspace/go-ratelimit
For usage examples checkout distributed_bench_test.go
Look at distributed_bench_test.go for examples.
- A low impact rate limiter backed by redis made for distributed systems that requires low latency but allows some inaccuracy in enforcing rate limit - code
- Uses
SyncMapLoadThenStore
to manage local states and synchronizes with redis asynchronously thus not blockingAllowN
functions - close to 1000x faster than go-redis/redis_rate
- go-redis reaches maximum output at 45000 rps on benchmark machine while RedisDelayedSync could go up to 44 million rps
This is just a wrapper around github.com/go-redis/redis_rate
, used primarily for testing and benchmarking.
- Mutex: Uses
sync.Mutex
. - RWMutex: Uses
sync.RWMutex
. - SyncMapLoadThenLoadOrStore: Uses
sync.Map
withLoad
thenLoadOrStore
. - SyncMapLoadOrStore: Uses
sync.Map
withLoadOrStore
. - SyncMapLoadThenStore: Uses
sync.Map
withLoad
thenStore
.
Benchmarking results at ./out/bench
Info of machine
> sysctl -a machdep.cpu
machdep.cpu.cores_per_package: 10
machdep.cpu.core_count: 10
machdep.cpu.logical_per_package: 10
machdep.cpu.thread_count: 10
machdep.cpu.brand_string: Apple M1 Pro
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Make sure to update tests as appropriate.
See the LICENSE file for details.
- @YesYouKenSpace