From c5af1d7a88c44af6a23137c9af94eb5cd3337d2f Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Wed, 4 May 2016 11:29:04 -0700 Subject: [PATCH] rafthttp: fix race on peer status activeSince --- rafthttp/peer.go | 2 +- rafthttp/peer_status.go | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/rafthttp/peer.go b/rafthttp/peer.go index 30b9c4cdc..582df504d 100644 --- a/rafthttp/peer.go +++ b/rafthttp/peer.go @@ -219,7 +219,7 @@ func (p *peer) attachOutgoingConn(conn *outgoingConn) { } } -func (p *peer) activeSince() time.Time { return p.status.activeSince } +func (p *peer) activeSince() time.Time { return p.status.activeSince() } // Pause pauses the peer. The peer will simply drops all incoming // messages without returning an error. diff --git a/rafthttp/peer_status.go b/rafthttp/peer_status.go index d838b7ab3..e98afc9a3 100644 --- a/rafthttp/peer_status.go +++ b/rafthttp/peer_status.go @@ -28,10 +28,10 @@ type failureType struct { } type peerStatus struct { - id types.ID - mu sync.Mutex // protect variables below - active bool - activeSince time.Time + id types.ID + mu sync.Mutex // protect variables below + active bool + since time.Time } func newPeerStatus(id types.ID) *peerStatus { @@ -46,7 +46,7 @@ func (s *peerStatus) activate() { if !s.active { plog.Infof("the connection with %s became active", s.id) s.active = true - s.activeSince = time.Now() + s.since = time.Now() } } @@ -58,7 +58,7 @@ func (s *peerStatus) deactivate(failure failureType, reason string) { plog.Errorf(msg) plog.Infof("the connection with %s became inactive", s.id) s.active = false - s.activeSince = time.Time{} + s.since = time.Time{} return } plog.Debugf(msg) @@ -69,3 +69,9 @@ func (s *peerStatus) isActive() bool { defer s.mu.Unlock() return s.active } + +func (s *peerStatus) activeSince() time.Time { + s.mu.Lock() + defer s.mu.Unlock() + return s.since +}