https://github.com/jgltechnologies/gin-rate-limit
A rate limiter for the gin web framework
https://github.com/jgltechnologies/gin-rate-limit
gin go-library golang rate-limiter rate-limiting
Last synced: 6 months ago
JSON representation
A rate limiter for the gin web framework
- Host: GitHub
- URL: https://github.com/jgltechnologies/gin-rate-limit
- Owner: JGLTechnologies
- License: mit
- Created: 2021-12-29T04:53:30.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-06-28T06:58:37.000Z (over 1 year ago)
- Last Synced: 2025-04-09T13:07:46.211Z (6 months ago)
- Topics: gin, go-library, golang, rate-limiter, rate-limiting
- Language: Go
- Homepage:
- Size: 63.5 KB
- Stars: 97
- Watchers: 1
- Forks: 13
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gin-rate-limit
gin-rate-limit is a rate limiter for the gin framework. By default, it
can only store rate limit info in memory and with redis. If you want to store it somewhere else you can make your own
store or use third party stores. The library is relatively new so there are no third party stores yet.
Contributions would be appreciated.Install
```shell
go get github.com/JGLTechnologies/gin-rate-limit
```
Redis Example
```go
package mainimport (
"github.com/JGLTechnologies/gin-rate-limit"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
"time"
)func keyFunc(c *gin.Context) string {
return c.ClientIP()
}func errorHandler(c *gin.Context, info ratelimit.Info) {
c.String(429, "Too many requests. Try again in "+time.Until(info.ResetTime).String())
}func main() {
server := gin.Default()
// This makes it so each ip can only make 5 requests per second
store := ratelimit.RedisStore(&ratelimit.RedisOptions{
RedisClient: redis.NewClient(&redis.Options{
Addr: "localhost:7680",
}),
Rate: time.Second,
Limit: 5,
})
mw := ratelimit.RateLimiter(store, &ratelimit.Options{
ErrorHandler: errorHandler,
KeyFunc: keyFunc,
})
server.GET("/", mw, func(c *gin.Context) {
c.String(200, "Hello World")
})
server.Run(":8080")
}
```
Basic Setup
```go
package mainimport (
"github.com/gin-gonic/gin"
"github.com/JGLTechnologies/gin-rate-limit"
"time"
)func keyFunc(c *gin.Context) string {
return c.ClientIP()
}func errorHandler(c *gin.Context, info ratelimit.Info) {
c.String(429, "Too many requests. Try again in "+time.Until(info.ResetTime).String())
}func main() {
server := gin.Default()
// This makes it so each ip can only make 5 requests per second
store := ratelimit.InMemoryStore(&ratelimit.InMemoryOptions{
Rate: time.Second,
Limit: 5,
})
mw := ratelimit.RateLimiter(store, &ratelimit.Options{
ErrorHandler: errorHandler,
KeyFunc: keyFunc,
})
server.GET("/", mw, func(c *gin.Context) {
c.String(200, "Hello World")
})
server.Run(":8080")
}
```
Custom Store Example
```go
package mainimport (
"github.com/JGLTechnologies/gin-rate-limit"
"github.com/gin-gonic/gin"
)type CustomStore struct {
}// Your store must have a method called Limit that takes a key, *gin.Context and returns ratelimit.Info
func (s *CustomStore) Limit(key string, c *gin.Context) Info {
if UserWentOverLimit {
return Info{
Limit: 100,
RateLimited: true,
ResetTime: reset,
RemainingHits: 0,
}
}
return Info{
Limit: 100,
RateLimited: false,
ResetTime: reset,
RemainingHits: remaining,
}
}
```