From a11bcd9686b210ac5d1bc17aff4875fb38d6db53 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Thu, 11 Jun 2020 17:34:51 -0700 Subject: [PATCH] [consensus] replace blockNumMutex with atomic.Value in messageSender Signed-off-by: Leo Chen --- consensus/consensus_msg_sender.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/consensus/consensus_msg_sender.go b/consensus/consensus_msg_sender.go index 538cf5fc2..cb0198158 100644 --- a/consensus/consensus_msg_sender.go +++ b/consensus/consensus_msg_sender.go @@ -2,6 +2,7 @@ package consensus import ( "sync" + "sync/atomic" "time" msg_pb "github.com/harmony-one/harmony/api/proto/message" @@ -18,7 +19,6 @@ const ( // MessageSender is the wrapper object that controls how a consensus message is sent type MessageSender struct { blockNum uint64 // The current block number at consensus - blockNumMutex sync.Mutex messagesToRetry sync.Map // The p2p host used to send/receive p2p messages host p2p.Host @@ -44,9 +44,7 @@ func NewMessageSender(host p2p.Host) *MessageSender { // Reset resets the sender's state for new block func (sender *MessageSender) Reset(blockNum uint64) { - sender.blockNumMutex.Lock() - sender.blockNum = blockNum - sender.blockNumMutex.Unlock() + atomic.StoreUint64(&sender.blockNum, blockNum) sender.StopAllRetriesExceptCommitted() sender.messagesToRetry.Range(func(key interface{}, value interface{}) bool { if msgRetry, ok := value.(*MessageRetry); ok { @@ -95,13 +93,11 @@ func (sender *MessageSender) Retry(msgRetry *MessageRetry) { msgRetry.isActiveMutex.Unlock() if msgRetry.msgType != msg_pb.MessageType_COMMITTED { - sender.blockNumMutex.Lock() - if msgRetry.blockNum < sender.blockNum { - sender.blockNumMutex.Unlock() + senderBlockNum := atomic.LoadUint64(&sender.blockNum) + if msgRetry.blockNum < senderBlockNum { // Block already moved ahead, no need to retry old block's messages return } - sender.blockNumMutex.Unlock() } msgRetry.retryCount++