Skip to content

fix(edge): fix shared result tables in llm_limiter and incomplete reset in circuit_breaker#153

Open
levleontiev wants to merge 3 commits intomainfrom
fix/llm-limiter-shared-results-and-cb-reset
Open

fix(edge): fix shared result tables in llm_limiter and incomplete reset in circuit_breaker#153
levleontiev wants to merge 3 commits intomainfrom
fix/llm-limiter-shared-results-and-cb-reset

Conversation

@levleontiev
Copy link
Copy Markdown
Contributor

@levleontiev levleontiev commented Apr 13, 2026

Summary

This PR fixes two significant bugs and adds performance optimizations in fairvisor/edge:

  1. llm_limiter.lua: Shared Result Tables (Bug Fix)

    • Issue: The module used module-level tables _result and _reconcile_result to return results from check and reconcile. This caused data corruption when multiple rules/policies used the LLM limiter in the same request, or across concurrent requests in the same worker.
    • Fix: Removed shared tables and _clear_result. Updated functions to return new table literals.
  2. circuit_breaker.lua: Incomplete Auto-Reset (Bug Fix)

    • Issue: The check functions auto-reset logic only deleted the state_key, but did not clear the rate_keys`. Since the sliding window calculation continued immediately after reset, it would see the old (high) rates and immediately re-trip the breaker.
    • Fix: Updated check to call _M.reset() which correctly clears both state and rate keys.
  3. llm_limiter.lua: _simple_word_estimate Optimization (Perf)

    • Optimization: Replaced string.sub truncation with index-based scanning to avoid large memory allocations for large request bodies. Also replaced string.sub with string.byte in the backslash-scanning loop (Zero-Allocation policy).
  4. saas_client.lua: Authorization Header Caching (Perf)

    • Optimization: Cached the rendered Bearer ... token in _state to avoid redundant string concatenations and validation regex on every SaaS API call (heartbeats, event flushes, config polls).

Testing

  • Verified with multiple reproduction scripts (shared table corruption, CB reset, regex anchoring behavior).
  • Ran all 649 unit tests via busted: All passed.

@levleontiev levleontiev force-pushed the fix/llm-limiter-shared-results-and-cb-reset branch from 5f49653 to a5ff14e Compare April 13, 2026 05:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants