@@ -33,9 +33,11 @@ var timers = sync.Pool{
3333
3434// Stats contains pool state information and accumulated stats.
3535type Stats struct {
36- Hits uint32 // number of times free connection was found in the pool
37- Misses uint32 // number of times free connection was NOT found in the pool
38- Timeouts uint32 // number of times a wait timeout occurred
36+ Hits uint32 // number of times free connection was found in the pool
37+ Misses uint32 // number of times free connection was NOT found in the pool
38+ Timeouts uint32 // number of times a wait timeout occurred
39+ WaitCount uint32 // number of times a connection was waited
40+ WaitDurationNs int64 // total time spent for waiting a connection in nanoseconds
3941
4042 TotalConns uint32 // number of total connections in the pool
4143 IdleConns uint32 // number of idle connections in the pool
@@ -90,7 +92,8 @@ type ConnPool struct {
9092 poolSize int
9193 idleConnsLen int
9294
93- stats Stats
95+ stats Stats
96+ waitDurationNs atomic.Int64
9497
9598 _closed uint32 // atomic
9699}
@@ -320,6 +323,7 @@ func (p *ConnPool) waitTurn(ctx context.Context) error {
320323 default :
321324 }
322325
326+ start := time .Now ()
323327 timer := timers .Get ().(* time.Timer )
324328 timer .Reset (p .cfg .PoolTimeout )
325329
@@ -331,6 +335,8 @@ func (p *ConnPool) waitTurn(ctx context.Context) error {
331335 timers .Put (timer )
332336 return ctx .Err ()
333337 case p .queue <- struct {}{}:
338+ p .waitDurationNs .Add (time .Since (start ).Nanoseconds ())
339+ atomic .AddUint32 (& p .stats .WaitCount , 1 )
334340 if ! timer .Stop () {
335341 <- timer .C
336342 }
@@ -457,9 +463,11 @@ func (p *ConnPool) IdleLen() int {
457463
458464func (p * ConnPool ) Stats () * Stats {
459465 return & Stats {
460- Hits : atomic .LoadUint32 (& p .stats .Hits ),
461- Misses : atomic .LoadUint32 (& p .stats .Misses ),
462- Timeouts : atomic .LoadUint32 (& p .stats .Timeouts ),
466+ Hits : atomic .LoadUint32 (& p .stats .Hits ),
467+ Misses : atomic .LoadUint32 (& p .stats .Misses ),
468+ Timeouts : atomic .LoadUint32 (& p .stats .Timeouts ),
469+ WaitCount : atomic .LoadUint32 (& p .stats .WaitCount ),
470+ WaitDurationNs : p .waitDurationNs .Load (),
463471
464472 TotalConns : uint32 (p .Len ()),
465473 IdleConns : uint32 (p .IdleLen ()),
0 commit comments