airframe-control: Add RateLimiter with token bucket algorithm#3942
airframe-control: Add RateLimiter with token bucket algorithm#3942
Conversation
Co-authored-by: xerial <[email protected]>
Co-authored-by: xerial <[email protected]>
xerial
left a comment
There was a problem hiding this comment.
Compilation is failing in Scala.js. Fix the error
| test("handle concurrent access") { | ||
| val limiter = RateLimiter.create(100.0) // High rate to avoid blocking in test | ||
| val counter = new AtomicInteger(0) | ||
| val threads = 10 | ||
| val acquisitionsPerThread = 10 | ||
|
|
||
| val threadList = (1 to threads).map { _ => | ||
| new Thread(() => { | ||
| for (_ <- 1 to acquisitionsPerThread) { | ||
| limiter.acquire() | ||
| counter.incrementAndGet() | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| threadList.foreach(_.start()) | ||
| threadList.foreach(_.join()) | ||
|
|
||
| counter.get() shouldBe threads * acquisitionsPerThread | ||
| } |
There was a problem hiding this comment.
java.lang.Thread doesn't exist in Scala.js. If this test is important, move this test to .jvm folder
There was a problem hiding this comment.
Moved the concurrent access test that uses java.lang.Thread to a new JVM-specific test file RateLimiterJVMTest.scala in the .jvm/src/test/scala directory. This resolves the Scala.js compilation issue while preserving the important concurrency test for JVM environments. Commit: 98a26ae
…mpilation Co-authored-by: xerial <[email protected]>
Fixed the Scala.js compilation error by moving the Thread-based concurrent test to a JVM-specific test file. The test now runs only on JVM while maintaining cross-platform compatibility for all other RateLimiter functionality. Commit: 98a26ae |
|
The code is not formatted correctly. Use |
Co-authored-by: xerial <[email protected]>
Fixed code formatting by running |
This PR implements a
RateLimiterfor airframe-control using a token bucket algorithm to control the rate of operations. This addresses the need for rate limiting and retry budget control mentioned in the issue.Features
Core Functionality
Compat.sleepacquire()andtryAcquire()methodstryAcquire(permits, timeout, unit)Usage Examples
Basic rate limiting:
Burst control:
Per-host rate limiting:
Implementation Details
Key Design Decisions
tryReservePermits(non-blocking) doesn't allow token bucket to go negative, whilereservePermits(blocking) can reserve future tokensAtomicReference[RateLimiterState]with immutable case class for thread safetypermitsPerSecondbut can be customizedCompat.sleepfor millisecond-level precision across platformsTest Coverage
Documentation
Added comprehensive documentation to
airframe-control.mdwith:The implementation provides a foundation for maintaining average traffic rates and implementing retry budgets as mentioned in the original issue, complementing the existing retry and circuit breaker functionality in airframe-control.
Fixes #1027.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.