package adminhandler import ( "sync" "time" ) // rateLimiter is a simple fixed-window per-key limiter used to throttle // anonymous public submissions (basic anti-spam; captcha can be added later). type rateLimiter struct { mu sync.Mutex hits map[string][]time.Time limit int window time.Duration } func newRateLimiter(limit int, window time.Duration) *rateLimiter { return &rateLimiter{hits: map[string][]time.Time{}, limit: limit, window: window} } // allow reports whether the key may proceed, recording the hit if so. func (r *rateLimiter) allow(key string) bool { now := time.Now() cutoff := now.Add(-r.window) r.mu.Lock() defer r.mu.Unlock() kept := r.hits[key][:0] for _, t := range r.hits[key] { if t.After(cutoff) { kept = append(kept, t) } } if len(kept) >= r.limit { r.hits[key] = kept return false } r.hits[key] = append(kept, now) return true }