1
0
mirror of https://github.com/redis/go-redis.git synced 2025-09-05 20:24:00 +03:00

refactor for readibility

This commit is contained in:
Nedyalko Dyakov
2025-08-21 16:08:57 +03:00
parent 2e47e39a97
commit 26923a2357
2 changed files with 33 additions and 41 deletions

View File

@@ -138,40 +138,40 @@ func (ph *PoolHook) OnGet(ctx context.Context, conn *pool.Conn, isNewConn bool)
// OnPut is called when a connection is returned to the pool // OnPut is called when a connection is returned to the pool
func (ph *PoolHook) OnPut(ctx context.Context, conn *pool.Conn) (shouldPool bool, shouldRemove bool, err error) { func (ph *PoolHook) OnPut(ctx context.Context, conn *pool.Conn) (shouldPool bool, shouldRemove bool, err error) {
// first check if we should handoff for faster rejection // first check if we should handoff for faster rejection
if conn.ShouldHandoff() { if !conn.ShouldHandoff() {
// check pending handoff to not queue the same connection twice // Default behavior (no handoff): pool the connection
_, hasPendingHandoff := ph.pending.Load(conn.GetID()) return true, false, nil
if !hasPendingHandoff { }
// Check for empty endpoint first (synchronous check)
if conn.GetHandoffEndpoint() == "" { // check pending handoff to not queue the same connection twice
conn.ClearHandoffState() _, hasPendingHandoff := ph.pending.Load(conn.GetID())
} else { if hasPendingHandoff {
if err := ph.queueHandoff(conn); err != nil { // Default behavior (pending handoff): pool the connection
// Failed to queue handoff, remove the connection return true, false, nil
internal.Logger.Printf(ctx, "Failed to queue handoff: %v", err) }
return false, true, nil // Don't pool, remove connection, no error to caller
} if err := ph.queueHandoff(conn); err != nil {
// Failed to queue handoff, remove the connection
// Check if handoff was already processed by a worker before we can mark it as queued internal.Logger.Printf(ctx, "Failed to queue handoff: %v", err)
if !conn.ShouldHandoff() { // Don't pool, remove connection, no error to caller
// Handoff was already processed - this is normal and the connection should be pooled return false, true, nil
return true, false, nil }
}
// Check if handoff was already processed by a worker before we can mark it as queued
if err := conn.MarkQueuedForHandoff(); err != nil { if !conn.ShouldHandoff() {
// If marking fails, check if handoff was processed in the meantime // Handoff was already processed - this is normal and the connection should be pooled
if !conn.ShouldHandoff() { return true, false, nil
// Handoff was processed - this is normal, pool the connection }
return true, false, nil
} if err := conn.MarkQueuedForHandoff(); err != nil {
// Other error - remove the connection // If marking fails, check if handoff was processed in the meantime
return false, true, nil if !conn.ShouldHandoff() {
} // Handoff was processed - this is normal, pool the connection
return true, false, nil return true, false, nil
} }
} // Other error - remove the connection
return false, true, nil
} }
// Default: pool the connection
return true, false, nil return true, false, nil
} }

View File

@@ -409,14 +409,6 @@ func (cn *Conn) MarkQueuedForHandoff() error {
return nil return nil
} }
// RestoreHandoffState restores the handoff state after a failed handoff (lock-free).
func (cn *Conn) RestoreHandoffState() {
// Restore shouldHandoff flag for retry
cn.shouldHandoffAtomic.Store(true)
// Keep usable=false to prevent the connection from being used until handoff succeeds
cn.setUsable(false)
}
// ShouldHandoff returns true if the connection needs to be handed off (lock-free). // ShouldHandoff returns true if the connection needs to be handed off (lock-free).
func (cn *Conn) ShouldHandoff() bool { func (cn *Conn) ShouldHandoff() bool {
return cn.shouldHandoff() return cn.shouldHandoff()