Skip to content

Skip permit bookkeeping for the no-op semaphore - #2420

Open
pavel-ptashyts wants to merge 1 commit into
AsyncHttpClient:mainfrom
maygemdev:skip-noop-permit-bookkeeping
Open

pavel-ptashyts wants to merge 1 commit into
AsyncHttpClient:mainfrom
maygemdev:skip-noop-permit-bookkeeping

Conversation

@pavel-ptashyts

Copy link
Copy Markdown
Contributor

Problem

With the default maxConnections=-1 and maxConnectionsPerHost=-1,
DefaultConnectionSemaphoreFactory returns a NoopConnectionSemaphore. Every
new connection still went through the whole permit dance for a permit that
does nothing:

  • NettyRequestSender.sendRequestWithNewChannel called isOnEventLoop(),
    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.acquirePartitionLockLazily stored the partition key as
    the held "lock";
  • NettyConnectListener.onSuccess took that key back, allocated an
    AtomicReference and two capturing release callbacks, stored one on the
    channel's permitRelease attribute and added the other as a closeFuture
    listener, 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 that
close connections).

Change

NettyRequestSender decides once, when the client is built, whether the
semaphore is exactly the built-in NoopConnectionSemaphore. If it is, the
sender keeps null instead and:

  • skips acquirePartitionLockLazily(isOnEventLoop()), so no group walk;
  • hands null to NettyResponseFuture and NettyConnectListener, which
    already had branches for a null semaphore: no lock token, no channel
    attribute, no close listener;
  • NettyConnectListener no longer allocates the empty AtomicReference when
    there is no permit to hold; releasePermitOnce treats a null holder as
    "nothing to release".

Checked every user of the semaphore and the permit on the null path:
Channels.releasePermit finds no attribute and does nothing; the abort paths
and NettyResponseFuture.releasePartitionKeyLock return early;
reacquirePermit and reuseOrDeferHttp2Connection are only reached from the
IOException of a failed acquire, which cannot happen without a semaphore;
registerHttp2AndManageSemaphore releases through releasePermitOnce, which is
a no-op either way.

What does not change

  • Any semaphore that is not exactly NoopConnectionSemaphore (a finite limit, a
    custom ConnectionSemaphoreFactory, a user subclass of
    NoopConnectionSemaphore) is used exactly as before, including non-blocking
    acquisition 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.
  • HTTP/2: the permit plumbing it shares is untouched for finite limits; for the
    no-op it was already a no-op.
  • No public API change. The new NettyRequestSender.newConnectionSemaphore is
    package-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 running
other builds (times are noisy, B/op is exact). noopPermit mirrors what a new
connection did with the default limits: the group walk from a caller thread,
acquirePartitionLockLazily on a real NettyResponseFuture with a
NoopConnectionSemaphore, the onSuccess bookkeeping and the close listener
firing. noPermit is the same connection on this branch. Both build the
response future and a fresh close promise, which every new connection has
anyway. isOnEventLoop is the group walk alone.

Benchmark eventLoops ns/op B/op
noopPermit (before) 8 136.6 +- 3.3 368
noPermit (after) 8 70.6 +- 1.0 272
noopPermit (before) 32 211.8 +- 11.4 368
noPermit (after) 32 71.3 +- 3.3 272
isOnEventLoop 8 29.9 +- 2.9 32
isOnEventLoop 32 79.5 +- 0.8 32

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 unlimited
    config yields no semaphore; finite maxConnections and
    maxConnectionsPerHost keep MaxConnectionSemaphore and
    CombinedConnectionSemaphore; a custom factory's semaphore and a
    NoopConnectionSemaphore subclass are kept as the same instance.
  • NettyConnectListenerPermitLeakTest: noSemaphoreBindsNoPermitRelease
    (no permit taken, no permitRelease attribute on the channel, release and
    close are harmless) and aFiniteSemaphoreBindsThePermitReleaseToTheChannel
    (the attribute is set, and an explicit release plus the close release the
    permit exactly once).
  • Existing SemaphoreTest, ConnectionSemaphoreLeakTest,
    NettyConnectListenerPermitLeakTest, NettyConnectListenerTest and
    ChannelManagerHttp2DrainPermitTest pass unchanged.
  • ./mvnw clean verify on 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

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>
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.

1 participant