feat: add SafeContext()/SetSafeContext() for concurrency-safe user-defined context - #760
Merged
Merged
Conversation
…afe user-defined context
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #760 +/- ##
==========================================
+ Coverage 84.38% 84.43% +0.04%
==========================================
Files 22 22
Lines 2402 2422 +20
==========================================
+ Hits 2027 2045 +18
- Misses 255 257 +2
Partials 120 120
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
- TestSafeContext in gnet_test.go: verifies initial nil state, round-trip correctness across rotating concrete types (including nil) driven by OnTraffic, concurrent background-goroutine safety, non-interference with Context(), and safety when called from OnClose before release(). - TestClientSafeContext in client_test.go: verifies DialContext's ctx argument is immediately reflected by both Context() and SafeContext(), plus the same rotating-type/concurrent-access pattern on the client side. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Port 10000 is used by netdisk_s (ndmp) on some macOS systems, causing spurious bind: address already in use failures. Switch the four affected tests (TestDisconnectedAsyncWrite, TestUDPSendtoServer, TestStreamProxyServer, TestUDPProxyServer) to port 19999 instead. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Reference Context()/SetContext() directly when describing the concurrency-safe SafeContext()/SetSafeContext() variants. - Note that LocalAddr()/RemoteAddr() return values must be copied via net.Addr.String() if used outside of EventHandler methods. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- TestClientSafeContext: assert Context()/SafeContext() reflect the dial-time ctx directly inside OnOpen (guaranteed to run before any OnTraffic mutates it), instead of racily checking from the test goroutine after DialContext returns. Also verify SafeContext() becomes nil once the connection is released. - TestSafeContext: verify SafeContext() becomes nil once the connection is released, in addition to the existing OnOpen check that it starts out nil before the first SetSafeContext call. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
panjf2000
added a commit
that referenced
this pull request
Jul 3, 2026
* feat: add SafeContext()/SetSafeContext() for concurrency-safe user-defined context (#760) Add SafeContext() and SetSafeContext(ctx any) to the Conn interface, backed by atomic.Pointer[any], allowing goroutines other than the owning event-loop to safely read/write a connection's user-defined context without racing against Context()/SetContext(), which remain event-loop-only. - Implement safeCtx atomic.Pointer[any] field and accessors in connection_unix.go/connection_windows.go; wire up initial context on Windows via SetSafeContext at connection construction; reset safeCtx to nil in release(). - Clarify Conn interface doc comments for SafeContext/SetSafeContext (reference Context/SetContext directly) and for LocalAddr/RemoteAddr (note callers must copy the returned net.Addr via String() if used outside EventHandler). - Add TestSafeContext (gnet_test.go) and TestClientSafeContext (client_test.go) covering: nil before first SetSafeContext, rotating concrete types (including nil) via OnTraffic, concurrent SafeContext reads from a background goroutine, no interference with Context()/SetContext(), correctness immediately after Dial/OnOpen, safety when called from OnClose, and reset to nil after release(). - Avoid port 10000 in existing tests (conflicts with a common local service on some machines), switching to 19999. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: gunli <gunli@tencent.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.
add SafeContext() and SetSafeContext() to support concurrency-safe user-defined context
1. Are you opening this pull request for bug-fix, optimization or new feature?
New feature.
2. Please describe how these code changes achieve your intention.
SafeContext() (ctx any)andSetSafeContext(ctx any)to Conn;safeCtx atomic.Pointer[any]to conn and implementSafeContext() (ctx any)andSetSafeContext(ctx any)in connection_unix.go and connection_windows.go;ctxandsafeCtxwhen conn is released;ctxandsafeCtxin client_unix.go and eventloop_unix.go.3. Please link to the relevant issues (if any).
4. What documentation changes (if any) need to be made/updated because of this PR?
No
4. Checklist