Repository navigation
Skip permit bookkeeping for the no-op semaphore - #2420
Open
pavel-ptashyts wants to merge 1 commit into
Open
pavel-ptashyts wants to merge 1 commit into
pavel-ptashyts wants to merge 1 commit into
Conversation
With the default unlimited connection limits the semaphore is the built-in NoopConnectionSemaphore, yet every new connection still walked the event loop group to pick a blocking or non-blocking acquire, stored the partition key as a held lock, and in NettyConnectListener.onSuccess allocated a permit holder and two release callbacks, set the channel's permitRelease attribute and added a close listener. NettyRequestSender now decides once, when the client is built, that exactly that class needs no bookkeeping and keeps no semaphore. The future and the connect listener already handle a null semaphore. Any other semaphore, including a subclass of the no-op one, a custom factory's or a finite limit, keeps today's behaviour. The gain is small and only for new connections: about 65 ns and 96 bytes per connection with 8 event loops, 140 ns with 32. Claude Code on behalf of Pavel Ptashyts Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Problem
With the default
maxConnections=-1andmaxConnectionsPerHost=-1,DefaultConnectionSemaphoreFactoryreturns aNoopConnectionSemaphore. Everynew connection still went through the whole permit dance for a permit that
does nothing:
NettyRequestSender.sendRequestWithNewChannelcalledisOnEventLoop(),which walks every executor of the event loop group, only to choose between a
blocking and a non-blocking acquire of the no-op permit;
NettyResponseFuture.acquirePartitionLockLazilystored the partition key asthe held "lock";
NettyConnectListener.onSuccesstook that key back, allocated anAtomicReferenceand two capturing release callbacks, stored one on thechannel's
permitReleaseattribute and added the other as acloseFuturelistener, which runs again when the connection closes.
This is paid once per new connection, not per pooled request, so it only shows
under connection churn (
keepAlive=false, short idle timeouts, servers thatclose connections).
Change
NettyRequestSenderdecides once, when the client is built, whether thesemaphore is exactly the built-in
NoopConnectionSemaphore. If it is, thesender keeps
nullinstead and:acquirePartitionLockLazily(isOnEventLoop()), so no group walk;nulltoNettyResponseFutureandNettyConnectListener, whichalready had branches for a null semaphore: no lock token, no channel
attribute, no close listener;
NettyConnectListenerno longer allocates the emptyAtomicReferencewhenthere is no permit to hold;
releasePermitOncetreats a null holder as"nothing to release".
Checked every user of the semaphore and the permit on the null path:
Channels.releasePermitfinds no attribute and does nothing; the abort pathsand
NettyResponseFuture.releasePartitionKeyLockreturn early;reacquirePermitandreuseOrDeferHttp2Connectionare only reached from theIOExceptionof a failed acquire, which cannot happen without a semaphore;registerHttp2AndManageSemaphorereleases throughreleasePermitOnce, which isa no-op either way.
What does not change
NoopConnectionSemaphore(a finite limit, acustom
ConnectionSemaphoreFactory, a user subclass ofNoopConnectionSemaphore) is used exactly as before, including non-blockingacquisition on an event loop and the exactly-once transfer and release on
abort, close, a failed TLS handshake and a connect retry. A custom factory
that returns the built-in no-op class itself also takes the fast path; that
class does nothing, so nothing observable changes.
no-op it was already a no-op.
NettyRequestSender.newConnectionSemaphoreispackage-private.
Benchmark
New
ConnectionPermitBenchmark, JMH 1.37, JDK 11.0.33 (Corretto), Windows,1 fork, 5 x 1 s warmup, 5 x 1 s measurement,
-prof gc, on a machine runningother builds (times are noisy, B/op is exact).
noopPermitmirrors what a newconnection did with the default limits: the group walk from a caller thread,
acquirePartitionLockLazilyon a realNettyResponseFuturewith aNoopConnectionSemaphore, theonSuccessbookkeeping and the close listenerfiring.
noPermitis the same connection on this branch. Both build theresponse future and a fresh close promise, which every new connection has
anyway.
isOnEventLoopis the group walk alone.So a new connection saves about 65 ns and 96 bytes with 8 event loops, and
about 140 ns with 32 (the default is two per available processor). This is
small: a new connection costs a TCP connect, often a TLS handshake, and tens of
microseconds or more of other work. Pooled requests are not affected at all.
The change is offered because it is small and confined, not because the gain
is large.
Tests
NettyRequestSenderConnectionSemaphoreTest(new): the default unlimitedconfig yields no semaphore; finite
maxConnectionsandmaxConnectionsPerHostkeepMaxConnectionSemaphoreandCombinedConnectionSemaphore; a custom factory's semaphore and aNoopConnectionSemaphoresubclass are kept as the same instance.NettyConnectListenerPermitLeakTest:noSemaphoreBindsNoPermitRelease(no permit taken, no
permitReleaseattribute on the channel, release andclose are harmless) and
aFiniteSemaphoreBindsThePermitReleaseToTheChannel(the attribute is set, and an explicit release plus the close release the
permit exactly once).
SemaphoreTest,ConnectionSemaphoreLeakTest,NettyConnectListenerPermitLeakTest,NettyConnectListenerTestandChannelManagerHttp2DrainPermitTestpass unchanged../mvnw clean verifyon JDK 11 (Corretto 11.0.33): BUILD SUCCESS, 2490 tests, 0 failures, 0 errors, 47 skipped, Revapi passed (run with-Dgpg.skip=true: there is no signing key on this machine).Claude Code on behalf of Pavel Ptashyts
🤖 Generated with Claude Code