From bdc55feeba3780d66d3f51af97b27de04cb654fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Fri, 11 Aug 2023 12:58:40 +0800 Subject: [PATCH] feat: remove RateLimiter --- http/limit/limit.go | 62 ---------------------------------------- http/rate_limiter.go | 29 ------------------- http/service_provider.go | 3 -- 3 files changed, 94 deletions(-) delete mode 100644 http/limit/limit.go delete mode 100644 http/rate_limiter.go diff --git a/http/limit/limit.go b/http/limit/limit.go deleted file mode 100644 index 6ad2cc149..000000000 --- a/http/limit/limit.go +++ /dev/null @@ -1,62 +0,0 @@ -package limit - -import ( - "github.com/goravel/framework/contracts/http" -) - -func PerMinute(maxAttempts int) http.Limit { - return NewLimit(maxAttempts, 1) -} - -func PerMinutes(decayMinutes, maxAttempts int) http.Limit { - return NewLimit(maxAttempts, decayMinutes) -} - -func PerHour(maxAttempts int) http.Limit { - return NewLimit(maxAttempts, 60) -} - -func PerHours(decayHours, maxAttempts int) http.Limit { - return NewLimit(maxAttempts, 60*decayHours) -} - -func PerDay(maxAttempts int) http.Limit { - return NewLimit(maxAttempts, 60*24) -} - -func PerDays(decayDays, maxAttempts int) http.Limit { - return NewLimit(maxAttempts, 60*24*decayDays) -} - -type Limit struct { - // The rate limit signature key. - Key string - // The maximum number of attempts allowed within the given number of minutes. - MaxAttempts int - // The number of minutes until the rate limit is reset. - DecayMinutes int - // The response generator callback. - ResponseCallback func(ctx http.Context) -} - -func NewLimit(maxAttempts, decayMinutes int) *Limit { - return &Limit{ - MaxAttempts: maxAttempts, - DecayMinutes: decayMinutes, - ResponseCallback: func(ctx http.Context) { - ctx.Request().AbortWithStatus(http.StatusTooManyRequests) - }, - } -} - -func (r *Limit) By(key string) http.Limit { - r.Key = key - - return r -} - -func (r *Limit) Response(callable func(ctx http.Context)) http.Limit { - r.ResponseCallback = callable - - return r -} diff --git a/http/rate_limiter.go b/http/rate_limiter.go deleted file mode 100644 index 1b3a40019..000000000 --- a/http/rate_limiter.go +++ /dev/null @@ -1,29 +0,0 @@ -package http - -import ( - "github.com/goravel/framework/contracts/http" -) - -type RateLimiter struct { - limiters map[string]func(ctx http.Context) []http.Limit -} - -func NewRateLimiter() *RateLimiter { - return &RateLimiter{ - limiters: make(map[string]func(ctx http.Context) []http.Limit), - } -} - -func (r *RateLimiter) For(name string, callback func(ctx http.Context) http.Limit) { - r.limiters[name] = func(ctx http.Context) []http.Limit { - return []http.Limit{callback(ctx)} - } -} - -func (r *RateLimiter) ForWithLimits(name string, callback func(ctx http.Context) []http.Limit) { - r.limiters[name] = callback -} - -func (r *RateLimiter) Limiter(name string) func(ctx http.Context) []http.Limit { - return r.limiters[name] -} diff --git a/http/service_provider.go b/http/service_provider.go index 3c21c45cd..4c3f16684 100644 --- a/http/service_provider.go +++ b/http/service_provider.go @@ -12,9 +12,6 @@ type ServiceProvider struct { } func (http *ServiceProvider) Register(app foundation.Application) { - app.Singleton(Binding, func(app foundation.Application) (any, error) { - return NewRateLimiter(), nil - }) } func (http *ServiceProvider) Boot(app foundation.Application) {