MCPcopy
hub / github.com/redis/go-redis / CompareAndSwapUsed

Method CompareAndSwapUsed

internal/pool/conn.go:327–346  ·  view source on GitHub ↗

Used - State machine based implementation CompareAndSwapUsed atomically compares and swaps the used flag (lock-free). This method is kept for backwards compatibility. This is the preferred method for acquiring a connection from the pool, as it ensures that only one goroutine marks the connection as

(old, new bool)

Source from the content-addressed store, hash-verified

325// Returns true if the swap was successful (old value matched), false otherwise.
326// Deprecated: Use GetStateMachine().TryTransition() directly for better state management.
327func (cn *Conn) CompareAndSwapUsed(old, new bool) bool {
328 if old == new {
329 // No change needed
330 currentState := cn.stateMachine.GetState()
331 currentUsed := (currentState == StateInUse)
332 return currentUsed == old
333 }
334
335 if !old && new {
336 // Acquiring: IDLE → IN_USE
337 // Use predefined slice to avoid allocation
338 _, err := cn.stateMachine.TryTransition(validFromCreatedOrIdle, StateInUse)
339 return err == nil
340 } else {
341 // Releasing: IN_USE → IDLE
342 // Use predefined slice to avoid allocation
343 _, err := cn.stateMachine.TryTransition(validFromInUse, StateIdle)
344 return err == nil
345 }
346}
347
348// IsUsed returns true if the connection is currently in use (lock-free).
349//

Callers 4

TestReAuthOnlyWhenIdleFunction · 0.95
TestConn_UsedMethodsFunction · 0.95

Calls 2

TryTransitionMethod · 0.80
GetStateMethod · 0.45

Tested by 4

TestReAuthOnlyWhenIdleFunction · 0.76
TestConn_UsedMethodsFunction · 0.76