Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions beamsync/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type EventCallback func(eventName string, data string)
//go:embed ui/*.html ui/*.png
var uiFS embed.FS

const rateLimitPruneInterval = 2 * time.Minute

// serverState holds per-instance connection tracking (no more package-level globals).
type serverState struct {
mu sync.Mutex
Expand Down Expand Up @@ -98,13 +100,30 @@ type clientRateLimiter struct {
}

func newClientRateLimiter(limit int, window time.Duration) *clientRateLimiter {
return &clientRateLimiter{
limiter := &clientRateLimiter{
limit: limit,
window: window,
maxClients: 4096,
clients: make(map[string]*rateLimitState),
now: time.Now,
}
limiter.startBackgroundPruner()
return limiter
}

func (l *clientRateLimiter) startBackgroundPruner() {
if l.limit <= 0 || l.window <= 0 {
return
}

go func() {
ticker := time.NewTicker(rateLimitPruneInterval)
defer ticker.Stop()

for range ticker.C {
l.prune(l.now())
}
}()
}

func (l *clientRateLimiter) allow(client string) rateLimitDecision {
Expand All @@ -117,8 +136,6 @@ func (l *clientRateLimiter) allow(client string) rateLimitDecision {
l.mu.Lock()
defer l.mu.Unlock()

l.prune(now)

state, ok := l.clients[client]
if !ok || now.Sub(state.windowStart) >= l.window {
if !ok {
Expand Down Expand Up @@ -159,6 +176,9 @@ func (l *clientRateLimiter) allow(client string) rateLimitDecision {
}

func (l *clientRateLimiter) prune(now time.Time) {
l.mu.Lock()
defer l.mu.Unlock()

for client, state := range l.clients {
if now.Sub(state.lastSeen) > 2*l.window {
delete(l.clients, client)
Expand Down
Loading