Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVladimir Shushlin <v.shushlin@gmail.com>2021-12-09 17:17:17 +0300
committerVladimir Shushlin <v.shushlin@gmail.com>2021-12-20 14:10:35 +0300
commit91dd7bc9011640dd02b497acf9fa78bee35a8402 (patch)
treea42e4d7ca3b2d169fc8454952ff90e2075589481 /internal/handlers
parentf8512edbec4ec83b426c8ca2dda467de424685e4 (diff)
refactor: handle defaults in ratelimiter package itself
also fix tests: * float64(1/time.Milesecond) == 0 * rate package doesn't actually refill the bucket on fractional seconds, so we need to use integers
Diffstat (limited to 'internal/handlers')
-rw-r--r--internal/handlers/ratelimiter.go10
1 files changed, 3 insertions, 7 deletions
diff --git a/internal/handlers/ratelimiter.go b/internal/handlers/ratelimiter.go
index 9c66c15d..8263f497 100644
--- a/internal/handlers/ratelimiter.go
+++ b/internal/handlers/ratelimiter.go
@@ -10,19 +10,15 @@ import (
// Ratelimiter configures the ratelimiter middleware
// TODO: make this unexported once https://gitlab.com/gitlab-org/gitlab-pages/-/issues/670 is done
-func Ratelimiter(handler http.Handler, config *config.Config) http.Handler {
- if config.RateLimit.SourceIPLimitPerSecond == 0 {
- return handler
- }
-
+func Ratelimiter(handler http.Handler, config *config.RateLimit) http.Handler {
rl := ratelimiter.New(
"source_ip",
ratelimiter.WithCacheMaxSize(ratelimiter.DefaultSourceIPCacheSize),
ratelimiter.WithCachedEntriesMetric(metrics.RateLimitSourceIPCachedEntries),
ratelimiter.WithCachedRequestsMetric(metrics.RateLimitSourceIPCacheRequests),
ratelimiter.WithBlockedCountMetric(metrics.RateLimitSourceIPBlockedCount),
- ratelimiter.WithLimitPerSecond(config.RateLimit.SourceIPLimitPerSecond),
- ratelimiter.WithBurstSize(config.RateLimit.SourceIPBurst),
+ ratelimiter.WithLimitPerSecond(config.SourceIPLimitPerSecond),
+ ratelimiter.WithBurstSize(config.SourceIPBurst),
)
return rl.Middleware(handler)