• erio's avatar
    fix(billing): add window expiration check to Redis rate limit Lua script · 67c05062
    erio authored
    The updateRateLimitUsageScript Lua script previously performed
    unconditional HINCRBYFLOAT on all usage counters without checking
    whether the rate limit window had expired. This caused usage to
    accumulate across window boundaries in Redis while the DB correctly
    reset on expiration, leading to incorrect 429 rate limiting that
    could persist for up to 24 hours.
    
    The Lua script now checks each window timestamp before incrementing:
    - If the window has expired, usage is reset to the current cost and
      the window timestamp is updated (matching DB-side semantics)
    - If the window is still valid, usage is accumulated normally
    
    This also resolves the async race condition where stale HINCRBYFLOAT
    tasks from the worker queue could pollute a freshly rebuilt cache
    after invalidation, since the script now self-corrects expired windows.
    
    Closes #1049
    67c05062
billing_cache.go 10.1 KB