From cd50bf0b39c5999bc7ce3e32c26a1020bb3d6d17 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 11 Mar 2024 17:38:24 -0400 Subject: [PATCH] refactor: add a rw mutex lock to all getter/setter methods --- net/peer.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/net/peer.go b/net/peer.go index f3127e3..5678a0a 100644 --- a/net/peer.go +++ b/net/peer.go @@ -5,6 +5,7 @@ import ( "go.uber.org/zap" "net" "net/url" + "sync" ) var ( @@ -58,13 +59,18 @@ type BasePeer struct { socket interface{} id *encoding.NodeId handshaked bool + lock sync.RWMutex } func (b *BasePeer) IsConnected() bool { + b.lock.RLock() + defer b.lock.RUnlock() return b.isConnected } func (b *BasePeer) SetConnected(isConnected bool) { + b.lock.Lock() + defer b.lock.Unlock() b.isConnected = isConnected } @@ -99,40 +105,60 @@ func (b *BasePeer) SetIP(ip net.Addr) { } func (b *BasePeer) Challenge() []byte { + b.lock.RLock() + defer b.lock.RUnlock() return b.challenge } func (b *BasePeer) SetChallenge(challenge []byte) { + b.lock.Lock() + defer b.lock.Unlock() b.challenge = challenge } func (b *BasePeer) Socket() interface{} { + b.lock.RLock() + defer b.lock.RUnlock() return b.socket } func (b *BasePeer) SetSocket(socket interface{}) { + b.lock.Lock() + defer b.lock.Unlock() b.socket = socket } func (b *BasePeer) Id() *encoding.NodeId { + b.lock.RLock() + defer b.lock.RUnlock() return b.id } func (b *BasePeer) SetId(id *encoding.NodeId) { + b.lock.Lock() + defer b.lock.Unlock() b.id = id } func (b *BasePeer) SetConnectionURIs(uris []*url.URL) { + b.lock.Lock() + defer b.lock.Unlock() b.connectionURIs = uris } func (b *BasePeer) ConnectionURIs() []*url.URL { + b.lock.RLock() + b.lock.RUnlock() return b.connectionURIs } func (b *BasePeer) IsHandshakeDone() bool { + b.lock.RLock() + defer b.lock.RUnlock() return b.handshaked } func (b *BasePeer) SetHandshakeDone(status bool) { + b.lock.Lock() + defer b.lock.Unlock() b.handshaked = status }