diff --git a/dot/peerset/peerset.go b/dot/peerset/peerset.go index 6df05900c76..e39aac92c31 100644 --- a/dot/peerset/peerset.go +++ b/dot/peerset/peerset.go @@ -547,14 +547,14 @@ func (ps *PeerSet) incoming(setID int, peers ...peer.ID) error { var nodeReputation Reputation - state.peerStateRWMutex.RLock() + state.RLock() node, has := state.nodes[pid] if has { nodeReputation = node.rep } - state.peerStateRWMutex.RUnlock() + state.RUnlock() switch { case nodeReputation < BannedThresholdValue: diff --git a/dot/peerset/peerstate.go b/dot/peerset/peerstate.go index 75b55bcbe30..44ef1b3f95f 100644 --- a/dot/peerset/peerstate.go +++ b/dot/peerset/peerstate.go @@ -102,12 +102,12 @@ type PeersState struct { // since, single Info can also manage the flow. sets []Info - peerStateRWMutex sync.RWMutex + sync.RWMutex } func (ps *PeersState) getNode(p peer.ID) (*node, error) { - ps.peerStateRWMutex.RLock() - defer ps.peerStateRWMutex.RUnlock() + ps.RLock() + defer ps.RUnlock() if n, ok := ps.nodes[p]; ok { return n, nil } @@ -149,8 +149,8 @@ func (ps *PeersState) getSetLength() int { // peerStatus returns the status of peer based on its connection state // i.e. connectedPeer, notConnectedPeer or unknownPeer. func (ps *PeersState) peerStatus(set int, peerID peer.ID) string { - ps.peerStateRWMutex.RLock() - defer ps.peerStateRWMutex.RUnlock() + ps.RLock() + defer ps.RUnlock() node, has := ps.nodes[peerID] if !has { @@ -169,8 +169,8 @@ func (ps *PeersState) peerStatus(set int, peerID peer.ID) string { // peers return the list of all the peers we know of. func (ps *PeersState) peers() []peer.ID { - ps.peerStateRWMutex.RLock() - defer ps.peerStateRWMutex.RUnlock() + ps.RLock() + defer ps.RUnlock() peerIDs := make([]peer.ID, 0, len(ps.nodes)) for k := range ps.nodes { @@ -181,8 +181,8 @@ func (ps *PeersState) peers() []peer.ID { // sortedPeers returns the list of peers we are connected to of a specific set. func (ps *PeersState) sortedPeers(idx int) peer.IDSlice { - ps.peerStateRWMutex.RLock() - defer ps.peerStateRWMutex.RUnlock() + ps.RLock() + defer ps.RUnlock() if len(ps.sets) == 0 || len(ps.sets) < idx { logger.Debug("peer state doesn't have info for the provided index") @@ -220,8 +220,8 @@ func (ps *PeersState) sortedPeers(idx int) peer.IDSlice { } func (ps *PeersState) updateReputationByTick(peerID peer.ID) (after Reputation, err error) { - ps.peerStateRWMutex.Lock() - defer ps.peerStateRWMutex.Unlock() + ps.Lock() + defer ps.Unlock() node, has := ps.nodes[peerID] if !has { @@ -239,8 +239,8 @@ func (ps *PeersState) updateReputationByTick(peerID peer.ID) (after Reputation, func (ps *PeersState) addReputation(peerID peer.ID, change ReputationChange) ( newReputation Reputation, err error) { - ps.peerStateRWMutex.Lock() - defer ps.peerStateRWMutex.Unlock() + ps.Lock() + defer ps.Unlock() node, has := ps.nodes[peerID] if !has { @@ -255,8 +255,8 @@ func (ps *PeersState) addReputation(peerID peer.ID, change ReputationChange) ( // highestNotConnectedPeer returns the peer with the highest Reputation and that we are not connected to. func (ps *PeersState) highestNotConnectedPeer(set int) peer.ID { - ps.peerStateRWMutex.RLock() - defer ps.peerStateRWMutex.RUnlock() + ps.RLock() + defer ps.RUnlock() maxRep := math.MinInt32 var higestPeerID peer.ID @@ -289,8 +289,8 @@ func (ps *PeersState) hasFreeIncomingSlot(set int) bool { // addNoSlotNode adds a node to the list of nodes that don't occupy slots. // has no effect if the node was already in the group. func (ps *PeersState) addNoSlotNode(idx int, peerID peer.ID) error { - ps.peerStateRWMutex.Lock() - defer ps.peerStateRWMutex.Unlock() + ps.Lock() + defer ps.Unlock() if _, ok := ps.sets[idx].noSlotNodes[peerID]; ok { logger.Debugf("peer %s already exists in no slot node", peerID) @@ -316,8 +316,8 @@ func (ps *PeersState) addNoSlotNode(idx int, peerID peer.ID) error { } func (ps *PeersState) removeNoSlotNode(idx int, peerID peer.ID) error { - ps.peerStateRWMutex.Lock() - defer ps.peerStateRWMutex.Unlock() + ps.Lock() + defer ps.Unlock() if _, ok := ps.sets[idx].noSlotNodes[peerID]; !ok { logger.Debugf("peer %s is not in no-slot node map", peerID) @@ -344,8 +344,8 @@ func (ps *PeersState) removeNoSlotNode(idx int, peerID peer.ID) error { // disconnect updates the node status to the notConnected state. // It should be called only when the node is in connected state. func (ps *PeersState) disconnect(idx int, peerID peer.ID) error { - ps.peerStateRWMutex.Lock() - defer ps.peerStateRWMutex.Unlock() + ps.Lock() + defer ps.Unlock() info := ps.sets[idx] node, has := ps.nodes[peerID] @@ -376,8 +376,8 @@ func (ps *PeersState) disconnect(idx int, peerID peer.ID) error { // discover takes input for set id and create a node and insert in the list. // the initial Reputation of the peer will be 0 and ingoing notMember state. func (ps *PeersState) discover(set int, peerID peer.ID) { - ps.peerStateRWMutex.Lock() - defer ps.peerStateRWMutex.Unlock() + ps.Lock() + defer ps.Unlock() numSet := len(ps.sets) @@ -390,8 +390,8 @@ func (ps *PeersState) discover(set int, peerID peer.ID) { } func (ps *PeersState) lastConnectedAndDiscovered(set int, peerID peer.ID) (time.Time, error) { - ps.peerStateRWMutex.RLock() - defer ps.peerStateRWMutex.RUnlock() + ps.RLock() + defer ps.RUnlock() node, has := ps.nodes[peerID] if !has { @@ -407,8 +407,8 @@ func (ps *PeersState) lastConnectedAndDiscovered(set int, peerID peer.ID) (time. // forgetPeer removes the peer with reputation 0 from the peerSet. func (ps *PeersState) forgetPeer(set int, peerID peer.ID) error { - ps.peerStateRWMutex.Lock() - defer ps.peerStateRWMutex.Unlock() + ps.Lock() + defer ps.Unlock() node, has := ps.nodes[peerID] if !has { @@ -444,8 +444,8 @@ func (ps *PeersState) forgetPeer(set int, peerID peer.ID) error { // If the slots are full, the node stays "not connected" and we return the error ErrOutgoingSlotsUnavailable. // non slot occupying nodes don't count towards the number of slots. func (ps *PeersState) tryOutgoing(setID int, peerID peer.ID) error { - ps.peerStateRWMutex.Lock() - defer ps.peerStateRWMutex.Unlock() + ps.Lock() + defer ps.Unlock() _, isNoSlotNode := ps.sets[setID].noSlotNodes[peerID] @@ -471,8 +471,8 @@ func (ps *PeersState) tryOutgoing(setID int, peerID peer.ID) error { // If the slots are full, the node stays "not connected" and we return Err. // non slot occupying nodes don't count towards the number of slots. func (ps *PeersState) tryAcceptIncoming(setID int, peerID peer.ID) error { - ps.peerStateRWMutex.Lock() - defer ps.peerStateRWMutex.Unlock() + ps.Lock() + defer ps.Unlock() var isNoSlotOccupied bool if _, ok := ps.sets[setID].noSlotNodes[peerID]; ok {