From 2ae8eb8bc6524fc4296265b58d6e34a944438df9 Mon Sep 17 00:00:00 2001 From: luk3skyw4lker Date: Mon, 22 Jul 2024 16:58:38 -0300 Subject: [PATCH] refactor: rename MaxCalculator to MaxFunc --- middleware/limiter/config.go | 6 +++--- middleware/limiter/limiter_fixed.go | 2 +- middleware/limiter/limiter_sliding.go | 2 +- middleware/limiter/limiter_test.go | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/middleware/limiter/config.go b/middleware/limiter/config.go index e7ccaa7b13..b0befc0b22 100644 --- a/middleware/limiter/config.go +++ b/middleware/limiter/config.go @@ -23,7 +23,7 @@ type Config struct { // Default: func(c fiber.Ctx) int { // return c.Max // } - MaxCalculator func(c fiber.Ctx) int + MaxFunc func(c fiber.Ctx) int // KeyGenerator allows you to generate custom keys, by default c.IP() is used // @@ -109,8 +109,8 @@ func configDefault(config ...Config) Config { if cfg.LimiterMiddleware == nil { cfg.LimiterMiddleware = ConfigDefault.LimiterMiddleware } - if cfg.MaxCalculator == nil { - cfg.MaxCalculator = func(_ fiber.Ctx) int { + if cfg.MaxFunc == nil { + cfg.MaxFunc = func(_ fiber.Ctx) int { return cfg.Max } } diff --git a/middleware/limiter/limiter_fixed.go b/middleware/limiter/limiter_fixed.go index 23dd0ca974..42b08afaf6 100644 --- a/middleware/limiter/limiter_fixed.go +++ b/middleware/limiter/limiter_fixed.go @@ -27,7 +27,7 @@ func (FixedWindow) New(cfg Config) fiber.Handler { // Return new handler return func(c fiber.Ctx) error { // Generate max from generator, if no generator was provided the default value returned is 5 - max := cfg.MaxCalculator(c) + max := cfg.MaxFunc(c) // Don't execute middleware if Next returns true or if the max is 0 if (cfg.Next != nil && cfg.Next(c)) || max == 0 { diff --git a/middleware/limiter/limiter_sliding.go b/middleware/limiter/limiter_sliding.go index 7c57519d74..1fc3138b9b 100644 --- a/middleware/limiter/limiter_sliding.go +++ b/middleware/limiter/limiter_sliding.go @@ -28,7 +28,7 @@ func (SlidingWindow) New(cfg Config) fiber.Handler { // Return new handler return func(c fiber.Ctx) error { // Generate max from generator, if no generator was provided the default value returned is 5 - max := cfg.MaxCalculator(c) + max := cfg.MaxFunc(c) // Don't execute middleware if Next returns true or if the max is 0 if (cfg.Next != nil && cfg.Next(c)) || max == 0 { diff --git a/middleware/limiter/limiter_test.go b/middleware/limiter/limiter_test.go index 487d8d6af6..dd7b59a290 100644 --- a/middleware/limiter/limiter_test.go +++ b/middleware/limiter/limiter_test.go @@ -20,7 +20,7 @@ func Test_Limiter_With_Max_Calculator_With_Zero(t *testing.T) { app := fiber.New() app.Use(New(Config{ - MaxCalculator: func(_ fiber.Ctx) int { + MaxFunc: func(_ fiber.Ctx) int { return 0 }, Expiration: 2 * time.Second, @@ -61,7 +61,7 @@ func Test_Limiter_With_Max_Calculator(t *testing.T) { max := 10 app.Use(New(Config{ - MaxCalculator: func(_ fiber.Ctx) int { + MaxFunc: func(_ fiber.Ctx) int { return max }, Expiration: 2 * time.Second,