From f3149d540bade35e3a1501354113097b2c9644f0 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sun, 23 Jan 2022 17:13:51 +0100 Subject: [PATCH] Add rate limiting allowed burst field to settings --- main.go | 2 +- settings/settings.go | 22 +++++++++++++++------- settings/settings_test.go | 5 ++++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 281005a..0191c1b 100644 --- a/main.go +++ b/main.go @@ -61,7 +61,7 @@ func main() { pipes := pipelines.LoadPipelines() authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Token) - rateLimiterMiddleware, err := middlewares.CreateRateLimiterMiddleware(appSettings.RequestsPerMinute, 0) + rateLimiterMiddleware, err := middlewares.CreateRateLimiterMiddleware(appSettings.RateLimiter.RequestsPerMinute, appSettings.RateLimiter.AllowedBurst) if err != nil { panic(err) } diff --git a/settings/settings.go b/settings/settings.go index 760b8b6..6e33efe 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -15,10 +15,10 @@ const ( type FileSystemType int type Settings struct { - Endpoint string `json:"endpoint"` - Token string `json:"token"` - RequestsPerMinute int `json:"requests_per_minute"` - StorageProvider StorageSettings `json:"storage_provider"` + Endpoint string `json:"endpoint"` + Token string `json:"token"` + RateLimiter RateLimiterSettings `json:"rate_limiter"` + StorageProvider StorageSettings `json:"storage_provider"` } type StorageSettings struct { @@ -26,6 +26,11 @@ type StorageSettings struct { BasePath string `json:"base_path"` } +type RateLimiterSettings struct { + RequestsPerMinute int `json:"requests_per_minute"` + AllowedBurst int `json:"allowed_burst"` +} + func parseSettings(data []byte) Settings { settings := Settings{} @@ -50,9 +55,12 @@ func LoadSettings(fileSystem afero.Fs) (Settings, error) { // If file does not exist, create default settings defaultSettings := Settings{ - Endpoint: "127.0.0.1:8000", - Token: "changeme", - RequestsPerMinute: 20, + Endpoint: "127.0.0.1:8000", + Token: "changeme", + RateLimiter: RateLimiterSettings{ + RequestsPerMinute: 20, + AllowedBurst: 5, + }, StorageProvider: StorageSettings{ Type: Local, BasePath: "assets", diff --git a/settings/settings_test.go b/settings/settings_test.go index 3621955..fa801ff 100644 --- a/settings/settings_test.go +++ b/settings/settings_test.go @@ -13,7 +13,10 @@ func TestSettingsParsing(t *testing.T) { const file string = `{ "endpoint": "0.0.0.0:8000", "token": "foobar", - "requests_per_minute": 20, + "rate_limiter": { + "requests_per_minute": 20, + "allowed_burst": 5 + }, "storage_provider": { "type": 0, "base_path": "assets"