Refactor/modularize resilientws#13
Open
fakhrilainur wants to merge 11 commits into
Open
Conversation
chore: remove unused eventhandlers
refactor: simplify websocket config and heartbeat handling with optio…
feat: implement intelligent backoff strategy with connection stabilit…
chore: replace intercafe{} with any
Fix some bugs
fix: prevent goroutine leaks
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.
Code Changes Summary: Refactor & Concurrent Safety Fixes
Overview
This change refactors the monolithic
resilientws.go(~1000 lines) into 7 focused, single-responsibility modules while simultaneously fixing critical concurrent write race conditions and lock contention issues.Why This Change?
1. Code Organization & Maintainability
resilientws.gomixed concerns: connection management, message I/O, backoff logic, heartbeat, logging, and state management2. Concurrent Safety
Send,SendJSON,processMessageQueue,heartbeat) could race on writesr.mu.Lock()held during I/O operations blocked all readers/writers for 100+ msKey Changes
New Files Created
types.gologger.gostate.gobackoff.goconnection.goheartbeat.goio.goCritical Bug Fixes
1. Write-side Race Condition (NEW:
writeMuin types.go, io.go)Impact: Prevents data corruption from concurrent writes to websocket connection.
2. TOCTOU (Time-of-Check-Time-of-Use) Race in Read/Write Methods
Impact: Eliminates nil-pointer dereferences and race windows.
3. Lock Held During I/O in Close/CloseAndReconnect (resilientws.go)
Impact: Prevents reader/writer goroutines from blocking on every reconnect.
4. Safe Type Assertion in emitEvent (resilientws.go:286)
Impact: Prevents runtime panic from type assertion failures.
5. Backoff Integer Overflow (backoff.go:33-42)
Impact: Prevents backoff wrap-around from integer overflow.
6. Reader Exit on Close Message (io.go)
Impact: Reader goroutine properly exits on server-initiated close.
7. Subscribe Retry Off-by-One (connection.go)
Impact: Retry termination logic checks correct backoff value.
Testing
Files Modified
resilientws.goreduced from ~904 lines to ~325 linesBackward Compatibility
✅ Fully backward compatible all public APIs unchanged, internal refactoring only.