Skip to content

Conversation

@JustinDFuller
Copy link
Owner

@JustinDFuller JustinDFuller commented Sep 11, 2025

Note

This PR was generated with Claude Code (https://claude.ai/code)

Summary

This PR implements pure probabilistic rate limiting to fix the "first request bypass" issue where the first request of each interval was always allowed through regardless of flow rate.

Key Changes

  1. Pure Probabilistic Rate Limiting: Every request now has exactly flowRate% chance of being allowed

    • Eliminates ALL edge cases and special handling
    • Unified approach for all requests (no special first request logic)
    • Based on Law of Large Numbers for convergence
  2. Upgraded to math/rand/v2: Better performance and concurrency

    • ChaCha8 algorithm for faster random number generation
    • Per-goroutine state eliminating global lock contention
    • Better statistical properties
  3. Updated Tests: Added statistical tolerances for natural variance

    • Tests now validate behavior patterns rather than exact values
    • More robust to inherent randomness of probabilistic decisions

Performance Impact

Benchmark comparison shows significant improvements with math/rand/v2:

Benchmark Before (ns/op) After (ns/op) Change Impact
DoBool_Open 232.6 228.1 -4.5 ns 2% faster
DoBool_Closed 50.77 47.64 -3.13 ns 6% faster
DoBool_Half 238.5 213.1 -25.4 ns 11% faster
DoError_Open 235.2 223.5 -11.7 ns 5% faster
DoError_Closed 61.66 61.09 -0.57 ns 1% faster
StateSnapshot 160,068 143,953 -16,115 ns 10% faster

Key Performance Wins:

  • Critical hot path (DoBool_Half) is 11% faster
  • Zero memory allocations maintained (0 B/op, 0 allocs/op)
  • Better concurrency with no global lock contention
  • Most operations show 2-11% improvement

Bug Fix

Previous behavior (bug):

// When allowed=0 and blocked=0:
allowRate = 0
if allowRate < flowRate // Always true for flowRate > 0
  // First request always allowed

New behavior (fixed):

// Every request, including first:
if rand.N(100) < flowRate
  // Exactly flowRate% chance

Testing

  • ✅ All existing tests pass
  • ✅ New test TestFirstRequestRespectsFlowRate validates the fix
  • ✅ Blackbox tests updated with statistical tolerances
  • ✅ Zero performance regression in hot paths
  • ✅ Memory allocations remain at zero

Breaking Changes

None. The API remains unchanged. The only observable difference is that the first request of each interval is no longer guaranteed to be allowed (which was the bug being fixed).

🤖 Generated with Claude Code (https://claude.ai/code)

JustinDFuller and others added 2 commits September 10, 2025 22:36
The first request of each interval was always allowed through regardless
of flowRate because allowed/blocked counters reset to 0, making the
allowRate calculation (0 < flowRate) always true.

This change implements a probabilistic decision for the first request,
ensuring it has exactly flowRate% chance of being allowed. This is
theoretically correct as rate limiting is fundamentally about probability
distributions.

Changes:
- Add probabilistic logic when allowed=0 and blocked=0
- Fix edge case where allowed=0 but blocked>0
- First request now respects the flow rate statistically
- Maintains all other rate limiting properties

Benefits:
- Fixes 20x rate limit violation for low flow rates
- Prevents timing attacks and unfairness
- Mathematically correct implementation
- Minimal code change with optimal performance

The probabilistic approach directly implements the intended probability
rather than approximating it through counting, which failed at boundaries.

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
Simplified the rate limiting algorithm to use pure probabilistic decisions
for ALL requests, not just the first one. This unified approach eliminates
all edge cases and special handling.

Key changes:
- Every request now has exactly flowRate% chance of being allowed
- Removed complex ratio calculations and special case logic
- Reduced ~20 lines of code to just 5 lines
- No more distinction between first and subsequent requests

Benefits:
- Eliminates ALL edge cases with a single unified algorithm
- Mathematically correct by directly implementing probability distribution
- Simpler, more maintainable code
- Better represents the stochastic nature of real traffic

Note: The blackbox tests fail because they expect deterministic behavior
with ±1 tolerance. Probabilistic rate limiting has natural variance that
exceeds this tolerance. These tests need updating to account for the
probabilistic nature of the algorithm.

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
@JustinDFuller JustinDFuller changed the title fix: implement probabilistic first request to prevent rate limit bypass feat: implement pure probabilistic rate limiting Sep 11, 2025
Replace math/rand with math/rand/v2 to leverage:
- ChaCha8 algorithm for faster random number generation (~1-2ns vs ~2-3ns)
- Per-goroutine state eliminating global lock contention
- Better statistical properties for more uniform distribution
- Improved concurrent performance in high-throughput scenarios

Also update blackbox tests to use statistical tolerances accounting for
natural variance in probabilistic rate limiting. Tests now validate
behavior patterns rather than exact values, making them more robust
to the inherent randomness of probabilistic decisions.

🤖 Generated with Claude Code (https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
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