From e4e4a6ab5872e5d1cfc2713b8cc44695bc8778d6 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 24 Aug 2018 17:54:26 -0700 Subject: [PATCH] peer: move atomic var increment in msgConsumer goroutine to defer In this commit we move the atomic var increment that signals the consumer goourtine has exited to the top of the method in a defer statement. This cleans up some duplicate code and also adheres to the pattern of using defers to signal cleaning up any dependent goroutine state on exit. --- peer.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/peer.go b/peer.go index 63566a8e..a9833e4b 100644 --- a/peer.go +++ b/peer.go @@ -738,6 +738,7 @@ func (ms *msgStream) Stop() { func (ms *msgStream) msgConsumer() { defer ms.wg.Done() defer peerLog.Tracef(ms.stopMsg) + defer atomic.StoreInt32(&ms.streamShutdown, 1) peerLog.Tracef(ms.startMsg) @@ -754,7 +755,6 @@ func (ms *msgStream) msgConsumer() { select { case <-ms.quit: ms.msgCond.L.Unlock() - atomic.StoreInt32(&ms.streamShutdown, 1) return default: } @@ -778,7 +778,6 @@ func (ms *msgStream) msgConsumer() { select { case ms.producerSema <- struct{}{}: case <-ms.quit: - atomic.StoreInt32(&ms.streamShutdown, 1) return } }