@@ -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
@@ -304,6 +306,7 @@ func (p *ConnPool) waitTurn(ctx context.Context) error {
304
306
default :
305
307
}
306
308
309
+ start := time .Now ()
307
310
timer := timers .Get ().(* time.Timer )
308
311
timer .Reset (p .cfg .PoolTimeout )
309
312
@@ -315,6 +318,8 @@ func (p *ConnPool) waitTurn(ctx context.Context) error {
315
318
timers .Put (timer )
316
319
return ctx .Err ()
317
320
case p .queue <- struct {}{}:
321
+ atomic .AddInt64 (& p .stats .WaitDurationNs , time .Since (start ).Nanoseconds ())
322
+ atomic .AddUint32 (& p .stats .WaitCount , 1 )
318
323
if ! timer .Stop () {
319
324
<- timer .C
320
325
}
@@ -441,9 +446,11 @@ func (p *ConnPool) IdleLen() int {
441
446
442
447
func (p * ConnPool ) Stats () * Stats {
443
448
return & Stats {
444
- Hits : atomic .LoadUint32 (& p .stats .Hits ),
445
- Misses : atomic .LoadUint32 (& p .stats .Misses ),
446
- Timeouts : atomic .LoadUint32 (& p .stats .Timeouts ),
449
+ Hits : atomic .LoadUint32 (& p .stats .Hits ),
450
+ Misses : atomic .LoadUint32 (& p .stats .Misses ),
451
+ Timeouts : atomic .LoadUint32 (& p .stats .Timeouts ),
452
+ WaitCount : atomic .LoadUint32 (& p .stats .WaitCount ),
453
+ WaitDurationNs : atomic .LoadInt64 (& p .stats .WaitDurationNs ),
447
454
448
455
TotalConns : uint32 (p .Len ()),
449
456
IdleConns : uint32 (p .IdleLen ()),
0 commit comments