⚡ perf: Narrow client user hook lock scope safely#4375
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves Fiber’s HTTP client concurrency by reducing lock contention in the request/response hook pipeline, allowing multiple requests using the same client to execute hook logic concurrently.
Changes:
- Narrow
Client.muscope inpreHooks/afterHooksto only reading hook slice references (instead of holding the lock during hook execution). - Add tests asserting that
preHooksandafterHooksdo not serialize concurrent executions behind the client lock.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| client/core.go | Switch hook execution to use a short RLock window rather than holding the client lock while running hooks. |
| client/core_test.go | Add concurrency-focused tests to verify hook execution isn’t serialized by the client lock. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4375 +/- ##
==========================================
- Coverage 91.39% 91.33% -0.06%
==========================================
Files 132 132
Lines 13098 13113 +15
==========================================
+ Hits 11971 11977 +6
- Misses 710 717 +7
- Partials 417 419 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@copilot The lock-narrowing looks good and CI is green, but one issue from the automated review still needs to be fixed before this is safe to merge. Snapshotting the pre/response hook slices by plain assignment only copies the slice header; the backing array stays shared. A concurrent Please clone the slices while holding the lock, then release and iterate the clones: reqHooks := slices.Clone(c.userRequestHooks)
resHooks := slices.Clone(c.userResponseHooks)Re-run the affected tests with |
Description
Client request/response hooks were executed while holding the client write lock, forcing concurrent requests on a shared client to serialize through hook execution. This change narrows the lock scope safely by cloning user hook slices under lock, then releasing the lock before iterating those cloned user hooks, while keeping builtin hooks protected because they still touch shared fasthttp-backed client state during request/response preparation.
Lock scope
userRequestHooksanduserResponseHooksunder lock withslices.Clone(...).Concurrency coverage
preHooksandafterHooksexecutions are not blocked on user hook execution.-raceto verify the slice-sharing race is removed.Changes introduced
Type of change
Please delete options that are not relevant.
Checklist
Before you submit your pull request, please make sure you meet these requirements:
/docs/directory for Fiber's documentation.Commit formatting
Please use emojis in commit messages for an easy way to identify the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md