[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
This commit is contained in:
kim
2023-12-16 11:53:42 +00:00
committed by GitHub
parent 1312695c46
commit d56a8d095e
4 changed files with 208 additions and 40 deletions

View File

@ -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
}