Change activeTCPConn.close to atomic.Bool

Replace manual atomic operations with atomic.Bool type for better
type safety and cleaner code. This modernizes the atomic usage
pattern from atomic.LoadInt32/StoreInt32 to the newer Load/Store
methods on atomic.Bool.

- Update activeTCPConn.closed field type from int32 to atomic.Bool
- Replace atomic.LoadInt32(&a.closed) with a.closed.Load()
- Replace atomic.StoreInt32(&a.closed, 1) with a.closed.Store(true)

All existing functionality preserved with improved type safety.

Signed-off-by: Xiaobo Liu <cppcoffee@gmail.com>

tweak

Signed-off-by: Xiaobo Liu <cppcoffee@gmail.com>
This commit is contained in:
Xiaobo Liu
2025-06-14 15:16:36 +08:00
parent 753c2a0fe8
commit bbb9792ca9
+8 -8
View File
@@ -18,7 +18,7 @@ import (
type activeTCPConn struct {
readBuffer, writeBuffer *packetio.Buffer
localAddr, remoteAddr atomic.Value
closed int32
closed atomic.Bool
}
func newActiveTCPConn(
@@ -34,7 +34,7 @@ func newActiveTCPConn(
laddr, err := getTCPAddrOnInterface(localAddress)
if err != nil {
atomic.StoreInt32(&a.closed, 1)
a.closed.Store(true)
log.Infof("Failed to dial TCP address %s: %v", remoteAddress, err)
return a
@@ -43,7 +43,7 @@ func newActiveTCPConn(
go func() {
defer func() {
atomic.StoreInt32(&a.closed, 1)
a.closed.Store(true)
}()
dialer := &net.Dialer{
@@ -60,7 +60,7 @@ func newActiveTCPConn(
go func() {
buff := make([]byte, receiveMTU)
for atomic.LoadInt32(&a.closed) == 0 {
for !a.closed.Load() {
n, err := readStreamingPacket(conn, buff)
if err != nil {
log.Infof("Failed to read streaming packet: %s", err)
@@ -78,7 +78,7 @@ func newActiveTCPConn(
buff := make([]byte, receiveMTU)
for atomic.LoadInt32(&a.closed) == 0 {
for !a.closed.Load() {
n, err := a.writeBuffer.Read(buff)
if err != nil {
log.Infof("Failed to read from buffer: %s", err)
@@ -102,7 +102,7 @@ func newActiveTCPConn(
}
func (a *activeTCPConn) ReadFrom(buff []byte) (n int, srcAddr net.Addr, err error) {
if atomic.LoadInt32(&a.closed) == 1 {
if a.closed.Load() {
return 0, nil, io.ErrClosedPipe
}
@@ -114,7 +114,7 @@ func (a *activeTCPConn) ReadFrom(buff []byte) (n int, srcAddr net.Addr, err erro
}
func (a *activeTCPConn) WriteTo(buff []byte, _ net.Addr) (n int, err error) {
if atomic.LoadInt32(&a.closed) == 1 {
if a.closed.Load() {
return 0, io.ErrClosedPipe
}
@@ -122,7 +122,7 @@ func (a *activeTCPConn) WriteTo(buff []byte, _ net.Addr) (n int, err error) {
}
func (a *activeTCPConn) Close() error {
atomic.StoreInt32(&a.closed, 1)
a.closed.Store(true)
_ = a.readBuffer.Close()
_ = a.writeBuffer.Close()