@@ -33,9 +33,11 @@ var timers = sync.Pool{
33
33
34
34
// Stats contains pool state information and accumulated stats.
35
35
type 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
39
41
40
42
TotalConns uint32 // number of total connections in the pool
41
43
IdleConns uint32 // number of idle connections in the pool
@@ -90,7 +92,8 @@ type ConnPool struct {
90
92
poolSize int
91
93
idleConnsLen int
92
94
93
- stats Stats
95
+ stats Stats
96
+ waitDurationNs atomic.Int64
94
97
95
98
_closed uint32 // atomic
96
99
}
@@ -320,6 +323,7 @@ func (p *ConnPool) waitTurn(ctx context.Context) error {
320
323
default :
321
324
}
322
325
326
+ start := time .Now ()
323
327
timer := timers .Get ().(* time.Timer )
324
328
timer .Reset (p .cfg .PoolTimeout )
325
329
@@ -331,6 +335,8 @@ func (p *ConnPool) waitTurn(ctx context.Context) error {
331
335
timers .Put (timer )
332
336
return ctx .Err ()
333
337
case p .queue <- struct {}{}:
338
+ p .waitDurationNs .Add (time .Since (start ).Nanoseconds ())
339
+ atomic .AddUint32 (& p .stats .WaitCount , 1 )
334
340
if ! timer .Stop () {
335
341
<- timer .C
336
342
}
@@ -457,9 +463,11 @@ func (p *ConnPool) IdleLen() int {
457
463
458
464
func (p * ConnPool ) Stats () * Stats {
459
465
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 (),
463
471
464
472
TotalConns : uint32 (p .Len ()),
465
473
IdleConns : uint32 (p .IdleLen ()),
0 commit comments