feat: support redis rate limiting
This commit is contained in:
@@ -153,10 +153,21 @@ func Provide(opts ...opt.Option) error {
|
||||
}
|
||||
message := strings.TrimSpace(config.RateLimit.Message)
|
||||
|
||||
var limiterStorage fiber.Storage
|
||||
if config.RateLimit.Redis != nil {
|
||||
storage, err := newRedisLimiterStorage(config.RateLimit.Redis)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
limiterStorage = storage
|
||||
container.AddCloseAble(func() { _ = storage.Close() })
|
||||
}
|
||||
|
||||
skipPrefixes := append([]string{"/healthz", "/readyz"}, config.RateLimit.SkipPaths...)
|
||||
engine.Use(limiter.New(limiter.Config{
|
||||
Max: max,
|
||||
Expiration: time.Duration(windowSeconds) * time.Second,
|
||||
Storage: limiterStorage,
|
||||
LimitReached: func(c fiber.Ctx) error {
|
||||
appErr := errorx.ErrRateLimitExceeded
|
||||
if message != "" {
|
||||
|
||||
Reference in New Issue
Block a user