https://github.com/iv4n-ga6l/go-apiratelimiter
Api rate limiter built in Go
https://github.com/iv4n-ga6l/go-apiratelimiter
Last synced: 7 months ago
JSON representation
Api rate limiter built in Go
- Host: GitHub
- URL: https://github.com/iv4n-ga6l/go-apiratelimiter
- Owner: iv4n-ga6l
- Created: 2024-05-09T12:27:48.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-01T07:52:06.000Z (12 months ago)
- Last Synced: 2025-01-31T13:13:56.116Z (9 months ago)
- Language: Go
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Api rate limiter built in Go
This project demonstrates a simple API rate limiter implemented in Go using a token bucket algorithm. The rate limiter can be configured for both global limits and specific HTTP methods (GET, POST, etc.). It also includes real-time statistics displayed in the terminal and scripts to test the rate limiter by making requests to the endpoints.
Project Structure
- limiter/ratelimiter.go: Contains the implementation of the rate limiter and statistics display.
- api/main.go: Sets up the API server with rate-limited endpoints.
- get_requests.sh: Script to send 10 GET requests per second to the /data endpoint.
- post_requests.sh: Script to send 5 POST requests per second to the /submit endpoint.Implementation Details
- Rate Limiter :
The rate limiter is implemented using a token bucket algorithm. It allows configuring both global rate limits and method-specific rate limits. The RateLimiter struct holds the configuration and state for the rate limiter. The APIRegistry struct manages multiple RateLimiter instances for different APIs and methods.- Displaying Statistics :
The DisplayStats method of APIRegistry displays real-time statistics about incoming requests in a terminal table. This includes the total number of requests, allowed requests, denied requests, and the last update time for each API and method.- Setting Up the API Server :
The api/main.go file sets up an HTTP server with two endpoints: /data for GET requests and /submit for POST requests. The server uses the APIRegistry middleware to enforce rate limits on these endpoints.- Testing the Rate Limiter :
To test the rate limiter, two scripts (get_requests.sh and post_requests.sh) are provided. These scripts send requests to the server at specified rates, allowing you to observe the rate limiting in action and monitor the real-time statistics.Usage
-Prerequisites
- - Go 1.16 or later
- - curl command-line tool-
Setup
1. After cloning the repository, install dependencies
````
go mod tidy
go get
````
2. Run the API server
````
go run api/main.go
````
3. Run the test scripts
````
get_requests.sh
post_requests.sh
````