Files
fn_registry/functions/infra/rate_limiter_check.md
T
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.8 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_limiter_check function go infra 1.0.0 impure func RateLimiterCheck(rl *RateLimiter, key string) RateLimitResult Evalua un request del cliente identificado por key contra el token bucket. Recarga tokens segun el tiempo transcurrido y consume uno si esta permitido. Seguro para uso concurrente (mutex interno).
rate_limit
http
middleware
token_bucket
check
infra
RateLimiter_go_infra
RateLimitResult_go_infra
RateLimitResult_go_infra
false error_go_core
math
time
name desc
rl puntero al RateLimiter creado con RateLimiterNew
name desc
key identificador del cliente (IP, API key, user ID, etc.)
RateLimitResult con Allowed, Remaining, ResetAt y RetryAfter calculados a partir del estado del bucket true
primer request siempre allowed con burst-1 remaining
consumir todos los tokens bloquea siguiente request
los tokens se recargan con el paso del tiempo
retryAfter es positivo cuando se rechaza
keys distintas tienen buckets independientes
functions/infra/rate_limiter_check_test.go functions/infra/rate_limiter_check.go

Ejemplo

rl := RateLimiterNew(10, 20)

result := RateLimiterCheck(rl, "192.168.1.1")
if !result.Allowed {
    // sleep(result.RetryAfter * time.Second) o 429
}

Notas

Funcion impura porque depende de tiempo wall-clock (time.Now) y muta el estado del limiter. Algoritmo token bucket clasico: cada bucket se recarga linealmente a rate tokens/segundo hasta burst. Un request consume 1 token. La primera vez que aparece una key se crea el bucket lleno (descontando ya el primer token). El mutex asegura uso seguro desde multiples goroutines.