package infra import ( "math" "net/http" "strconv" ) // RateLimitHeaders construye los headers IETF estandar de rate limiting a partir de un resultado. // Setea X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset y, si no esta permitido, Retry-After. // limit es la capacidad total (burst) que se anuncia al cliente. // Funcion pura — solo formatea, no hace I/O. func RateLimitHeaders(result RateLimitResult, limit int) http.Header { h := http.Header{} h.Set("X-RateLimit-Limit", strconv.Itoa(limit)) h.Set("X-RateLimit-Remaining", strconv.Itoa(result.Remaining)) h.Set("X-RateLimit-Reset", strconv.FormatInt(result.ResetAt.Unix(), 10)) if !result.Allowed { // Retry-After segun RFC 7231: numero entero de segundos. retryAfter := int(math.Ceil(result.RetryAfter)) if retryAfter < 1 { retryAfter = 1 } h.Set("Retry-After", strconv.Itoa(retryAfter)) } return h }