@@ -26,13 +26,13 @@ var timers = sync.Pool{
2626
2727// Stats contains pool state information and accumulated stats.
2828type 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
3232
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
3636}
3737
3838type Pooler interface {
@@ -71,9 +71,9 @@ type Options struct {
7171type ConnPool struct {
7272 opt * Options
7373
74- dialErrorsNum uint32 // atomic
74+ dialErrorsNum atomic. Uint32
7575
76- _closed uint32 // atomic
76+ _closed atomic. Bool
7777
7878 lastDialErrorMu sync.RWMutex
7979 lastDialError error
@@ -188,14 +188,14 @@ func (p *ConnPool) dialConn(c context.Context, pooled bool) (*Conn, error) {
188188 return nil , ErrClosed
189189 }
190190
191- if atomic . LoadUint32 ( & p .dialErrorsNum ) >= uint32 (p .opt .PoolSize ) {
191+ if p .dialErrorsNum . Load ( ) >= uint32 (p .opt .PoolSize ) {
192192 return nil , p .getLastDialError ()
193193 }
194194
195195 netConn , err := p .opt .Dialer (c )
196196 if err != nil {
197197 p .setLastDialError (err )
198- if atomic . AddUint32 ( & p .dialErrorsNum , 1 ) == uint32 (p .opt .PoolSize ) {
198+ if p .dialErrorsNum . Add ( 1 ) == uint32 (p .opt .PoolSize ) {
199199 go p .tryDial ()
200200 }
201201 return nil , err
@@ -219,7 +219,7 @@ func (p *ConnPool) tryDial() {
219219 continue
220220 }
221221
222- atomic . StoreUint32 ( & p .dialErrorsNum , 0 )
222+ p .dialErrorsNum . Store ( 0 )
223223 _ = conn .Close ()
224224 return
225225 }
@@ -263,11 +263,11 @@ func (p *ConnPool) Get(ctx context.Context) (*Conn, error) {
263263 continue
264264 }
265265
266- atomic . AddUint32 ( & p .stats .Hits , 1 )
266+ p .stats .Hits . Add ( 1 )
267267 return cn , nil
268268 }
269269
270- atomic . AddUint32 ( & p .stats .Misses , 1 )
270+ p .stats .Misses . Add ( 1 )
271271
272272 newcn , err := p .newConn (ctx , true )
273273 if err != nil {
@@ -313,7 +313,7 @@ func (p *ConnPool) waitTurn(c context.Context) error {
313313 return nil
314314 case <- timer .C :
315315 timers .Put (timer )
316- atomic . AddUint32 ( & p .stats .Timeouts , 1 )
316+ p .stats .Timeouts . Add ( 1 )
317317 return ErrPoolTimeout
318318 }
319319}
@@ -402,20 +402,19 @@ func (p *ConnPool) IdleLen() int {
402402}
403403
404404func (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 {
411406 TotalConns : uint32 (p .Len ()),
412- IdleConns : uint32 (idleLen ),
413- StaleConns : atomic .LoadUint32 (& p .stats .StaleConns ),
407+ IdleConns : uint32 (p .IdleLen ()),
414408 }
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
415414}
416415
417416func (p * ConnPool ) closed () bool {
418- return atomic . LoadUint32 ( & p ._closed ) == 1
417+ return p ._closed . Load ()
419418}
420419
421420func (p * ConnPool ) Filter (fn func (* Conn ) bool ) error {
@@ -433,7 +432,7 @@ func (p *ConnPool) Filter(fn func(*Conn) bool) error {
433432}
434433
435434func (p * ConnPool ) Close () error {
436- if ! atomic . CompareAndSwapUint32 ( & p ._closed , 0 , 1 ) {
435+ if ! p ._closed . CompareAndSwap ( false , true ) {
437436 return ErrClosed
438437 }
439438
@@ -466,7 +465,7 @@ func (p *ConnPool) reaper(frequency time.Duration) {
466465 internal .Logger .Printf (context .TODO (), "ReapStaleConns failed: %s" , err )
467466 continue
468467 }
469- atomic . AddUint32 ( & p .stats .StaleConns , uint32 (n ))
468+ p .stats .StaleConns . Add ( uint32 (n ))
470469 }
471470}
472471
0 commit comments