Skip to content

Commit 26923a2

Browse files
committed
refactor for readibility
1 parent 2e47e39 commit 26923a2

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed

hitless/pool_hook.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -138,40 +138,40 @@ func (ph *PoolHook) OnGet(ctx context.Context, conn *pool.Conn, isNewConn bool)
138138
// OnPut is called when a connection is returned to the pool
139139
func (ph *PoolHook) OnPut(ctx context.Context, conn *pool.Conn) (shouldPool bool, shouldRemove bool, err error) {
140140
// first check if we should handoff for faster rejection
141-
if conn.ShouldHandoff() {
142-
// check pending handoff to not queue the same connection twice
143-
_, hasPendingHandoff := ph.pending.Load(conn.GetID())
144-
if !hasPendingHandoff {
145-
// Check for empty endpoint first (synchronous check)
146-
if conn.GetHandoffEndpoint() == "" {
147-
conn.ClearHandoffState()
148-
} else {
149-
if err := ph.queueHandoff(conn); err != nil {
150-
// Failed to queue handoff, remove the connection
151-
internal.Logger.Printf(ctx, "Failed to queue handoff: %v", err)
152-
return false, true, nil // Don't pool, remove connection, no error to caller
153-
}
141+
if !conn.ShouldHandoff() {
142+
// Default behavior (no handoff): pool the connection
143+
return true, false, nil
144+
}
154145

155-
// Check if handoff was already processed by a worker before we can mark it as queued
156-
if !conn.ShouldHandoff() {
157-
// Handoff was already processed - this is normal and the connection should be pooled
158-
return true, false, nil
159-
}
146+
// check pending handoff to not queue the same connection twice
147+
_, hasPendingHandoff := ph.pending.Load(conn.GetID())
148+
if hasPendingHandoff {
149+
// Default behavior (pending handoff): pool the connection
150+
return true, false, nil
151+
}
160152

161-
if err := conn.MarkQueuedForHandoff(); err != nil {
162-
// If marking fails, check if handoff was processed in the meantime
163-
if !conn.ShouldHandoff() {
164-
// Handoff was processed - this is normal, pool the connection
165-
return true, false, nil
166-
}
167-
// Other error - remove the connection
168-
return false, true, nil
169-
}
170-
return true, false, nil
171-
}
153+
if err := ph.queueHandoff(conn); err != nil {
154+
// Failed to queue handoff, remove the connection
155+
internal.Logger.Printf(ctx, "Failed to queue handoff: %v", err)
156+
// Don't pool, remove connection, no error to caller
157+
return false, true, nil
158+
}
159+
160+
// Check if handoff was already processed by a worker before we can mark it as queued
161+
if !conn.ShouldHandoff() {
162+
// Handoff was already processed - this is normal and the connection should be pooled
163+
return true, false, nil
164+
}
165+
166+
if err := conn.MarkQueuedForHandoff(); err != nil {
167+
// If marking fails, check if handoff was processed in the meantime
168+
if !conn.ShouldHandoff() {
169+
// Handoff was processed - this is normal, pool the connection
170+
return true, false, nil
172171
}
172+
// Other error - remove the connection
173+
return false, true, nil
173174
}
174-
// Default: pool the connection
175175
return true, false, nil
176176
}
177177

internal/pool/conn.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,6 @@ func (cn *Conn) MarkQueuedForHandoff() error {
409409
return nil
410410
}
411411

412-
// RestoreHandoffState restores the handoff state after a failed handoff (lock-free).
413-
func (cn *Conn) RestoreHandoffState() {
414-
// Restore shouldHandoff flag for retry
415-
cn.shouldHandoffAtomic.Store(true)
416-
// Keep usable=false to prevent the connection from being used until handoff succeeds
417-
cn.setUsable(false)
418-
}
419-
420412
// ShouldHandoff returns true if the connection needs to be handed off (lock-free).
421413
func (cn *Conn) ShouldHandoff() bool {
422414
return cn.shouldHandoff()

0 commit comments

Comments
 (0)