Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nicolas-ggd/rate-limiter
Rate Limiter middleware in Golang using the Gin based on Redis
https://github.com/nicolas-ggd/rate-limiter
gin-gonic go go-rate-limiter golang middleware rate-limiter rate-limiting redis token-bu token-bucket-algorithm
Last synced: about 1 month ago
JSON representation
Rate Limiter middleware in Golang using the Gin based on Redis
- Host: GitHub
- URL: https://github.com/nicolas-ggd/rate-limiter
- Owner: Nicolas-ggd
- License: mit
- Created: 2024-06-08T15:39:28.000Z (5 months ago)
- Default Branch: master
- Last Pushed: 2024-08-28T16:25:07.000Z (3 months ago)
- Last Synced: 2024-10-15T17:22:45.830Z (about 1 month ago)
- Topics: gin-gonic, go, go-rate-limiter, golang, middleware, rate-limiter, rate-limiting, redis, token-bu, token-bucket-algorithm
- Language: Go
- Homepage:
- Size: 44.9 KB
- Stars: 14
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Rate Limiter
This project showcases an implementation of a rate limiter middleware in Golang using Redis. The rate limiter restricts the number of requests a user can make to an API within a defined timeframe, helping to manage and control API usage effectively.## Features
- Easy configuration - During configuration phase, you can decide how many token is used in per request, you can configure maximum quantity of tokens, token refill time. However, you can decide to hash redis key or not.
- Redis Integration - Utilizes Redis for efficient storage and retrieval of rate limiting data, ensuring scalability and performance.
- Understanding Response - Defined response is showed when the rate limit is exceeded.## Installation
```shell
go get github.com/Nicolas-ggd/rate-limiter
```## Usage
Redis-based Rate Limiter1. Implement the Redis-based rate limiter middleware as shown:
```go
package mainimport (
"net/http"
"time""github.com/Nicolas-ggd/rate-limiter"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
)func main() {
r := gin.Default()client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})// Call NewRateLimiter function from rrl package.
limiter, err := rrl.NewRateLimiter(&rrl.RateLimiter{
Rate: 1, // amount of each request as a token
MaxTokens: 5, // maximum token quantity for requests
RefillInterval: 15 * time.Second, // each token fill in 'X' time frame
Client: client, // redis client
HashKey: false, // make true if you want to hash redis key
})
if err != nil {
log.Fatal(err)
}// Use RateLimiterMiddleware from rrl package and pass limiter.
// This middleware works for all routes in your application,
// including static files served when you open a web browser.
r.Use(rrl.RateLimiterMiddleware(limiter))r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Welcome!"})
})// Using this way allows the RateLimiterMiddleware to work for only specific routes.
r.GET("/some", rrl.RateLimiterMiddleware(limiter), func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Some!"})
})r.Run(":8080")
}```
## License
This project is licensed under the MIT License.## Contributing
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes ;).