This is my own implementation of an API rate-limiter middleware in Go using different algorithms. Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-rate-limiter/
- Token Bucket
- Fixed Window
- Sliding Window Log
- Sliding Window Counter
- Sliding Window Counter across multiple servers using Redis
make build
make install
go-rate-limiter <algorithm> --flag1 --flag2
make test
I am using the loadtest
package to run stress tests against the API server directly from the CLI. This can also be achieved by installing Postman
or .
- Install loadtest package using NPM
npm install -g loadtest
- Run the server
go-rate-limiter tokenBucket --capacity 20 --refillRate 1
- Run the tests
loadtest -c <number of concurrent users> --cores <number of cpu cores> --rps <target request per second by client> -k -n <number of requests to perform> http://localhost:8080/limited
You can run Redis together with multiple instances of the app to test the Sliding Window Counter algorithm across multiple servers:
make run-docker
- API Servers will be running on port 8085 and 8086.
-
Gin: Fast Web/API web server
-
Cobra: CLI: Used to create CLI commands
-
Go-redis/v9: Read/write data to Redis
-
Testify: Testing utilities, easy assertions, mocking, etc