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.

@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.

ndyakov and others added 5 commits October 25, 2025 21:44
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@ndyakov ndyakov force-pushed the ndyakov/state-machine-conn branch from 4a452b0 to b55cdad Compare October 30, 2025 16:23
@ndyakov ndyakov force-pushed the ndyakov/state-machine-conn branch from b55cdad to fc2da24 Compare October 30, 2025 16:27
@ndyakov ndyakov force-pushed the ndyakov/state-machine-conn branch from 1642bb5 to a7b2f91 Compare October 30, 2025 17:11
@ndyakov ndyakov force-pushed the ndyakov/state-machine-conn branch from a7b2f91 to d207749 Compare October 30, 2025 17:19
@ndyakov ndyakov force-pushed the ndyakov/state-machine-conn branch from 68199f2 to d91800d Compare October 30, 2025 20:20
@ndyakov ndyakov requested a review from Copilot November 5, 2025 15:29
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 4 comments.


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

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.

Overall looks good.
There aside from the other comments, can we preallocate all errors.New(...) too?

attempt++
}
}
_, err := stateMachine.AwaitAndTransition(ctx, []pool.ConnState{pool.StateIdle}, pool.StateUnusable)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we preallocate this case too?

// Trying to make usable - transition from UNUSABLE to IDLE
// This should only work from UNUSABLE or INITIALIZING states
_, err := cn.stateMachine.TryTransition(
[]ConnState{StateInitializing, StateUnusable},
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, can we preallocate?

// Trying to make unusable - transition from IDLE to UNUSABLE
// This is typically for acquiring the connection for background operations
_, err := cn.stateMachine.TryTransition(
[]ConnState{StateIdle},
Copy link
Contributor

Choose a reason for hiding this comment

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

-||-

defer cancel()
finalState, err := cn.stateMachine.AwaitAndTransition(
waitCtx,
[]ConnState{StateCreated, StateIdle, StateUnusable},
Copy link
Contributor

Choose a reason for hiding this comment

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

-||-

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.

3 participants