1
0
mirror of https://github.com/redis/go-redis.git synced 2025-12-02 06:22:31 +03:00

try faster approach

This commit is contained in:
Nedyalko Dyakov
2025-10-25 21:34:50 +03:00
parent d43b973b13
commit d34f1e0160

View File

@@ -696,7 +696,9 @@ func (p *ConnPool) putConn(ctx context.Context, cn *Conn, freeTurn bool) {
// Hot path optimization: try fast IN_USE → IDLE transition // Hot path optimization: try fast IN_USE → IDLE transition
// This is a single CAS operation, as fast as the old atomic bool // This is a single CAS operation, as fast as the old atomic bool
sm := cn.GetStateMachine() sm := cn.GetStateMachine()
if !sm.TryTransitionFast(StateInUse, StateIdle) { transitionedToIdle := sm.TryTransitionFast(StateInUse, StateIdle)
if !transitionedToIdle {
// Fast path failed - hook might have changed state (e.g., to UNUSABLE for handoff) // Fast path failed - hook might have changed state (e.g., to UNUSABLE for handoff)
// Keep the state set by the hook and pool the connection anyway // Keep the state set by the hook and pool the connection anyway
currentState := sm.GetState() currentState := sm.GetState()
@@ -705,7 +707,8 @@ func (p *ConnPool) putConn(ctx context.Context, cn *Conn, freeTurn bool) {
// unusable conns are expected to become usable at some point (background process is reconnecting them) // unusable conns are expected to become usable at some point (background process is reconnecting them)
// put them at the opposite end of the queue // put them at the opposite end of the queue
if !cn.IsUsable() { // Optimization: if we just transitioned to IDLE, we know it's usable - skip the check
if !transitionedToIdle && !cn.IsUsable() {
if p.cfg.PoolFIFO { if p.cfg.PoolFIFO {
p.connsMu.Lock() p.connsMu.Lock()
p.idleConns = append(p.idleConns, cn) p.idleConns = append(p.idleConns, cn)