Skip to content

Commit

Permalink
Use atomic Int32 instead of Bool
Browse files Browse the repository at this point in the history
Small change that lets us keep Go 1.15 support
  • Loading branch information
Sean-Der committed Sep 8, 2023
1 parent 6bdfa93 commit 536a6d1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
7 changes: 3 additions & 4 deletions udp/batchconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type BatchConn struct {
batchWriteSize int
batchWriteInterval time.Duration

closed atomic.Bool
closed int32
}

// NewBatchConn creates a *BatchConn from net.PacketConn with batch configs.
Expand Down Expand Up @@ -76,8 +76,7 @@ func NewBatchConn(conn net.PacketConn, batchWriteSize int, batchWriteInterval ti
go func() {
writeTicker := time.NewTicker(batchWriteInterval / 2)
defer writeTicker.Stop()

for !bc.closed.Load() {
for atomic.LoadInt32(&bc.closed) != 1 {
<-writeTicker.C
bc.batchWriteMutex.Lock()
if bc.batchWritePos > 0 && time.Since(bc.batchWriteLast) >= bc.batchWriteInterval {
Expand All @@ -93,7 +92,7 @@ func NewBatchConn(conn net.PacketConn, batchWriteSize int, batchWriteInterval ti

// Close batchConn and the underlying PacketConn
func (c *BatchConn) Close() error {
c.closed.Store(true)
atomic.StoreInt32(&c.closed, 1)
c.batchWriteMutex.Lock()
if c.batchWritePos > 0 {
_ = c.flush()
Expand Down
6 changes: 3 additions & 3 deletions udp/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,10 @@ func TestBatchIO(t *testing.T) {
var serverConnWg sync.WaitGroup
serverConnWg.Add(1)
go func() {
var exit atomic.Bool
var exit int32
defer func() {
defer serverConnWg.Done()
exit.Store(true)
atomic.StoreInt32(&exit, 1)
}()
for {
buf := make([]byte, 1400)
Expand All @@ -520,7 +520,7 @@ func TestBatchIO(t *testing.T) {
_ = conn.Close()
serverConnWg.Done()
}()
for !exit.Load() {
for atomic.LoadInt32(&exit) != 1 {
_ = conn.SetReadDeadline(time.Now().Add(time.Second))
n, rerr := conn.Read(buf)
if rerr != nil {
Expand Down

0 comments on commit 536a6d1

Please sign in to comment.