Skip to content

Conversation

@ndyakov
Copy link
Member

@ndyakov ndyakov commented Oct 22, 2025

conn init should be thread safe

@ndyakov ndyakov requested a review from Copilot October 22, 2025 13:37
Copy link
Contributor

Copilot AI left a 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 initializing atomic 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)
Copy link

Copilot AI Oct 22, 2025

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.

Suggested change
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)

Copilot uses AI. Check for mistakes.
Comment on lines +504 to +512
// 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)
}
}
Copy link

Copilot AI Oct 22, 2025

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.

Copilot uses AI. Check for mistakes.

// 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
Copy link

Copilot AI Oct 22, 2025

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.

Suggested change
// max retries of 100ms * 20 = 2 second
// max retries of 100ms * 20 = 2 seconds

Copilot uses AI. Check for mistakes.
@ndyakov
Copy link
Member Author

ndyakov commented Oct 24, 2025

closed in favour of #3559

@ndyakov ndyakov closed this Oct 24, 2025
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.

2 participants