Files
AetherForge/server/internal/api/agent_ws_limiter.go
AetherForge 415b5dc6a3
Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
Release validation: tests green, USB pack, fleet UX and API hardening.
Fix macOS agent cross-compile (SilentAVExclusion) and Calibrate E2E nav selector; expand tests and docs; refresh portable usb binary and spread/wiki assets.
2026-06-06 16:57:39 -07:00

45 lines
967 B
Go

package api
import (
"sync"
"time"
)
const (
agentWSRateLimitMax = 30
agentWSRateLimitWindow = time.Minute
agentWSAuthTimeout = 45 * time.Second
)
type agentWSRateLimiter struct {
mu sync.Mutex
attempts map[string][]time.Time
}
var agentWSRateLim = agentWSRateLimiter{attempts: make(map[string][]time.Time)}
func allowAgentWSUpgrade(clientIP string) bool {
if clientIP == "" {
return true
}
now := time.Now()
cutoff := now.Add(-agentWSRateLimitWindow)
agentWSRateLim.mu.Lock()
defer agentWSRateLim.mu.Unlock()
filtered := agentWSRateLim.attempts[clientIP][:0]
for _, t := range agentWSRateLim.attempts[clientIP] {
if t.After(cutoff) {
filtered = append(filtered, t)
}
}
if len(filtered) == 0 {
delete(agentWSRateLim.attempts, clientIP)
}
if len(filtered) >= agentWSRateLimitMax {
agentWSRateLim.attempts[clientIP] = filtered
return false
}
agentWSRateLim.attempts[clientIP] = append(filtered, now)
return true
}