mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Implement comment length and IP rate limiting in comment creation
This commit is contained in:
54
server/utils/request_limit.go
Normal file
54
server/utils/request_limit.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user