Gin Middleware Limiter with momery store are not working
linuxeye opened this issue · comments
Justo commented
Using redis is normal, but using memory does not seem to take effect. The code is as follows:
func Limiter() gin.HandlerFunc {
store := memory.NewStore()
return func(c *gin.Context) {
// Examples:
//
// * 5 reqs/second: "5-S"
// * 10 reqs/minute: "10-M"
// * 1000 reqs/hour: "1000-H"
// * 2000 reqs/day: "2000-D"
//
rate, err := limiter.NewRateFromFormatted("1-H")
if err != nil {
global.Log.Error(err)
c.Abort()
return
}
// // Create a redis client.
// option := redis.NewClient(&redis.Options{
// Addr: "10.43.xx.xxx:6379",
// Password: "mY9tJxFe8vPxxxxxxxx",
// DB: 10,
// })
//
// // Create a store with the redis client.
// store, err := sredis.NewStore(option)
// if err != nil {
// global.Log.Error(err)
// return
// }
instance := limiter.New(store, rate)
opt := mgin.WithLimitReachedHandler(ExceededHandler)
f := mgin.NewMiddleware(instance, opt)
f(c)
}
}