Move request limiter to middleware.

This commit is contained in:
2025-07-09 17:00:39 +08:00
parent 0021a73951
commit b568b234c4
11 changed files with 73 additions and 71 deletions

View File

@@ -6,12 +6,12 @@ import (
)
type RequestLimiter struct {
limit int
limit func() int
requestsByIP map[string]int
mu sync.Mutex
}
func NewRequestLimiter(limit int, duration time.Duration) *RequestLimiter {
func NewRequestLimiter(limit func() int, duration time.Duration) *RequestLimiter {
l := &RequestLimiter{
limit: limit,
requestsByIP: make(map[string]int),
@@ -38,7 +38,7 @@ func (rl *RequestLimiter) AllowRequest(ip string) bool {
count = 0
}
if count >= rl.limit {
if count >= rl.limit() {
return false // Exceeded request limit for this IP
}