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.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
---
|
||||
name: rate_limiter_cleanup
|
||||
kind: function
|
||||
lang: go
|
||||
domain: infra
|
||||
version: "1.0.0"
|
||||
purity: impure
|
||||
signature: "func RateLimiterCleanup(rl *RateLimiter, maxAge time.Duration, interval time.Duration) func()"
|
||||
description: "Arranca una goroutine que purga periodicamente las entries del RateLimiter sin actividad reciente. Retorna una funcion para detener el GC sin leaks de goroutine."
|
||||
tags: [rate_limit, http, middleware, cleanup, gc, goroutine, infra]
|
||||
uses_functions: []
|
||||
uses_types: [RateLimiter_go_infra]
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: "error_go_core"
|
||||
imports: [time]
|
||||
params:
|
||||
- name: rl
|
||||
desc: "puntero al RateLimiter cuyas entries se purgaran"
|
||||
- name: maxAge
|
||||
desc: "edad maxima sin actividad antes de purgar una entry (ej: 10*time.Minute)"
|
||||
- name: interval
|
||||
desc: "frecuencia del barrido de purga (ej: 5*time.Minute). 0 usa default 1 minuto"
|
||||
output: "funcion stop sin argumentos que detiene la goroutine de cleanup. Idempotente — llamarla varias veces es seguro"
|
||||
tested: true
|
||||
tests: ["purga entries con lastSeen mas antiguo que maxAge", "no purga entries recientes", "stop detiene la goroutine sin panic", "stop es idempotente"]
|
||||
test_file_path: "functions/infra/rate_limiter_cleanup_test.go"
|
||||
file_path: "functions/infra/rate_limiter_cleanup.go"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```go
|
||||
rl := RateLimiterNew(10, 20)
|
||||
stop := RateLimiterCleanup(rl, 10*time.Minute, 5*time.Minute)
|
||||
defer stop()
|
||||
|
||||
// Servir requests... el GC limpia entries inactivas en background.
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
Funcion impura — arranca una goroutine, depende de tiempo wall-clock. La goroutine se cierra al invocar la funcion retornada (cierra un channel interno). Sin invocar stop la goroutine vive hasta que el proceso termina (leak). Valores <= 0 para interval o maxAge se sustituyen por defaults seguros (1 min / 10 min). El barrido toma el mutex del limiter — un interval muy corto puede causar contencion en limiters con muchas keys.
|
||||
Reference in New Issue
Block a user