fix: fixed the regression issue in rate limiter
All checks were successful
Build and Push to Gitea / build-and-push (push) Successful in 58s

This commit is contained in:
Hayden Hargreaves 2026-04-09 12:42:29 -07:00
parent 5c1b619aa8
commit 915988b24c

View File

@ -67,8 +67,6 @@ func RateLimit(next http.Handler) http.Handler {
ip := getClientIP(r) ip := getClientIP(r)
rateLimiter.mu.Lock() rateLimiter.mu.Lock()
defer rateLimiter.mu.Unlock()
now := timeNow() now := timeNow()
oneHourAgo := now.Add(-1 * time.Hour) oneHourAgo := now.Add(-1 * time.Hour)
@ -86,6 +84,7 @@ func RateLimit(next http.Handler) http.Handler {
// Check if rate limit exceeded // Check if rate limit exceeded
// Trace: SDD_LLD_0011 - Restrict to 10 calls per hour per IP // Trace: SDD_LLD_0011 - Restrict to 10 calls per hour per IP
if len(recentRequests) >= 10 { if len(recentRequests) >= 10 {
rateLimiter.mu.Unlock()
// Trace: SRD_UseCase_0005, SRD_UseCase_0006 - Rate limit error handling // Trace: SRD_UseCase_0005, SRD_UseCase_0006 - Rate limit error handling
// Trace: SRD_QualAssurReq_0006 - Return error when rate limit reached // Trace: SRD_QualAssurReq_0006 - Return error when rate limit reached
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
@ -97,6 +96,7 @@ func RateLimit(next http.Handler) http.Handler {
// Add current request to history // Add current request to history
recentRequests = append(recentRequests, now) recentRequests = append(recentRequests, now)
rateLimiter.timestamps[ip] = recentRequests rateLimiter.timestamps[ip] = recentRequests
rateLimiter.mu.Unlock()
// Allow request to proceed // Allow request to proceed
next.ServeHTTP(w, r) next.ServeHTTP(w, r)