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

42 lines
1.7 KiB
Markdown

---
name: rate_limit_headers
kind: function
lang: go
domain: infra
version: "1.0.0"
purity: pure
signature: "func RateLimitHeaders(result RateLimitResult, limit int) http.Header"
description: "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."
tags: [rate_limit, http, headers, middleware, infra]
uses_functions: []
uses_types: [RateLimitResult_go_infra]
returns: []
returns_optional: false
error_type: ""
imports: [math, net/http, strconv]
params:
- name: result
desc: "RateLimitResult devuelto por RateLimiterCheck"
- name: limit
desc: "capacidad total (burst) anunciada al cliente en X-RateLimit-Limit"
output: "http.Header con los 3 headers X-RateLimit-* y, cuando result.Allowed es false, Retry-After (segundos enteros)"
tested: true
tests: ["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"]
test_file_path: "functions/infra/rate_limit_headers_test.go"
file_path: "functions/infra/rate_limit_headers.go"
---
## Ejemplo
```go
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.