Skip to content

Commit

Permalink
chore: use sync.Mutex composition
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Feb 7, 2022
1 parent 55dca9c commit f0c0e10
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
12 changes: 6 additions & 6 deletions dot/peerset/peerset.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func newReputationChange(value Reputation, reason string) ReputationChange {

// PeerSet is a container for all the components of a peerSet.
type PeerSet struct {
peerSetMutex sync.Mutex
peerState *PeersState
sync.Mutex
peerState *PeersState

reservedNode map[peer.ID]struct{}
// TODO: this will be useful for reserved only mode
Expand Down Expand Up @@ -213,8 +213,8 @@ func reputationTick(reput Reputation) Reputation {
// updateTime updates the value of latestTimeUpdate and performs all the updates that
// happen over time, such as Reputation increases for staying connected.
func (ps *PeerSet) updateTime() error {
ps.peerSetMutex.Lock()
defer ps.peerSetMutex.Unlock()
ps.Lock()
defer ps.Unlock()

currTime := time.Now()
// identify the time difference between current time and last update time for peer reputation in seconds.
Expand Down Expand Up @@ -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:
Expand Down
62 changes: 31 additions & 31 deletions dot/peerset/peerstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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]
Expand Down Expand Up @@ -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)

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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]

Expand All @@ -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 {
Expand Down

0 comments on commit f0c0e10

Please sign in to comment.