Implement comment length and IP rate limiting in comment creation

This commit is contained in:
2025-06-24 12:39:51 +08:00
parent 953b1cf86a
commit 3694e24aad
4 changed files with 85 additions and 33 deletions

View File

@@ -0,0 +1,54 @@
package utils
import (
"sync"
"time"
)
type RequestLimiter struct {
limit int
requestsByIP map[string]int
mu sync.Mutex
}
func NewRequestLimiter(limit int, duration time.Duration) *RequestLimiter {
l := &RequestLimiter{
limit: limit,
requestsByIP: make(map[string]int),
}
if duration > 0 {
go func() {
for {
time.Sleep(duration)
l.resetCounts()
}
}()
}
return l
}
func (rl *RequestLimiter) AllowRequest(ip string) bool {
rl.mu.Lock()
defer rl.mu.Unlock()
count, exists := rl.requestsByIP[ip]
if !exists {
count = 0
}
if count >= rl.limit {
return false // Exceeded request limit for this IP
}
rl.requestsByIP[ip] = count + 1
return true // Request allowed
}
func (rl *RequestLimiter) resetCounts() {
rl.mu.Lock()
defer rl.mu.Unlock()
rl.requestsByIP = make(map[string]int) // Reset all counts
}