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,19 @@
|
||||
package infra
|
||||
|
||||
// RateLimiterNew crea un RateLimiter token-bucket vacio con la tasa y rafaga indicadas.
|
||||
// rate son los tokens recargados por segundo (sostenido).
|
||||
// burst es la capacidad maxima del bucket (rafaga puntual).
|
||||
// Si rate o burst son <= 0 se usa 1 como minimo seguro.
|
||||
func RateLimiterNew(rate float64, burst int) *RateLimiter {
|
||||
if rate <= 0 {
|
||||
rate = 1
|
||||
}
|
||||
if burst <= 0 {
|
||||
burst = 1
|
||||
}
|
||||
return &RateLimiter{
|
||||
rate: rate,
|
||||
burst: burst,
|
||||
clients: make(map[string]*rateLimiterClient),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user