From f52ab34e311af7dabb91bd0167de1430f40ddf20 Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Sun, 26 Oct 2025 00:50:09 +0300 Subject: [PATCH] add comments --- internal/pool/conn.go | 21 ++++++++++++++------- internal/pool/pool.go | 4 ++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/internal/pool/conn.go b/internal/pool/conn.go index f424aa83..d7d7fa89 100644 --- a/internal/pool/conn.go +++ b/internal/pool/conn.go @@ -664,14 +664,17 @@ func (cn *Conn) GetStateMachine() *ConnStateMachine { // TryAcquire attempts to acquire the connection for use. // This is an optimized inline method for the hot path (Get operation). // -// It tries to transition from IDLE → IN_USE or CREATED → IN_USE. +// It tries to transition from IDLE -> IN_USE or CREATED -> IN_USE. // Returns true if the connection was successfully acquired, false otherwise. // // Performance: This is faster than calling GetStateMachine() + TryTransitionFast() -// because it avoids the extra pointer dereference and allows better inlining. +// +// NOTE: We directly access cn.stateMachine.state here instead of using the state machine's +// methods. This breaks encapsulation but is necessary for performance. +// The IDLE->IN_USE and CREATED->IN_USE transitions don't need +// waiter notification, and benchmarks show 1-3% improvement. If the state machine ever +// needs to notify waiters on these transitions, update this to use TryTransitionFast(). func (cn *Conn) TryAcquire() bool { - // Inline the hot path - try IDLE → IN_USE first (most common case) - // Then try CREATED → IN_USE (new connections) // The || operator short-circuits, so only 1 CAS in the common case return cn.stateMachine.state.CompareAndSwap(uint32(StateIdle), uint32(StateInUse)) || cn.stateMachine.state.CompareAndSwap(uint32(StateCreated), uint32(StateInUse)) @@ -680,11 +683,15 @@ func (cn *Conn) TryAcquire() bool { // Release releases the connection back to the pool. // This is an optimized inline method for the hot path (Put operation). // -// It tries to transition from IN_USE → IDLE. +// It tries to transition from IN_USE -> IDLE. // Returns true if the connection was successfully released, false otherwise. // -// Performance: This is faster than calling GetStateMachine() + TryTransitionFast() -// because it avoids the extra pointer dereference and allows better inlining. +// Performance: This is faster than calling GetStateMachine() + TryTransitionFast(). +// +// NOTE: We directly access cn.stateMachine.state here instead of using the state machine's +// methods. This breaks encapsulation but is necessary for performance. +// If the state machine ever needs to notify waiters +// on this transition, update this to use TryTransitionFast(). func (cn *Conn) Release() bool { // Inline the hot path - single CAS operation return cn.stateMachine.state.CompareAndSwap(uint32(StateInUse), uint32(StateIdle)) diff --git a/internal/pool/pool.go b/internal/pool/pool.go index f4508b14..23fc0d14 100644 --- a/internal/pool/pool.go +++ b/internal/pool/pool.go @@ -286,7 +286,7 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) { return nil, ErrClosed } - if p.cfg.MaxActiveConns > 0 && p.poolSize.Load() >= int32(p.cfg.MaxActiveConns) { + if p.cfg.MaxActiveConns > 0 && p.poolSize.Load() >= p.cfg.MaxActiveConns { return nil, ErrPoolExhausted } @@ -301,7 +301,7 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) { // when first used. Do NOT transition to IDLE here - that happens after initialization completes. // The state machine flow is: CREATED → INITIALIZING (in initConn) → IDLE (after init success) - if p.cfg.MaxActiveConns > 0 && p.poolSize.Load() > int32(p.cfg.MaxActiveConns) { + if p.cfg.MaxActiveConns > 0 && p.poolSize.Load() > p.cfg.MaxActiveConns { _ = cn.Close() return nil, ErrPoolExhausted }