first commit

This commit is contained in:
Mateusz Gruszczyński committed 2026-10-30 10:56:13 +01:00
commit fccfe2a4c8
26 files changed
+1819

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
package web
import (
"sync"
"time"
)
type rateEntry struct {
since time.Time
n int
}
type limiter struct {
mu sync.Mutex
entries map[string]rateEntry
}
func newLimiter() *limiter { return &limiter{entries: make(map[string]rateEntry)} }
func (l *limiter) allow(key string, max int, window time.Duration) bool {
now := time.Now()
l.mu.Lock()
defer l.mu.Unlock()
e := l.entries[key]
if e.since.IsZero() || now.Sub(e.since) >= window {
l.entries[key] = rateEntry{since: now, n: 1}
return true
}
if e.n >= max {
return false
}
e.n++
l.entries[key] = e
if len(l.entries) > 10000 {
for k, v := range l.entries {
if now.Sub(v.since) > 2*window {
delete(l.entries, k)
}
}
}
return true
}