Skip to content

Commit

Permalink
atomic int32 isClosed used
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuryshev committed May 31, 2019
1 parent cb3ef88 commit 96e2893
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 7 additions & 4 deletions pkg/transport/managed_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package transport
import (
"math/big"
"sync"
"sync/atomic"

"github.com/google/uuid"
)
Expand All @@ -18,7 +19,7 @@ type ManagedTransport struct {

doneChan chan struct{}
errChan chan error
isClosing bool
isClosing int32
mu sync.RWMutex

readLogChan chan int
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 2 additions & 4 deletions pkg/transport/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/big"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 96e2893

Please sign in to comment.