mirror of
https://github.com/redis/go-redis.git
synced 2025-12-02 06:22:31 +03:00
better timeouts
This commit is contained in:
@@ -69,7 +69,7 @@ type Conn struct {
|
|||||||
|
|
||||||
// Handoff metadata - managed separately from state machine
|
// Handoff metadata - managed separately from state machine
|
||||||
// These are atomic for lock-free access during handoff operations
|
// These are atomic for lock-free access during handoff operations
|
||||||
handoffStateAtomic atomic.Value // stores *HandoffState
|
handoffStateAtomic atomic.Value // stores *HandoffState
|
||||||
handoffRetriesAtomic atomic.Uint32 // retry counter
|
handoffRetriesAtomic atomic.Uint32 // retry counter
|
||||||
|
|
||||||
pooled bool
|
pooled bool
|
||||||
@@ -536,10 +536,14 @@ func (cn *Conn) SetNetConnAndInitConn(ctx context.Context, netConn net.Conn) err
|
|||||||
// Wait for and transition to INITIALIZING state - this prevents concurrent initializations
|
// Wait for and transition to INITIALIZING state - this prevents concurrent initializations
|
||||||
// Valid from states: CREATED (first init), IDLE (reconnect), UNUSABLE (handoff/reauth)
|
// Valid from states: CREATED (first init), IDLE (reconnect), UNUSABLE (handoff/reauth)
|
||||||
// If another goroutine is initializing, we'll wait for it to finish
|
// If another goroutine is initializing, we'll wait for it to finish
|
||||||
// Add 1ms timeout to prevent indefinite blocking
|
// if the context has a deadline, use that, otherwise use the connection read (relaxed) timeout
|
||||||
waitCtx, cancel := context.WithTimeout(ctx, time.Millisecond)
|
// which should be set during handoff. If it is not set, use a 5 second default
|
||||||
|
deadline, ok := ctx.Deadline()
|
||||||
|
if !ok {
|
||||||
|
deadline = time.Now().Add(cn.getEffectiveReadTimeout(5 * time.Second))
|
||||||
|
}
|
||||||
|
waitCtx, cancel := context.WithDeadline(ctx, deadline)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
finalState, err := cn.stateMachine.AwaitAndTransition(
|
finalState, err := cn.stateMachine.AwaitAndTransition(
|
||||||
waitCtx,
|
waitCtx,
|
||||||
[]ConnState{StateCreated, StateIdle, StateUnusable},
|
[]ConnState{StateCreated, StateIdle, StateUnusable},
|
||||||
|
|||||||
4
redis.go
4
redis.go
@@ -394,8 +394,8 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
|
|||||||
if finalState == pool.StateInitializing {
|
if finalState == pool.StateInitializing {
|
||||||
// Another goroutine is initializing - WAIT for it to complete
|
// Another goroutine is initializing - WAIT for it to complete
|
||||||
// Use AwaitAndTransition to wait for IDLE or IN_USE state
|
// Use AwaitAndTransition to wait for IDLE or IN_USE state
|
||||||
// Add 1ms timeout to prevent indefinite blocking
|
// use DialTimeout as the timeout for the wait
|
||||||
waitCtx, cancel := context.WithTimeout(ctx, time.Millisecond)
|
waitCtx, cancel := context.WithTimeout(ctx, c.opt.DialTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
finalState, err := cn.GetStateMachine().AwaitAndTransition(
|
finalState, err := cn.GetStateMachine().AwaitAndTransition(
|
||||||
|
|||||||
Reference in New Issue
Block a user