Skip to content

Conversation

@ndyakov
Copy link
Member

@ndyakov ndyakov commented Oct 24, 2025

  • Add connection state tracking instead of multiple flags
  • Resolve race conditions on handoff
  • Improve pool performance

This PR fixes issues related to us having multiple different atomic flags in the pool connections for handoff, usability, etc. Since I tried to slightly changed the get and set operations of the pool, this correctness (which negatively impacted the performance in the first place) does not have real world drawbacks and improves developer experience.

Also fixes:

@ndyakov ndyakov requested a review from Copilot October 24, 2025 12:07
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 refactors connection management to use a state machine architecture, replacing the previous boolean flags (usable, Inited, used) with a formal state machine that provides better concurrency control and clearer state transitions.

Key Changes:

  • Introduced a new ConnStateMachine with explicit states: CREATED → INITIALIZING → IDLE ⇄ IN_USE, with UNUSABLE for handoff/reauth operations
  • Replaced atomic boolean flags with atomic state transitions using CAS operations and FIFO waiting queues
  • Updated connection initialization logic to handle concurrent initialization attempts properly

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
internal/pool/conn_state.go New file implementing the connection state machine with FIFO waiting queue
internal/pool/conn_state_test.go Comprehensive tests for state machine transitions and concurrency
internal/pool/conn.go Refactored connection to use state machine instead of boolean flags
internal/pool/pool.go Updated pool operations to use state machine transitions
internal/pool/pool_single.go Added comments explaining SingleConnPool's non-thread-safe nature
redis.go Refactored initConn to handle state transitions and concurrent initialization
maintnotifications/pool_hook.go Updated to check handoff state before usability
maintnotifications/pool_hook_test.go Updated tests to use state machine API
maintnotifications/handoff_worker.go Added call to clear handoff state before closing
maintnotifications/errors.go Added new error type and documentation comments
internal/auth/streaming/pool_hook.go Simplified reauth logic using state machine's AwaitAndTransition
internal/auth/streaming/pool_hook_state_test.go New tests verifying reauth respects state machine

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@ndyakov ndyakov requested a review from Copilot October 25, 2025 00:33
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

Copilot reviewed 15 out of 15 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- this way a waiter that timesout at the same time
a releaser is releasing, won't throw token. the releaser
will fail to notify and will pick another waiter.

this hybrid approach should be faster than channels and maintains FIFO
jit-ci[bot]

This comment was marked as off-topic.

ofekshenawa
ofekshenawa previously approved these changes Nov 11, 2025
Copy link
Collaborator

@ofekshenawa ofekshenawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@htemelski-redis htemelski-redis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, 2 minor comments

@ndyakov ndyakov dismissed stale reviews from htemelski-redis and ofekshenawa via 9e24679 November 11, 2025 11:25
@ndyakov ndyakov requested a review from Copilot November 11, 2025 12:11
Copilot finished reviewing on behalf of ndyakov November 11, 2025 12:14
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

Copilot reviewed 34 out of 37 changed files in this pull request and generated 9 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ndyakov ndyakov merged commit 042610b into master Nov 11, 2025
22 checks passed
@ndyakov ndyakov deleted the ndyakov/state-machine-conn branch November 11, 2025 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misleading error log in pool dialConn if DialerRetryTimeout reached Connection pool issues after upgrading to v9.16.0

4 participants