Skip to content

perf(client): release xClient mutex before dialing; server pool/mutex cleanups - #940

Merged
smallnest merged 2 commits into
masterfrom
perf/xclient-dial-no-lock
Jul 2, 2026
Merged

smallnest merged 2 commits into
masterfrom
perf/xclient-dial-no-lock

Conversation

@smallnest

Copy link
Copy Markdown
Owner

Summary

Two performance fixes surfaced by a concurrency + allocation review of the recently-split client/server code.

1. getCachedClient no longer holds c.mu across the dial (main fix)

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:

  • fast path: cache lookup under c.mu, return on hit and unlock;
  • slow path: dial outside c.mu, inside singleflight, re-checking the cache (another goroutine may have cached between our unlock and entering singleflight) and taking c.mu only to insert.

DoClientConnected still fires exactly once per newly created connection via a created flag set only by the dialing goroutine.

2. Server object-pool Get/Put balance + dead mutex

  • handleRequest/handleRequestForFunction fetched argv from the object pool but leaked it on the nil-codec and Decode-error paths; now Put it back before returning the error so the pool doesn't degrade to always allocating.
  • Removed the unused embedded sync.Mutex from methodType/functionType (guarded a numCalls counter 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.
  • New BenchmarkServerHandleRequest (ReportAllocs) anchors the per-request dispatch path (server/ had no hot-path benchmark). Baseline ~811 ns/op, 809 B/op, 14 allocs/op.
  • Full client + server suites pass under -race, excluding two *Pool_SetPlugins_Concurrent races that pre-exist on master and are unrelated to this change.

Not included (flagged for follow-up)

  • Per-request make(map[string]string) for resMetadata in processOneRequest (allocated even when unused) โ€” needs a benchmark and downstream ResMetaDataKey audit.
  • Client-side protocol.NewMessage() per send/receive โ€” pooling needs message-lifetime guarantees.

chaoyuepan 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.
@smallnest
smallnest merged commit 30fb9b7 into master Jul 2, 2026
1 check passed
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