036c0a8d63
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.
1.8 KiB
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). |
|
|
|
false | error_go_core |
|
|
RateLimitResult con Allowed, Remaining, ResetAt y RetryAfter calculados a partir del estado del bucket | true |
|
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.