GinRateLimit 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 new so there are no third party stores yet, so I would appreciate if someone could make one.
Install
```shell go get github.com/JGLTechnologies/GinRateLimit
Redis Example
```go
package main
import (
"github.com/JGLTechnologies/GinRateLimit"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"time"
)
func keyFunc(c *gin.Context) string {
return c.ClientIP()
}
func errorHandler(c *gin.Context, remaining time.Duration) {
c.String(429, "Too many requests. Try again in "+remaining.String())
}
func main() {
server := gin.Default()
// This makes it so each ip can only make 5 requests per second
// The redis store is still being tested. If you experience any bugs, report it on our GitHub or make a pull request to fix it.
store := GinRateLimit.RedisStore(time.Second, 5, redis.NewClient(&redis.Options{
ReadTimeout: time.Second,
WriteTimeout: time.Second,
IdleTimeout: time.Second * 5,
}))
mw := GinRateLimit.RateLimiter(keyFunc, errorHandler, store)
server.GET("/", mw, func(c *gin.Context) {
c.String(200, "Hello World")
})
server.Run(":8080")
}
Basic Setup
package main
import (
"github.com/gin-gonic/gin"
"github.com/JGLTechnologies/GinRateLimit"
"time"
)
func keyFunc(c *gin.Context) string {
return c.ClientIP()
}
func errorHandler(c *gin.Context, remaining time.Duration) {
c.String(429, "Too many requests. Try again in "+remaining.String())
}
func main() {
server := gin.Default()
// This makes it so each ip can only make 5 requests per second
store := GinRateLimit.InMemoryStore(time.Second, 5)
mw := GinRateLimit.RateLimiter(keyFunc, errorHandler, store)
server.GET("/", mw, func(c *gin.Context) {
c.String(200, "Hello World")
})
server.Run(":8080")
}
Custom Store Example
package main
import "time"
type CustomStore struct {
}
// Your store must have a method called Limit that takes a key and returns a bool
func (s *CustomStore) Limit(key string) (bool, time.Duration) {
// Do your rate limit logic, and return true if the user went over the rate limit, otherwise return false
// Return the amount of time the client needs to wait to make a new request
if UserWentOverLimit {
return true
}
return false
}
—
$ claude mcp add gin-rate-limit \
-- python -m otcore.mcp_server <graph>