-
Couldn't load subscription status.
- Fork 2.5k
fix(init): conn init should be thread safe #3555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR improves thread safety for connection initialization by adding a new initializing flag to prevent concurrent initialization attempts. The changes introduce synchronized access to initialization state and proper cleanup on initialization failures.
Key changes:
- Adds an
initializingatomic flag to track in-progress initialization - Implements deferred cleanup to reset state on initialization failure
- Adds retry logic with timeout when replacing network connections
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| redis.go | Adds initialization guard with deferred cleanup and reorders initialization steps for thread safety |
| internal/pool/conn.go | Introduces initializing flag with accessor methods and adds retry logic for network connection replacement |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| time.Sleep(100 * time.Millisecond) | ||
| maxRetries-- | ||
| if maxRetries <= 0 { | ||
| return fmt.Errorf("failed to set net conn after %d attempts due to high contention", maxRetries) |
Copilot
AI
Oct 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message uses maxRetries which will always be 0 or negative at this point. Use the original value (20) or calculate the actual number of attempts made instead.
| return fmt.Errorf("failed to set net conn after %d attempts due to high contention", maxRetries) | |
| return fmt.Errorf("failed to set net conn after %d attempts due to high contention", 20) |
| // max retries of 100ms * 20 = 2 second | ||
| maxRetries := 20 | ||
| for cn.IsInitializing() || cn.IsUsed() { | ||
| time.Sleep(100 * time.Millisecond) | ||
| maxRetries-- | ||
| if maxRetries <= 0 { | ||
| return fmt.Errorf("failed to set net conn after %d attempts due to high contention", maxRetries) | ||
| } | ||
| } |
Copilot
AI
Oct 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The retry loop blocks the goroutine with fixed 100ms sleeps, which could cause unnecessary delays. Consider using exponential backoff or a more efficient synchronization mechanism like a condition variable.
|
|
||
| // SetNetConnAndInitConn replaces the underlying connection and executes the initialization. | ||
| func (cn *Conn) SetNetConnAndInitConn(ctx context.Context, netConn net.Conn) error { | ||
| // max retries of 100ms * 20 = 2 second |
Copilot
AI
Oct 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected 'second' to 'seconds' for grammatical accuracy.
| // max retries of 100ms * 20 = 2 second | |
| // max retries of 100ms * 20 = 2 seconds |
|
closed in favour of #3559 |
conn init should be thread safe