1
0
Fork 0

Scope the rate limit counter key by source AND by middleware

This commit is contained in:
aromeyer 2025-05-23 15:38:04 +02:00 committed by GitHub
parent 76153acac6
commit ab3234e458
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -146,7 +146,12 @@ func (rl *rateLimiter) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
logger.Info().Msgf("ignoring token bucket amount > 1: %d", amount) logger.Info().Msgf("ignoring token bucket amount > 1: %d", amount)
} }
delay, err := rl.limiter.Allow(ctx, source) // Each rate limiter has its own source space,
// ensuring independence between rate limiters,
// i.e., rate limit rules are only applied based on traffic
// where the rate limiter is active.
rlSource := fmt.Sprintf("%s:%s", rl.name, source)
delay, err := rl.limiter.Allow(ctx, rlSource)
if err != nil { if err != nil {
rl.logger.Error().Err(err).Msg("Could not insert/update bucket") rl.logger.Error().Err(err).Msg("Could not insert/update bucket")
observability.SetStatusErrorf(ctx, "Could not insert/update bucket") observability.SetStatusErrorf(ctx, "Could not insert/update bucket")