From d56a8d095e8fe84422ef4098d1e1a25198da17a1 Mon Sep 17 00:00:00 2001 From: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> Date: Sat, 16 Dec 2023 11:53:42 +0000 Subject: [performance] simpler throttling logic (#2407) * reduce complexity of throttling logic to use 1 queue and an atomic int * use atomic add instead of CAS, add throttling test --- internal/middleware/ratelimit.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'internal/middleware/ratelimit.go') diff --git a/internal/middleware/ratelimit.go b/internal/middleware/ratelimit.go index a59a3e608..57055fe70 100644 --- a/internal/middleware/ratelimit.go +++ b/internal/middleware/ratelimit.go @@ -29,6 +29,8 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/util" "github.com/ulule/limiter/v3" "github.com/ulule/limiter/v3/drivers/store/memory" + + apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" ) const rateLimitPeriod = 5 * time.Minute @@ -141,10 +143,12 @@ func RateLimit(limit int, exceptions []string) gin.HandlerFunc { if context.Reached { // Return JSON error message for // consistency with other endpoints. - c.AbortWithStatusJSON( + apiutil.Data(c, http.StatusTooManyRequests, - gin.H{"error": "rate limit reached"}, + apiutil.AppJSON, + apiutil.ErrorRateLimitReached, ) + c.Abort() return } -- cgit v1.2.3