Files
fn_registry/functions/infra/rate_limit_headers.md
egutierrez 036c0a8d63 feat(infra): rate limiter token-bucket in-memory + tipos y core funcs
Implementa fase 1 del issue 0016:
- Tipos RateLimiter, RateLimitConfig y RateLimitResult en types/infra/
- rate_limiter_new: constructor con validacion rate/burst > 0
- rate_limiter_check: evalua token bucket por key, calcula Allowed/Remaining/ResetAt/RetryAfter
- rate_limit_headers (pure): construye headers IETF X-RateLimit-* y Retry-After
- rate_limiter_cleanup: goroutine GC de entries inactivas con stop idempotente

Sin dependencias externas (no Redis). sync.Mutex + map. Algoritmo token bucket
estandar con recarga lineal proporcional al tiempo transcurrido.
2026-04-18 17:11:22 +02:00

1.7 KiB

name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, params, output, tested, tests, test_file_path, file_path
name kind lang domain version purity signature description tags uses_functions uses_types returns returns_optional error_type imports params output tested tests test_file_path file_path
rate_limit_headers function go infra 1.0.0 pure func RateLimitHeaders(result RateLimitResult, limit int) http.Header Construye los headers IETF estandar de rate limiting (X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset y Retry-After cuando se rechaza). Funcion pura, solo formatea.
rate_limit
http
headers
middleware
infra
RateLimitResult_go_infra
false
math
net/http
strconv
name desc
result RateLimitResult devuelto por RateLimiterCheck
name desc
limit capacidad total (burst) anunciada al cliente en X-RateLimit-Limit
http.Header con los 3 headers X-RateLimit-* y, cuando result.Allowed es false, Retry-After (segundos enteros) true
setea X-RateLimit-Limit con el burst
setea X-RateLimit-Remaining con result.Remaining
setea X-RateLimit-Reset como timestamp unix
incluye Retry-After cuando no esta permitido
no incluye Retry-After cuando esta permitido
functions/infra/rate_limit_headers_test.go functions/infra/rate_limit_headers.go

Ejemplo

result := RateLimiterCheck(rl, "192.168.1.1")
headers := RateLimitHeaders(result, 20)
for k, v := range headers {
    w.Header()[k] = v
}

Notas

Funcion pura — sin I/O, sin estado, deterministica para una misma entrada. Sigue draft-ietf-httpapi-ratelimit-headers. Retry-After se redondea hacia arriba en segundos enteros (RFC 7231) y nunca se setea por debajo de 1 (sino el cliente reintenta inmediatamente). El timestamp en X-RateLimit-Reset es Unix epoch en segundos.