feat(api): make API calls non-blocking#6733
Open
xxo1shine wants to merge 1 commit intotronprotocol:developfrom
Open
feat(api): make API calls non-blocking#6733xxo1shine wants to merge 1 commit intotronprotocol:developfrom
xxo1shine wants to merge 1 commit intotronprotocol:developfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Convert the HTTP and gRPC rate-limiter paths from blocking
acquire()to non-blockingtryAcquire(), and fix the related issues that surfaced along the way.Core API switched to non-blocking
GlobalRateLimiter.acquire()→GlobalRateLimiter.tryAcquire()(returnsboolean)IRateLimiter.acquire()/ strategies / adapters updated totryAcquire429/ gRPCRESOURCE_EXHAUSTEDimmediately instead of holding the worker thread in a wait queueReordered limit checks: per-endpoint first
RateLimiterServlet.serviceandRateLimiterInterceptor.interceptCallnow check the per-endpoint limit first, and only consult the global limiter onsuccess
acquireResource = perEndpointAcquired && GlobalRateLimiter.tryAcquire(...)Fixed semaphore leaks in
IPreemptibleRateLimiterfinallyblock now keys offperEndpointAcquiredinstead ofacquireResource)release()added on the early-return branch (global reject after per-endpoint acquired) and in thecatchblock whennext.startCall()throws
Hardened cache-load failure handling in
GlobalRateLimiter/IPQpsStrategycatch (ExecutionException)broadened tocatch (Exception)(coversUncheckedExecutionException, etc.)logger.warnwith the offending IP ande.getMessage()return false) — the previous code silently fail-opened by creating a fresh, non-cachedRateLimiterper call, which effectivelybypassed rate limiting for that IP after the first failure
Cleanup + test coverage
GlobalPreemptibleStrategy: removed the unused@Slf4jand an obsolete timeout constantAdaptorThread.java(the 20-thread timing-based assertion); replaced by deterministic sequential testsRateLimiterServletTestandRateLimiterInterceptorTest; expandedGlobalRateLimiterTestWhy are these changes required?
Blocking
acquire()is a tail-latency hazard. Under load, request threads pile up waiting for permits. Once the bounded Netty/Jetty worker pool is exhaustedby such waiters, the node stops responding to healthy traffic too — and the impact persists past the original burst. Non-blocking
tryAcquirereturns immediately,so threads remain available for legitimate requests.
The old ordering wasted global quota. A request was charged a global token before the per-endpoint check ran. A hot endpoint that was itself throttled could
still drain the global budget, letting attackers exhaust shared capacity through a single endpoint they were already capped on. Per-endpoint-first closes this gap.
Permit leaks in
IPreemptibleRateLimiterare a slow-motion outage. The previous code skippedrelease()in two paths:next.startCall()threw in the gRPC path.The semaphore drifts monotonically downward; once it reaches zero, every subsequent gRPC call blocks indefinitely. This is a latent availability bug that
accumulates with time.
Silent fail-open is an anti-abuse anti-pattern. When
cache.get(loader)threw, the original code created a freshRateLimiterwithout inserting it into thecache, so every subsequent call from the failing path got a full burst budget — effectively no rate limit, and no operational signal. Fail-closed plus a
warnlogmakes the failure both safe and observable.
The old
AdaptorTestwas a flaky test. 20 concurrent threads plusassertTrue(t1 - t0 > 4000)is highly sensitive to CI scheduler jitter and producedsporadic failures. Replaced with sequential
tryAcquirecalls that assert deterministic burst behavior of Guava'sSmoothBursty, so CI is stable.This PR has been tested by:
Follow up
Extra details
Fixes #6363