CODE_CONVENTIONS.md sets out the conventions for Go code in this repository: naming, layout, comments, errors, logging, configuration and tests. .golangci.json enables the linters that check the parts that can be checked (each limited to the rules the conventions use); the rest is applied by hand. Applied across the codebase: - Naming: full-word names in application code, initialisms in one case (BuyerID, PrintJSON, PrintYAML, ocdsGetBuyerID), no names shadowing built-ins (min/max are minGap/maxGap), mustRaw renamed rawJSON as it returns nil rather than panicking, and config.ConfigLevelToLogLevel renamed LevelToLogLevel. - Layout: lines kept to 80 characters where possible, assignments taken out of if and for statements that don't fit, wrapped arguments all on one line or one per line, steps separated by blank lines, no else after a return, function and struct literals wrapped. - Comments: doc comments on every exported name (including each section's Map, Flatten, TypeOf, ValueOf and SetValue), section banners, British spelling. - Errors: a created error starts with its package name, a wrapping error adds only what was being done, sentinels in one commented var block, errors.Is and errors.As in place of == and type assertions, and no err := shadowing of a named result. That fixes the mock server losing its access log's close error. - Logging: the fetcher passes its workers an untagged logger, so findatender's lines are tagged with their own component rather than with fetcher's as well; cmd/mock-fts logs "stopped" with slog. - Configuration: util.WithTimeout (moved from the websocket package) applies a timeout of 0 or less as no limit, used for the websocket write and ping, TCP close and tcpserver shutdown timeouts; every setting documents what 0 or empty means, including the rate limit rule's keys in internal/config/README.md. - Tests: related tests merged into table-driven ones with named cases. Separately from CODE_CONVENTIONS.md, internal/store/README.md gains a soft SQL style convention for the queries in internal/store/queries.go, and the queries are reformatted to follow it.
162 lines
3.8 KiB
Go
162 lines
3.8 KiB
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"hash/maphash"
|
|
"math"
|
|
"net/http"
|
|
"strconv"
|
|
"sync"
|
|
"time"
|
|
|
|
"golang.org/x/time/rate"
|
|
|
|
"git.avsdev.uk/Go/job-search/internal/httpx"
|
|
)
|
|
|
|
// Limiter is the rate limiting hook. Implementations decide whether a request
|
|
// identified by key may proceed. Swap in a Redis/memcached-backed limiter for
|
|
// multi-instance deployments by implementing this interface.
|
|
type Limiter interface {
|
|
Allow(key string) (allowed bool, retryAfter time.Duration)
|
|
}
|
|
|
|
// LimiterFunc adapts a function to the Limiter interface.
|
|
type LimiterFunc func(key string) (bool, time.Duration)
|
|
|
|
// Allow implements Limiter by calling f.
|
|
func (f LimiterFunc) Allow(key string) (bool, time.Duration) { return f(key) }
|
|
|
|
// KeyFunc derives the rate limit key from a request.
|
|
type KeyFunc func(*http.Request) string
|
|
|
|
// KeyByIP keys on the real client IP (after proxy processing).
|
|
func KeyByIP(r *http.Request) string { return ClientIP(r) }
|
|
|
|
// RateLimit turns a Limiter into middleware. Rejected requests get 429 with
|
|
// a Retry-After header. A nil limiter yields a pass-through middleware.
|
|
func RateLimit(limiter Limiter, key KeyFunc) func(http.Handler) http.Handler {
|
|
if limiter == nil {
|
|
return func(next http.Handler) http.Handler {
|
|
return next
|
|
}
|
|
}
|
|
if key == nil {
|
|
key = KeyByIP
|
|
}
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ok, retry := limiter.Allow(key(r))
|
|
if !ok {
|
|
seconds := int(math.Ceil(retry.Seconds()))
|
|
if seconds < 1 {
|
|
seconds = 1
|
|
}
|
|
w.Header().Set("Retry-After", strconv.Itoa(seconds))
|
|
httpx.WriteProblem(
|
|
w, r, http.StatusTooManyRequests, "rate limit exceeded",
|
|
)
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
}
|
|
|
|
// KeyedLimiter is an in-memory token bucket per key, sharded to reduce lock
|
|
// contention, with idle-entry eviction.
|
|
type KeyedLimiter struct {
|
|
limit rate.Limit
|
|
burst int
|
|
ttl time.Duration
|
|
seed maphash.Seed
|
|
shards [32]limiterShard
|
|
}
|
|
|
|
type limiterShard struct {
|
|
mu sync.Mutex
|
|
entries map[string]*limiterEntry
|
|
}
|
|
|
|
type limiterEntry struct {
|
|
limiter *rate.Limiter
|
|
last time.Time
|
|
}
|
|
|
|
// NewKeyedLimiter creates a limiter allowing requestsPerSecond sustained
|
|
// requests per second with the given burst. A janitor goroutine evicts
|
|
// entries idle for longer than ttl until ctx is cancelled.
|
|
func NewKeyedLimiter(
|
|
ctx context.Context, requestsPerSecond float64, burst int, ttl time.Duration,
|
|
) *KeyedLimiter {
|
|
limiter := &KeyedLimiter{
|
|
limit: rate.Limit(requestsPerSecond),
|
|
burst: burst,
|
|
ttl: ttl,
|
|
seed: maphash.MakeSeed(),
|
|
}
|
|
for i := range limiter.shards {
|
|
limiter.shards[i].entries = make(map[string]*limiterEntry)
|
|
}
|
|
|
|
if ttl > 0 {
|
|
go limiter.janitor(ctx)
|
|
}
|
|
|
|
return limiter
|
|
}
|
|
|
|
func (k *KeyedLimiter) shard(key string) *limiterShard {
|
|
return &k.shards[maphash.String(k.seed, key)%uint64(len(k.shards))]
|
|
}
|
|
|
|
// Allow implements Limiter.
|
|
func (k *KeyedLimiter) Allow(key string) (bool, time.Duration) {
|
|
now := time.Now()
|
|
shard := k.shard(key)
|
|
shard.mu.Lock()
|
|
entry := shard.entries[key]
|
|
if entry == nil {
|
|
entry = &limiterEntry{
|
|
limiter: rate.NewLimiter(k.limit, k.burst),
|
|
}
|
|
shard.entries[key] = entry
|
|
}
|
|
entry.last = now
|
|
limiter := entry.limiter
|
|
shard.mu.Unlock()
|
|
|
|
reservation := limiter.ReserveN(now, 1)
|
|
if !reservation.OK() {
|
|
return false, time.Second
|
|
}
|
|
if delay := reservation.DelayFrom(now); delay > 0 {
|
|
reservation.CancelAt(now)
|
|
return false, delay
|
|
}
|
|
|
|
return true, 0
|
|
}
|
|
|
|
func (k *KeyedLimiter) janitor(ctx context.Context) {
|
|
ticker := time.NewTicker(k.ttl / 2)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case now := <-ticker.C:
|
|
for i := range k.shards {
|
|
shard := &k.shards[i]
|
|
shard.mu.Lock()
|
|
for key, entry := range shard.entries {
|
|
if now.Sub(entry.last) > k.ttl {
|
|
delete(shard.entries, key)
|
|
}
|
|
}
|
|
shard.mu.Unlock()
|
|
}
|
|
}
|
|
}
|
|
}
|