feat: support redis rate limiting

This commit is contained in:
2026-01-17 12:04:55 +08:00
parent c399a65d83
commit 419877ffa8
4 changed files with 134 additions and 0 deletions

View File

@@ -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 != "" {