Files
stpweb/backend/services/ratelimit.go
T
adamfandClaude Opus 5.5 daa2a465fb
Test and deploy / test (push) Successful in 3m30s
Test and deploy / deploy (push) Has been cancelled
Cut the edge over from nginx and certbot to Caddy
Caddy takes nginx's place in compose: ports 80/443, the fixed address
172.28.255.251 that cloudflared pins the public names to, the network
aliases, the healthcheck and the autoheal label. nginx and certbot are
removed from compose; Caddy already holds its own certificates in
./caddy/data, obtained while it ran beside nginx. `traffic` now tails
logs/caddy/payloads.log, and the dev override switches Caddy to its dev
sites instead of putting nginx in DEV_MODE.

Docs and comments that described nginx's behaviour now describe Caddy's:
CLAUDE.md, DEPLOYMENT.md (topology, boot paths, TLS, rate limits, runbook
with a rollback, rough edges), the READMEs, .env.example and the comments
in the backend, Python service, Vue source and infra files. nginx/ and
certbot/ stay in the repo as the rollback until this has settled.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-17 23:28:28 +00:00

68 lines
2.3 KiB
Go

// Package services holds the backend's stateful collaborators: the database
// handle, JWT signing, the chat WebSocket hub, third-party API clients
// (Spotify, Steam, Gitea, Claude) and the email-sync pipeline. Handlers and
// GraphQL resolvers reach them through handlers.Store.
package services
import (
"sync"
"time"
)
// RateLimiter is an in-memory sliding-window limiter keyed by an arbitrary
// string (in practice the client IP). It is process-local: restarting the
// backend clears every window, and a multi-replica deployment would limit
// per replica rather than globally. Caddy applies its own rate limits in
// front of this, so this layer is a second line of defence, not the only one.
type RateLimiter struct {
mu sync.Mutex
// attempts holds the timestamps of recent allowed attempts per key.
// Keys are never evicted, so memory grows with the number of distinct
// client IPs seen since start-up; acceptable for a personal site, but
// worth knowing before reusing this elsewhere.
attempts map[string][]time.Time
max int
window time.Duration
}
// NewRateLimiter returns a limiter allowing at most max calls to Allow per
// key within any window-length period.
func NewRateLimiter(max int, window time.Duration) *RateLimiter {
return &RateLimiter{
attempts: make(map[string][]time.Time),
max: max,
window: window,
}
}
// Allow records an attempt for key and reports whether it is within the
// limit. A rejected attempt is deliberately NOT recorded, so a caller that
// keeps hammering does not extend its own lockout indefinitely.
func (rl *RateLimiter) Allow(key string) bool {
rl.mu.Lock()
defer rl.mu.Unlock()
now := time.Now()
cutoff := now.Add(-rl.window)
// Filter in place: slicing to [:0] reuses the existing backing array, so
// the append below overwrites the old timestamps instead of allocating a
// new slice on every request. Safe here because we only ever write an
// element at an index we have already read past. A missing key yields a
// nil slice, and nil[:0] is legal and yields nil.
valid := rl.attempts[key][:0]
for _, t := range rl.attempts[key] {
if t.After(cutoff) {
valid = append(valid, t)
}
}
if len(valid) >= rl.max {
rl.attempts[key] = valid
return false
}
rl.attempts[key] = append(valid, now)
return true
}