@@ -26,13 +26,13 @@ var timers = sync.Pool{
26
26
27
27
// Stats contains pool state information and accumulated stats.
28
28
type Stats struct {
29
- Hits uint32 // number of times free connection was found in the pool
30
- Misses uint32 // number of times free connection was NOT found in the pool
31
- Timeouts uint32 // number of times a wait timeout occurred
29
+ Hits atomic. Uint32 // number of times free connection was found in the pool
30
+ Misses atomic. Uint32 // number of times free connection was NOT found in the pool
31
+ Timeouts atomic. Uint32 // number of times a wait timeout occurred
32
32
33
- TotalConns uint32 // number of total connections in the pool
34
- IdleConns uint32 // number of idle connections in the pool
35
- StaleConns uint32 // number of stale connections removed from the pool
33
+ TotalConns uint32 // number of total connections in the pool
34
+ IdleConns uint32 // number of idle connections in the pool
35
+ StaleConns atomic. Uint32 // number of stale connections removed from the pool
36
36
}
37
37
38
38
type Pooler interface {
@@ -71,9 +71,9 @@ type Options struct {
71
71
type ConnPool struct {
72
72
opt * Options
73
73
74
- dialErrorsNum uint32 // atomic
74
+ dialErrorsNum atomic. Uint32
75
75
76
- _closed uint32 // atomic
76
+ _closed atomic. Bool
77
77
78
78
lastDialErrorMu sync.RWMutex
79
79
lastDialError error
@@ -188,14 +188,14 @@ func (p *ConnPool) dialConn(c context.Context, pooled bool) (*Conn, error) {
188
188
return nil , ErrClosed
189
189
}
190
190
191
- if atomic . LoadUint32 ( & p .dialErrorsNum ) >= uint32 (p .opt .PoolSize ) {
191
+ if p .dialErrorsNum . Load ( ) >= uint32 (p .opt .PoolSize ) {
192
192
return nil , p .getLastDialError ()
193
193
}
194
194
195
195
netConn , err := p .opt .Dialer (c )
196
196
if err != nil {
197
197
p .setLastDialError (err )
198
- if atomic . AddUint32 ( & p .dialErrorsNum , 1 ) == uint32 (p .opt .PoolSize ) {
198
+ if p .dialErrorsNum . Add ( 1 ) == uint32 (p .opt .PoolSize ) {
199
199
go p .tryDial ()
200
200
}
201
201
return nil , err
@@ -219,7 +219,7 @@ func (p *ConnPool) tryDial() {
219
219
continue
220
220
}
221
221
222
- atomic . StoreUint32 ( & p .dialErrorsNum , 0 )
222
+ p .dialErrorsNum . Store ( 0 )
223
223
_ = conn .Close ()
224
224
return
225
225
}
@@ -263,11 +263,11 @@ func (p *ConnPool) Get(ctx context.Context) (*Conn, error) {
263
263
continue
264
264
}
265
265
266
- atomic . AddUint32 ( & p .stats .Hits , 1 )
266
+ p .stats .Hits . Add ( 1 )
267
267
return cn , nil
268
268
}
269
269
270
- atomic . AddUint32 ( & p .stats .Misses , 1 )
270
+ p .stats .Misses . Add ( 1 )
271
271
272
272
newcn , err := p .newConn (ctx , true )
273
273
if err != nil {
@@ -313,7 +313,7 @@ func (p *ConnPool) waitTurn(c context.Context) error {
313
313
return nil
314
314
case <- timer .C :
315
315
timers .Put (timer )
316
- atomic . AddUint32 ( & p .stats .Timeouts , 1 )
316
+ p .stats .Timeouts . Add ( 1 )
317
317
return ErrPoolTimeout
318
318
}
319
319
}
@@ -402,20 +402,19 @@ func (p *ConnPool) IdleLen() int {
402
402
}
403
403
404
404
func (p * ConnPool ) Stats () * Stats {
405
- idleLen := p .IdleLen ()
406
- return & Stats {
407
- Hits : atomic .LoadUint32 (& p .stats .Hits ),
408
- Misses : atomic .LoadUint32 (& p .stats .Misses ),
409
- Timeouts : atomic .LoadUint32 (& p .stats .Timeouts ),
410
-
405
+ stats := & Stats {
411
406
TotalConns : uint32 (p .Len ()),
412
- IdleConns : uint32 (idleLen ),
413
- StaleConns : atomic .LoadUint32 (& p .stats .StaleConns ),
407
+ IdleConns : uint32 (p .IdleLen ()),
414
408
}
409
+ stats .Hits .Store (p .stats .Hits .Load ())
410
+ stats .Misses .Store (p .stats .Misses .Load ())
411
+ stats .Timeouts .Store (p .stats .Timeouts .Load ())
412
+ stats .StaleConns .Store (p .stats .StaleConns .Load ())
413
+ return stats
415
414
}
416
415
417
416
func (p * ConnPool ) closed () bool {
418
- return atomic . LoadUint32 ( & p ._closed ) == 1
417
+ return p ._closed . Load ()
419
418
}
420
419
421
420
func (p * ConnPool ) Filter (fn func (* Conn ) bool ) error {
@@ -433,7 +432,7 @@ func (p *ConnPool) Filter(fn func(*Conn) bool) error {
433
432
}
434
433
435
434
func (p * ConnPool ) Close () error {
436
- if ! atomic . CompareAndSwapUint32 ( & p ._closed , 0 , 1 ) {
435
+ if ! p ._closed . CompareAndSwap ( false , true ) {
437
436
return ErrClosed
438
437
}
439
438
@@ -466,7 +465,7 @@ func (p *ConnPool) reaper(frequency time.Duration) {
466
465
internal .Logger .Printf (context .TODO (), "ReapStaleConns failed: %s" , err )
467
466
continue
468
467
}
469
- atomic . AddUint32 ( & p .stats .StaleConns , uint32 (n ))
468
+ p .stats .StaleConns . Add ( uint32 (n ))
470
469
}
471
470
}
472
471
0 commit comments