Trim comments to the non-obvious why
Drop narration, history and restated-code comments across the repo, keep short ones for security boundaries, upstream quirks and ordering constraints, and record the convention in CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
// 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 holds the backend's stateful collaborators, reached
|
||||
// through handlers.Store.
|
||||
package services
|
||||
|
||||
import (
|
||||
@@ -9,24 +7,15 @@ import (
|
||||
"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.
|
||||
// RateLimiter is an in-memory sliding window per key, behind Caddy's limits.
|
||||
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.
|
||||
// Never evicted; grows with distinct client IPs.
|
||||
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),
|
||||
@@ -35,9 +24,7 @@ func NewRateLimiter(max int, window time.Duration) *RateLimiter {
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
// Allow doesn't record rejected attempts, so hammering doesn't extend a lockout.
|
||||
func (rl *RateLimiter) Allow(key string) bool {
|
||||
rl.mu.Lock()
|
||||
defer rl.mu.Unlock()
|
||||
@@ -45,11 +32,7 @@ func (rl *RateLimiter) Allow(key string) bool {
|
||||
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.
|
||||
// Filtered in place, reusing the backing array.
|
||||
valid := rl.attempts[key][:0]
|
||||
for _, t := range rl.attempts[key] {
|
||||
if t.After(cutoff) {
|
||||
|
||||
Reference in New Issue
Block a user