From f8feb0ed4e80e16c4950fff06f6689a7d190d698 Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Sat, 25 Oct 2025 22:52:37 +0300 Subject: [PATCH] fix linter issues --- internal/pool/export_test.go | 2 +- internal/pool/pool.go | 38 ++++++++++++++++-------------------- internal/pool/semaphore.go | 13 +----------- 3 files changed, 19 insertions(+), 34 deletions(-) diff --git a/internal/pool/export_test.go b/internal/pool/export_test.go index a38e33ea..a162d065 100644 --- a/internal/pool/export_test.go +++ b/internal/pool/export_test.go @@ -20,5 +20,5 @@ func (p *ConnPool) CheckMinIdleConns() { } func (p *ConnPool) QueueLen() int { - return int(p.semaphore.len()) + return int(p.semaphore.count.Load()) } diff --git a/internal/pool/pool.go b/internal/pool/pool.go index e09e6c55..f95d276e 100644 --- a/internal/pool/pool.go +++ b/internal/pool/pool.go @@ -486,19 +486,20 @@ func (p *ConnPool) getConn(ctx context.Context) (*Conn, error) { } // Process connection using the hooks system + // Combine error and rejection checks to reduce branches if hookManager != nil { acceptConn, err := hookManager.ProcessOnGet(ctx, cn, false) - if err != nil { - internal.Logger.Printf(ctx, "redis: connection pool: failed to process idle connection by hook: %v", err) - _ = p.CloseConn(cn) - continue - } - if !acceptConn { - internal.Logger.Printf(ctx, "redis: connection pool: conn[%d] rejected by hook, returning to pool", cn.GetID()) - // Return connection to pool without freeing the turn that this Get() call holds. - // We use putConnWithoutTurn() to run all the Put hooks and logic without freeing a turn. - p.putConnWithoutTurn(ctx, cn) - cn = nil + if err != nil || !acceptConn { + if err != nil { + internal.Logger.Printf(ctx, "redis: connection pool: failed to process idle connection by hook: %v", err) + _ = p.CloseConn(cn) + } else { + internal.Logger.Printf(ctx, "redis: connection pool: conn[%d] rejected by hook, returning to pool", cn.GetID()) + // Return connection to pool without freeing the turn that this Get() call holds. + // We use putConnWithoutTurn() to run all the Put hooks and logic without freeing a turn. + p.putConnWithoutTurn(ctx, cn) + cn = nil + } continue } } @@ -547,11 +548,12 @@ func (p *ConnPool) waitTurn(ctx context.Context) error { start := time.Now() err := p.semaphore.acquire(ctx, p.cfg.PoolTimeout) - if err == nil { + switch err { + case nil: // Successfully acquired after waiting p.waitDurationNs.Add(time.Now().UnixNano() - start.UnixNano()) atomic.AddUint32(&p.stats.WaitCount, 1) - } else if err == ErrPoolTimeout { + case ErrPoolTimeout: atomic.AddUint32(&p.stats.Timeouts, 1) } @@ -665,18 +667,12 @@ func (p *ConnPool) putConn(ctx context.Context, cn *Conn, freeTurn bool) { } } - // If hooks say to remove the connection, do so - if shouldRemove { + // Combine all removal checks into one - reduces branches + if shouldRemove || !shouldPool { p.removeConnInternal(ctx, cn, errors.New("hook requested removal"), freeTurn) return } - // If processor says not to pool the connection, remove it - if !shouldPool { - p.removeConnInternal(ctx, cn, errors.New("hook requested no pooling"), freeTurn) - return - } - if !cn.pooled { p.removeConnInternal(ctx, cn, errors.New("connection not pooled"), freeTurn) return diff --git a/internal/pool/semaphore.go b/internal/pool/semaphore.go index e16ee733..85115a97 100644 --- a/internal/pool/semaphore.go +++ b/internal/pool/semaphore.go @@ -124,15 +124,4 @@ func (s *fastSemaphore) release() { default: // No waiters, that's fine } -} - -// len returns the current number of acquired tokens. -func (s *fastSemaphore) len() int32 { - return s.count.Load() -} - -// cap returns the maximum capacity of the semaphore. -func (s *fastSemaphore) cap() int32 { - return s.max -} - +} \ No newline at end of file