Repository navigation
perf(client): release xClient mutex before dialing; server pool/mutex cleanups - #940
Merged
Merged
Conversation
added 2 commits
July 1, 2026 13:58
getCachedClient held c.mu across generateClient -> client.Connect (a blocking TCP dial). singleflight dedups concurrent dials to the same key, but the outer c.mu serialized ALL callers of the xClient behind one slow/hanging dial โ a single unstable server could stall selects for every other server key. Split into a fast path (cache lookup under c.mu) and a slow path (dial outside c.mu, inside singleflight, re-checking the cache and taking c.mu only to insert). The DoClientConnected plugin still fires exactly once per newly created connection via a `created` flag set only by the dialing goroutine. Add TestGetCachedClientDialDoesNotBlockOtherKeys, which fails (2s timeout) against the old locked version and passes with the fix.
- handleRequest/handleRequestForFunction fetched argv from the object pool but leaked it on the nil-codec and Decode-error paths; Put it back before returning the error so the pool doesn't degrade to always allocating. - Remove the unused embedded sync.Mutex from methodType/functionType. It guarded a numCalls counter that was removed long ago, is never locked, and pollutes the type method set with Lock/Unlock. - Add BenchmarkServerHandleRequest (ReportAllocs) as an anchor for the per-request dispatch path; server/ previously had no hot-path benchmark. Baseline ~811 ns/op, 809 B/op, 14 allocs/op.
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.
Summary
Two performance fixes surfaced by a concurrency + allocation review of the recently-split
client/servercode.1.
getCachedClientno longer holdsc.muacross the dial (main fix)getCachedClientheldc.muacrossgenerateClient โ client.Connect(a blocking TCP dial).singleflightdedups concurrent dials to the same key, but the outerc.muserialized all callers of the xClient behind one slow/hanging dial โ a single unstable server could stall selects for every other server key.Split into:
c.mu, return on hit and unlock;c.mu, insidesingleflight, re-checking the cache (another goroutine may have cached between our unlock and entering singleflight) and takingc.muonly to insert.DoClientConnectedstill fires exactly once per newly created connection via acreatedflag set only by the dialing goroutine.2. Server object-pool Get/Put balance + dead mutex
handleRequest/handleRequestForFunctionfetchedargvfrom the object pool but leaked it on the nil-codec and Decode-error paths; nowPutit back before returning the error so the pool doesn't degrade to always allocating.sync.MutexfrommethodType/functionType(guarded anumCallscounter removed long ago; never locked; pollutes the type method set).Testing
TestGetCachedClientDialDoesNotBlockOtherKeysโ fails (2s timeout) against the old locked version, passes with the fix; stable under-race -count=3.BenchmarkServerHandleRequest(ReportAllocs) anchors the per-request dispatch path (server/ had no hot-path benchmark). Baseline ~811 ns/op, 809 B/op, 14 allocs/op.client+serversuites pass under-race, excluding two*Pool_SetPlugins_Concurrentraces that pre-exist on master and are unrelated to this change.Not included (flagged for follow-up)
make(map[string]string)forresMetadatainprocessOneRequest(allocated even when unused) โ needs a benchmark and downstreamResMetaDataKeyaudit.protocol.NewMessage()per send/receive โ pooling needs message-lifetime guarantees.