Files
fn_registry/functions/infra/rate_limiter_new.md
T
egutierrez 4ac93a0933 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.5 KiB
Markdown

---
name: rate_limiter_new
kind: function
lang: go
domain: infra
version: "1.0.0"
purity: impure
signature: "func RateLimiterNew(rate float64, burst int) *RateLimiter"
description: "Crea un RateLimiter token-bucket in-memory vacio. rate son los tokens recargados por segundo, burst es la capacidad maxima del bucket. Valores <= 0 se sustituyen por 1 como minimo seguro."
tags: [rate_limit, http, middleware, token_bucket, infra]
uses_functions: []
uses_types: [RateLimiter_go_infra]
returns: [RateLimiter_go_infra]
returns_optional: false
error_type: "error_go_core"
imports: []
params:
- name: rate
desc: "tokens recargados por segundo (tasa sostenida, ej: 10 = 10 req/s)"
- name: burst
desc: "capacidad maxima del bucket (rafaga maxima permitida en un instante, ej: 20)"
output: "puntero a RateLimiter inicializado con mapa vacio de clientes, listo para usar con RateLimiterCheck"
tested: true
tests: ["crea limiter con rate y burst configurados", "valores cero se sustituyen por 1", "el mapa de clientes empieza vacio"]
test_file_path: "functions/infra/rate_limiter_new_test.go"
file_path: "functions/infra/rate_limiter_new.go"
---
## Ejemplo
```go
// 10 req/s sostenidos, hasta 20 en rafaga
rl := RateLimiterNew(10, 20)
// 1 req/s, sin rafaga
strict := RateLimiterNew(1, 1)
```
## Notas
Funcion impura porque retorna un valor con estado mutable (mapa interno). No hace I/O ni network. La validacion de rate y burst evita panics por configuracion invalida (division por cero, buckets imposibles). Para iniciar el GC de entries inactivas usar `RateLimiterCleanup`.