Skip to content

Commit

Permalink
go: Use new atomic types introduced in go1.19
Browse files Browse the repository at this point in the history
Those come with nocopy protection, so they can prevent bugs like people
passing the types by value instead of by pointer from the compiler.
  • Loading branch information
fishy committed Feb 3, 2023
1 parent d21188a commit 1448934
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/go/thrift/serializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,15 @@ func TestSerializer(t *testing.T) {

func TestSerializerPoolAsync(t *testing.T) {
var wg sync.WaitGroup
var counter int64
var counter atomic.Int64
s := NewTSerializerPool(NewTSerializer)
d := NewTDeserializerPool(NewTDeserializer)
f := func(i int64) bool {
wg.Add(1)
go func() {
defer wg.Done()
t.Run(
fmt.Sprintf("#%d-%d", atomic.AddInt64(&counter, 1), i),
fmt.Sprintf("#%d-%d", counter.Add(1), i),
func(t *testing.T) {
m := MyTestStruct{
Int64: i,
Expand Down
10 changes: 5 additions & 5 deletions lib/go/thrift/simple_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var ServerStopTimeout = time.Duration(0)
* This will work if golang user implements a conn-pool like thing in client side.
*/
type TSimpleServer struct {
closed int32
closed atomic.Int32
wg sync.WaitGroup
mu sync.Mutex
stopChan chan struct{}
Expand Down Expand Up @@ -186,7 +186,7 @@ func (p *TSimpleServer) innerAccept() (int32, error) {
client, err := p.serverTransport.Accept()
p.mu.Lock()
defer p.mu.Unlock()
closed := atomic.LoadInt32(&p.closed)
closed := p.closed.Load()
if closed != 0 {
return closed, nil
}
Expand Down Expand Up @@ -246,10 +246,10 @@ func (p *TSimpleServer) Stop() error {
p.mu.Lock()
defer p.mu.Unlock()

if atomic.LoadInt32(&p.closed) != 0 {
if !p.closed.CompareAndSwap(0, 1) {
// Already closed
return nil
}
atomic.StoreInt32(&p.closed, 1)
p.serverTransport.Interrupt()

ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -328,7 +328,7 @@ func (p *TSimpleServer) processRequests(client TTransport) (err error) {
defer outputTransport.Close()
}
for {
if atomic.LoadInt32(&p.closed) != 0 {
if p.closed.Load() != 0 {
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions lib/go/thrift/socket_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type socketConn struct {
net.Conn

buffer [1]byte
closed int32
closed atomic.Int32
}

var _ net.Conn = (*socketConn)(nil)
Expand Down Expand Up @@ -67,7 +67,7 @@ func wrapSocketConn(conn net.Conn) *socketConn {
// It's the same as the previous implementation of TSocket.IsOpen and
// TSSLSocket.IsOpen before we added connectivity check.
func (sc *socketConn) isValid() bool {
return sc != nil && sc.Conn != nil && atomic.LoadInt32(&sc.closed) == 0
return sc != nil && sc.Conn != nil && sc.closed.Load() == 0
}

// IsOpen checks whether the connection is open.
Expand Down Expand Up @@ -119,6 +119,6 @@ func (sc *socketConn) Close() error {
// Already closed
return net.ErrClosed
}
atomic.StoreInt32(&sc.closed, 1)
sc.closed.Store(1)
return sc.Conn.Close()
}

0 comments on commit 1448934

Please sign in to comment.