From 2ba8af057e2292d34aca0d222a7a87f728193f70 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Wed, 5 Jun 2019 18:33:52 +0300 Subject: [PATCH] Changed killWorker - now it's using sync.Once --- pkg/router/router.go | 2 ++ pkg/transport/managed_transport.go | 12 +++++------- pkg/transport/manager.go | 2 -- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/pkg/router/router.go b/pkg/router/router.go index d5b62302ef..9cd06f564e 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -85,6 +85,8 @@ func (r *Router) Serve(ctx context.Context) error { isAccepted, isSetup := tp.Accepted, r.IsSetupTransport(tp) r.mu.Unlock() + r.Logger.Infof("New transport: isAccepted: %v, isSetup: %v", isAccepted, isSetup) + var serve func(io.ReadWriter) error switch { case isAccepted && isSetup: diff --git a/pkg/transport/managed_transport.go b/pkg/transport/managed_transport.go index 58e09e5ff6..912bab151a 100644 --- a/pkg/transport/managed_transport.go +++ b/pkg/transport/managed_transport.go @@ -21,6 +21,7 @@ type ManagedTransport struct { errChan chan error isClosing int32 mu sync.RWMutex + once sync.Once readLogChan chan int writeLogChan chan int @@ -34,8 +35,8 @@ func newManagedTransport(id uuid.UUID, tr Transport, public bool, accepted bool) Accepted: accepted, doneChan: make(chan struct{}), errChan: make(chan error), - readLogChan: make(chan int), - writeLogChan: make(chan int), + readLogChan: make(chan int, 16), + writeLogChan: make(chan int, 16), LogEntry: &LogEntry{new(big.Int), new(big.Int)}, } } @@ -72,12 +73,9 @@ func (tr *ManagedTransport) Write(p []byte) (n int, err error) { // killWorker sends signal to Manager.manageTransport goroutine to exit // it's safe to call it multiple times func (tr *ManagedTransport) killWorker() { - select { - case <-tr.doneChan: - return - default: + tr.once.Do(func() { close(tr.doneChan) - } + }) } // Close closes underlying diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index 1ad99a5617..80fcbc2158 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -245,8 +245,6 @@ func (tm *Manager) DeleteTransport(id uuid.UUID) error { tr := tm.transports[id] delete(tm.transports, id) tm.mu.Unlock() - mgrQty := atomic.AddInt32(&tm.mgrQty, -1) - tm.Logger.Infof("Manager.DeleteTransport id: %v, mgrQty = %v", id, mgrQty) tr.Close()