@@ -35,9 +35,9 @@ func (e BadConnError) Unwrap() error {
3535
3636type StickyConnPool struct {
3737 pool Pooler
38- shared int32 // atomic
38+ shared atomic. Int32
3939
40- state uint32 // atomic
40+ state atomic. Uint32
4141 ch chan * Conn
4242
4343 _badConnError atomic.Value
@@ -53,7 +53,7 @@ func NewStickyConnPool(pool Pooler) *StickyConnPool {
5353 ch : make (chan * Conn , 1 ),
5454 }
5555 }
56- atomic . AddInt32 ( & p .shared , 1 )
56+ p .shared . Add ( 1 )
5757 return p
5858}
5959
@@ -68,13 +68,13 @@ func (p *StickyConnPool) CloseConn(cn *Conn) error {
6868func (p * StickyConnPool ) Get (ctx context.Context ) (* Conn , error ) {
6969 // In worst case this races with Close which is not a very common operation.
7070 for i := 0 ; i < 1000 ; i ++ {
71- switch atomic . LoadUint32 ( & p .state ) {
71+ switch p .state . Load ( ) {
7272 case stateDefault :
7373 cn , err := p .pool .Get (ctx )
7474 if err != nil {
7575 return nil , err
7676 }
77- if atomic . CompareAndSwapUint32 ( & p .state , stateDefault , stateInited ) {
77+ if p .state . CompareAndSwap ( stateDefault , stateInited ) {
7878 return cn , nil
7979 }
8080 p .pool .Remove (ctx , cn , ErrClosed )
@@ -124,16 +124,16 @@ func (p *StickyConnPool) Remove(ctx context.Context, cn *Conn, reason error) {
124124}
125125
126126func (p * StickyConnPool ) Close () error {
127- if shared := atomic . AddInt32 ( & p .shared , - 1 ); shared > 0 {
127+ if shared := p .shared . Add ( - 1 ); shared > 0 {
128128 return nil
129129 }
130130
131131 for i := 0 ; i < 1000 ; i ++ {
132- state := atomic . LoadUint32 ( & p .state )
132+ state := p .state . Load ( )
133133 if state == stateClosed {
134134 return ErrClosed
135135 }
136- if atomic . CompareAndSwapUint32 ( & p .state , state , stateClosed ) {
136+ if p .state . CompareAndSwap ( state , stateClosed ) {
137137 close (p .ch )
138138 cn , ok := <- p .ch
139139 if ok {
@@ -162,8 +162,8 @@ func (p *StickyConnPool) Reset(ctx context.Context) error {
162162 return errors .New ("pg: StickyConnPool does not have a Conn" )
163163 }
164164
165- if ! atomic . CompareAndSwapUint32 ( & p .state , stateInited , stateDefault ) {
166- state := atomic . LoadUint32 ( & p .state )
165+ if ! p .state . CompareAndSwap ( stateInited , stateDefault ) {
166+ state := p .state . Load ( )
167167 return fmt .Errorf ("pg: invalid StickyConnPool state: %d" , state )
168168 }
169169
@@ -181,7 +181,7 @@ func (p *StickyConnPool) badConnError() error {
181181}
182182
183183func (p * StickyConnPool ) Len () int {
184- switch atomic . LoadUint32 ( & p .state ) {
184+ switch p .state . Load ( ) {
185185 case stateDefault :
186186 return 0
187187 case stateInited :
0 commit comments