From 96e28935d559a3bd6d20f23c8b2f4f8e08e420a2 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Fri, 31 May 2019 14:37:01 +0300 Subject: [PATCH] atomic int32 isClosed used --- pkg/transport/managed_transport.go | 11 +++++++---- pkg/transport/manager.go | 6 ++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/pkg/transport/managed_transport.go b/pkg/transport/managed_transport.go index 621caca5f9..17181b3ac9 100644 --- a/pkg/transport/managed_transport.go +++ b/pkg/transport/managed_transport.go @@ -3,6 +3,7 @@ package transport import ( "math/big" "sync" + "sync/atomic" "github.com/google/uuid" ) @@ -18,7 +19,7 @@ type ManagedTransport struct { doneChan chan struct{} errChan chan error - isClosing bool + isClosing int32 mu sync.RWMutex readLogChan chan int @@ -89,10 +90,12 @@ func (tr *ManagedTransport) Write(p []byte) (n int, err error) { // Close closes underlying func (tr *ManagedTransport) Close() error { - tr.mu.Lock() + + atomic.StoreInt32(&tr.isClosing, 1) + + tr.mu.RLock() err := tr.Transport.Close() - tr.isClosing = true - tr.mu.Unlock() + tr.mu.RUnlock() select { case <-tr.doneChan: diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index e96daa7f60..82c8e830ef 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -6,6 +6,7 @@ import ( "math/big" "strings" "sync" + "sync/atomic" "time" "github.com/google/uuid" @@ -387,10 +388,7 @@ func (tm *Manager) manageTransport(ctx context.Context, managedTr *ManagedTransp tm.Logger.Infof("Transport %s closed", managedTr.ID) return case err := <-managedTr.errChan: - managedTr.mu.RLock() - isClosing := managedTr.isClosing - managedTr.mu.RUnlock() - if !isClosing { + if atomic.LoadInt32(&managedTr.isClosing) == 0 { tm.Logger.Infof("Transport %s failed with error: %s. Re-dialing...", managedTr.ID, err) if accepted { if err := tm.DeleteTransport(managedTr.ID); err != nil {