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)
| 325 | // Returns true if the swap was successful (old value matched), false otherwise. |
| 326 | // Deprecated: Use GetStateMachine().TryTransition() directly for better state management. |
| 327 | func (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 | // |