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
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 allow some inaccuracy in enforcing rate limit - code
- Uses
SyncMapLoadThenStore
to manage local states and synchronizes with redis asynchronously thus not blockingAllowN
functions
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