Skip to content

Commit 0752aec

Browse files
committed
Fix broken initialization of idle connections
1 parent f1c8884 commit 0752aec

File tree

5 files changed

+12
-4
lines changed

5 files changed

+12
-4
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

internal/pool/conn.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,20 +699,22 @@ func (cn *Conn) GetStateMachine() *ConnStateMachine {
699699
// TryAcquire attempts to acquire the connection for use.
700700
// This is an optimized inline method for the hot path (Get operation).
701701
//
702-
// It tries to transition from IDLE -> IN_USE or CREATED -> IN_USE.
702+
// It tries to transition from IDLE -> IN_USE or CREATED -> CREATED.
703703
// Returns true if the connection was successfully acquired, false otherwise.
704+
// The CREATED->CREATED is done so we can keep the state correct for later
705+
// initialization of the connection in initConn.
704706
//
705707
// Performance: This is faster than calling GetStateMachine() + TryTransitionFast()
706708
//
707709
// NOTE: We directly access cn.stateMachine.state here instead of using the state machine's
708710
// methods. This breaks encapsulation but is necessary for performance.
709-
// The IDLE->IN_USE and CREATED->IN_USE transitions don't need
711+
// The IDLE->IN_USE and CREATED->CREATED transitions don't need
710712
// waiter notification, and benchmarks show 1-3% improvement. If the state machine ever
711713
// needs to notify waiters on these transitions, update this to use TryTransitionFast().
712714
func (cn *Conn) TryAcquire() bool {
713715
// The || operator short-circuits, so only 1 CAS in the common case
714716
return cn.stateMachine.state.CompareAndSwap(uint32(StateIdle), uint32(StateInUse)) ||
715-
cn.stateMachine.state.CompareAndSwap(uint32(StateCreated), uint32(StateInUse))
717+
cn.stateMachine.state.CompareAndSwap(uint32(StateCreated), uint32(StateCreated))
716718
}
717719

718720
// Release releases the connection back to the pool.

maintnotifications/e2e/config_parser_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ func (cf *ClientFactory) Create(key string, options *CreateClientOptions) (redis
319319
}
320320

321321
var client redis.UniversalClient
322+
var opts interface{}
322323

323324
// Determine if this is a cluster configuration
324325
if len(cf.config.Endpoints) > 1 || cf.isClusterEndpoint() {
@@ -349,6 +350,7 @@ func (cf *ClientFactory) Create(key string, options *CreateClientOptions) (redis
349350
}
350351
}
351352

353+
opts = clusterOptions
352354
client = redis.NewClusterClient(clusterOptions)
353355
} else {
354356
// Create single client
@@ -379,9 +381,14 @@ func (cf *ClientFactory) Create(key string, options *CreateClientOptions) (redis
379381
}
380382
}
381383

384+
opts = clientOptions
382385
client = redis.NewClient(clientOptions)
383386
}
384387

388+
if err := client.Ping(context.Background()).Err(); err != nil {
389+
return nil, fmt.Errorf("failed to connect to Redis: %w\nOptions: %+v", err, opts)
390+
}
391+
385392
// Store the client
386393
cf.clients[key] = client
387394

@@ -832,7 +839,6 @@ func (m *TestDatabaseManager) DeleteDatabase(ctx context.Context) error {
832839
return fmt.Errorf("failed to trigger database deletion: %w", err)
833840
}
834841

835-
836842
// Wait for deletion to complete
837843
status, err := m.faultInjector.WaitForAction(ctx, resp.ActionID,
838844
WithMaxWaitTime(2*time.Minute),

0 commit comments

Comments
 (0)