@@ -155,7 +155,7 @@ type broker struct {
155155 // reqs manages incoming message requests.
156156 reqs ringReq
157157 // dead is an atomic so a backed up reqs cannot block broker stoppage.
158- dead int32
158+ dead atomicBool
159159}
160160
161161// brokerVersions is loaded once (and potentially a few times concurrently if
@@ -214,7 +214,7 @@ func (cl *Client) newBroker(nodeID int32, host string, port int32, rack *string)
214214
215215// stopForever permanently disables this broker.
216216func (b * broker ) stopForever () {
217- if atomic . SwapInt32 ( & b .dead , 1 ) == 1 {
217+ if b .dead . Swap ( true ) {
218218 return
219219 }
220220
@@ -502,7 +502,7 @@ func (b *broker) loadConnection(ctx context.Context, req kmsg.Request) (*brokerC
502502 pcxn = & b .cxnSlow
503503 }
504504
505- if * pcxn != nil && atomic . LoadInt32 ( & ( * pcxn ).dead ) == 0 {
505+ if * pcxn != nil && ! ( * pcxn ).dead . Load () {
506506 return * pcxn , nil
507507 }
508508
@@ -581,7 +581,7 @@ func (b *broker) reapConnections(idleTimeout time.Duration) (total int) {
581581 b .cxnGroup ,
582582 b .cxnSlow ,
583583 } {
584- if cxn == nil || atomic . LoadInt32 ( & cxn .dead ) == 1 {
584+ if cxn == nil || cxn .dead . Load () {
585585 continue
586586 }
587587
@@ -592,11 +592,11 @@ func (b *broker) reapConnections(idleTimeout time.Duration) (total int) {
592592 // - produce can write but never read
593593 // - fetch can hang for a while reading (infrequent writes)
594594
595- lastWrite := time .Unix (0 , atomic . LoadInt64 ( & cxn .lastWrite ))
596- lastRead := time .Unix (0 , atomic . LoadInt64 ( & cxn .lastRead ))
595+ lastWrite := time .Unix (0 , cxn .lastWrite . Load ( ))
596+ lastRead := time .Unix (0 , cxn .lastRead . Load ( ))
597597
598- writeIdle := time .Since (lastWrite ) > idleTimeout && atomic . LoadUint32 ( & cxn .writing ) == 0
599- readIdle := time .Since (lastRead ) > idleTimeout && atomic . LoadUint32 ( & cxn .reading ) == 0
598+ writeIdle := time .Since (lastWrite ) > idleTimeout && ! cxn .writing . Load ()
599+ readIdle := time .Since (lastRead ) > idleTimeout && ! cxn .reading . Load ()
600600
601601 if writeIdle && readIdle {
602602 cxn .die ()
@@ -634,7 +634,7 @@ func (b *broker) connect(ctx context.Context) (net.Conn, error) {
634634// brokerCxn manages an actual connection to a Kafka broker. This is separate
635635// the broker struct to allow lazy connection (re)creation.
636636type brokerCxn struct {
637- throttleUntil int64 // atomic nanosec
637+ throttleUntil atomicI64 // atomic nanosec
638638
639639 conn net.Conn
640640
@@ -651,17 +651,17 @@ type brokerCxn struct {
651651 // The following four fields are used for connection reaping.
652652 // Write is only updated in one location; read is updated in three
653653 // due to readConn, readConnAsync, and discard.
654- lastWrite int64
655- lastRead int64
656- writing uint32
657- reading uint32
654+ lastWrite atomicI64
655+ lastRead atomicI64
656+ writing atomicBool
657+ reading atomicBool
658658
659659 successes uint64
660660
661661 // resps manages reading kafka responses.
662662 resps ringResp
663663 // dead is an atomic so that a backed up resps cannot block cxn death.
664- dead int32
664+ dead atomicBool
665665 // closed in cloneConn; allows throttle waiting to quit
666666 deadCh chan struct {}
667667}
@@ -982,7 +982,7 @@ func maybeUpdateCtxErr(clientCtx, reqCtx context.Context, err *error) {
982982func (cxn * brokerCxn ) writeRequest (ctx context.Context , enqueuedForWritingAt time.Time , req kmsg.Request ) (corrID int32 , bytesWritten int , writeWait , timeToWrite time.Duration , readEnqueue time.Time , writeErr error ) {
983983 // A nil ctx means we cannot be throttled.
984984 if ctx != nil {
985- throttleUntil := time .Unix (0 , atomic . LoadInt64 ( & cxn .throttleUntil ))
985+ throttleUntil := time .Unix (0 , cxn .throttleUntil . Load ( ))
986986 if sleep := time .Until (throttleUntil ); sleep > 0 {
987987 after := time .NewTimer (sleep )
988988 select {
@@ -1037,10 +1037,10 @@ func (cxn *brokerCxn) writeConn(
10371037 timeout time.Duration ,
10381038 enqueuedForWritingAt time.Time ,
10391039) (bytesWritten int , writeWait , timeToWrite time.Duration , readEnqueue time.Time , writeErr error ) {
1040- atomic . SwapUint32 ( & cxn .writing , 1 )
1040+ cxn .writing . Store ( true )
10411041 defer func () {
1042- atomic . StoreInt64 ( & cxn .lastWrite , time .Now ().UnixNano ())
1043- atomic . SwapUint32 ( & cxn .writing , 0 )
1042+ cxn .lastWrite . Store ( time .Now ().UnixNano ())
1043+ cxn .writing . Store ( false )
10441044 }()
10451045
10461046 if ctx == nil {
@@ -1085,10 +1085,10 @@ func (cxn *brokerCxn) readConn(
10851085 timeout time.Duration ,
10861086 enqueuedForReadingAt time.Time ,
10871087) (nread int , buf []byte , readWait , timeToRead time.Duration , err error ) {
1088- atomic . SwapUint32 ( & cxn .reading , 1 )
1088+ cxn .reading . Store ( true )
10891089 defer func () {
1090- atomic . StoreInt64 ( & cxn .lastRead , time .Now ().UnixNano ())
1091- atomic . SwapUint32 ( & cxn .reading , 0 )
1090+ cxn .lastRead . Store ( time .Now ().UnixNano ())
1091+ cxn .reading . Store ( false )
10921092 }()
10931093
10941094 if ctx == nil {
@@ -1256,7 +1256,7 @@ func (cxn *brokerCxn) closeConn() {
12561256// die kills a broker connection (which could be dead already) and replies to
12571257// all requests awaiting responses appropriately.
12581258func (cxn * brokerCxn ) die () {
1259- if cxn == nil || atomic . SwapInt32 ( & cxn .dead , 1 ) == 1 {
1259+ if cxn == nil || cxn .dead . Swap ( true ) {
12601260 return
12611261 }
12621262 cxn .closeConn ()
@@ -1364,10 +1364,10 @@ func (cxn *brokerCxn) discard() {
13641364 }
13651365 deadlineMu .Unlock ()
13661366
1367- atomic . SwapUint32 ( & cxn .reading , 1 )
1367+ cxn .reading . Store ( true )
13681368 defer func () {
1369- atomic . StoreInt64 ( & cxn .lastRead , time .Now ().UnixNano ())
1370- atomic . SwapUint32 ( & cxn .reading , 0 )
1369+ cxn .lastRead . Store ( time .Now ().UnixNano ())
1370+ cxn .reading . Store ( false )
13711371 }()
13721372
13731373 readStart := time .Now ()
@@ -1470,8 +1470,8 @@ func (cxn *brokerCxn) handleResp(pr promisedResp) {
14701470 if millis > 0 {
14711471 if throttlesAfterResp {
14721472 throttleUntil := time .Now ().Add (time .Millisecond * time .Duration (millis )).UnixNano ()
1473- if throttleUntil > cxn .throttleUntil {
1474- atomic . StoreInt64 ( & cxn .throttleUntil , throttleUntil )
1473+ if throttleUntil > cxn .throttleUntil . Load () {
1474+ cxn .throttleUntil . Store ( throttleUntil )
14751475 }
14761476 }
14771477 cxn .cl .cfg .hooks .each (func (h Hook ) {
0 commit comments