Skip to content

Commit 48fa299

Browse files
committed
fix(ratelimit): actually handle if user isnt found
1 parent ce53228 commit 48fa299

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

internal/transport/http/ratelimit/ratelimit.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
package ratelimit
44

55
import (
6-
"log/slog"
7-
"net"
86
"net/http"
97
"sync"
108
"time"
@@ -54,10 +52,12 @@ func (r *rateLimiter) getVisitor(ip visitorIP) *rate.Limiter {
5452

5553
v, exists := r.visitors[ip]
5654
if !exists {
55+
limit := rate.NewLimiter(r.limit, r.burst)
5756
r.visitors[ip] = &visitor{
58-
limiter: rate.NewLimiter(1, 1),
57+
limiter: limit,
5958
lastSeen: time.Now(),
6059
}
60+
return limit
6161
}
6262

6363
v.lastSeen = time.Now()
@@ -97,17 +97,13 @@ func MiddlewareWithConfig(c Config) gin.HandlerFunc {
9797
go lmt.cleanupVisitors()
9898

9999
return func(c *gin.Context) {
100-
ip, _, err := net.SplitHostPort(c.Request.RemoteAddr)
101-
if err != nil {
102-
slog.Error("splitting host and port", "err", err)
103-
c.AbortWithStatusJSON(
104-
http.StatusInternalServerError,
105-
gin.H{"message": "internal server error"},
106-
)
100+
visitor := lmt.getVisitor(visitorIP(c.ClientIP()))
101+
if visitor == nil {
102+
c.AbortWithStatus(http.StatusInternalServerError)
107103
return
108104
}
109105

110-
if !lmt.getVisitor(visitorIP(ip)).Allow() {
106+
if !visitor.Allow() {
111107
c.AbortWithStatus(http.StatusTooManyRequests)
112108
return
113109
}

0 commit comments

Comments
 (0)