From 536a6d13627fe050e477cb12ac82287da7259d1b Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Sun, 3 Sep 2023 23:04:35 -0400 Subject: [PATCH] Use atomic Int32 instead of Bool Small change that lets us keep Go 1.15 support --- udp/batchconn.go | 7 +++---- udp/conn_test.go | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/udp/batchconn.go b/udp/batchconn.go index b01be25..54bdab6 100644 --- a/udp/batchconn.go +++ b/udp/batchconn.go @@ -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. @@ -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 { @@ -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() diff --git a/udp/conn_test.go b/udp/conn_test.go index 1904d55..6b5c154 100644 --- a/udp/conn_test.go +++ b/udp/conn_test.go @@ -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) @@ -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 {