diff --git a/les/client_handler.go b/les/client_handler.go index 6e7d88fc5a..25a508d42d 100644 --- a/les/client_handler.go +++ b/les/client_handler.go @@ -21,6 +21,7 @@ import ( "math/big" "math/rand" "sync" + "sync/atomic" "time" "github.com/ethereum/go-ethereum/common" @@ -143,8 +144,8 @@ func (h *clientHandler) handle(p *serverPeer, noInitAnnounce bool) error { } // Mark the peer starts to be served. - p.serving.Store(true) - defer p.serving.Store(false) + atomic.StoreUint32(&p.serving, 1) + defer atomic.StoreUint32(&p.serving, 0) // Spawn a main loop to handle all incoming messages. for { diff --git a/les/peer.go b/les/peer.go index 909d912e94..100cc7e4f1 100644 --- a/les/peer.go +++ b/les/peer.go @@ -121,13 +121,13 @@ type peerCommons struct { *p2p.Peer rw p2p.MsgReadWriter - id string // Peer identity. - version int // Protocol version negotiated. - network uint64 // Network ID being on. - frozen atomic.Bool // Flag whether the peer is frozen. - announceType uint64 // New block announcement type. - serving atomic.Bool // The status indicates the peer is served. - headInfo blockInfo // Last announced block information. + id string // Peer identity. + version int // Protocol version negotiated. + network uint64 // Network ID being on. + frozen uint32 // Flag whether the peer is frozen. + announceType uint64 // New block announcement type. + serving uint32 // The status indicates the peer is served. + headInfo blockInfo // Last announced block information. // Background task queue for caching peer tasks and executing in order. sendQueue *utils.ExecQueue @@ -143,7 +143,7 @@ type peerCommons struct { // isFrozen returns true if the client is frozen or the server has put our // client in frozen state func (p *peerCommons) isFrozen() bool { - return p.frozen.Load() + return atomic.LoadUint32(&p.frozen) != 0 } // canQueue returns an indicator whether the peer can queue an operation. @@ -398,7 +398,7 @@ func (p *serverPeer) rejectUpdate(size uint64) bool { // freeze processes Stop messages from the given server and set the status as // frozen. func (p *serverPeer) freeze() { - if p.frozen.CompareAndSwap(false, true) { + if atomic.CompareAndSwapUint32(&p.frozen, 0, 1) { p.sendQueue.Clear() } } @@ -406,7 +406,7 @@ func (p *serverPeer) freeze() { // unfreeze processes Resume messages from the given server and set the status // as unfrozen. func (p *serverPeer) unfreeze() { - p.frozen.Store(false) + atomic.StoreUint32(&p.frozen, 0) } // sendRequest send a request to the server based on the given message type @@ -823,11 +823,11 @@ func (p *clientPeer) freeze() { if p.version < lpv3 { // if Stop/Resume is not supported then just drop the peer after setting // its frozen status permanently - p.frozen.Store(true) + atomic.StoreUint32(&p.frozen, 1) p.Peer.Disconnect(p2p.DiscUselessPeer) return } - if !p.frozen.Swap(true) { + if atomic.SwapUint32(&p.frozen, 1) == 0 { go func() { p.sendStop() time.Sleep(freezeTimeBase + time.Duration(rand.Int63n(int64(freezeTimeRandom)))) @@ -840,7 +840,7 @@ func (p *clientPeer) freeze() { time.Sleep(freezeCheckPeriod) continue } - p.frozen.Store(false) + atomic.StoreUint32(&p.frozen, 0) p.sendResume(bufValue) return } diff --git a/les/server_handler.go b/les/server_handler.go index 418974c37c..39c7ace1c9 100644 --- a/les/server_handler.go +++ b/les/server_handler.go @@ -19,6 +19,7 @@ package les import ( "errors" "sync" + "sync/atomic" "time" "github.com/ethereum/go-ethereum/common" @@ -163,8 +164,8 @@ func (h *serverHandler) handle(p *clientPeer) error { }() // Mark the peer as being served. - p.serving.Store(true) - defer p.serving.Store(false) + atomic.StoreUint32(&p.serving, 1) + defer atomic.StoreUint32(&p.serving, 0) // Spawn a main loop to handle all incoming messages. for { diff --git a/les/test_helper.go b/les/test_helper.go index ead97ddd17..70f78498d4 100644 --- a/les/test_helper.go +++ b/les/test_helper.go @@ -25,6 +25,7 @@ import ( "crypto/rand" "fmt" "math/big" + "sync/atomic" "testing" "time" @@ -379,7 +380,7 @@ func newTestPeerPair(name string, version int, server *serverHandler, client *cl return nil, nil, fmt.Errorf("failed to establish protocol connection %v", err) default: } - if peer1.serving.Load() && peer2.serving.Load() { + if atomic.LoadUint32(&peer1.serving) == 1 && atomic.LoadUint32(&peer2.serving) == 1 { break } time.Sleep(50 * time.Millisecond) @@ -440,7 +441,7 @@ func (client *testClient) newRawPeer(t *testing.T, name string, version int, rec return nil, nil, nil default: } - if peer.serving.Load() { + if atomic.LoadUint32(&peer.serving) == 1 { break } time.Sleep(50 * time.Millisecond) @@ -504,7 +505,7 @@ func (server *testServer) newRawPeer(t *testing.T, name string, version int) (*t return nil, nil, nil default: } - if peer.serving.Load() { + if atomic.LoadUint32(&peer.serving) == 1 { break } time.Sleep(50 * time.Millisecond) diff --git a/les/ulc_test.go b/les/ulc_test.go index 791bc28853..9a29a24cee 100644 --- a/les/ulc_test.go +++ b/les/ulc_test.go @@ -20,6 +20,7 @@ import ( "crypto/rand" "fmt" "net" + "sync/atomic" "testing" "time" @@ -135,7 +136,7 @@ func connect(server *serverHandler, serverId enode.ID, client *clientHandler, pr return nil, nil, fmt.Errorf("failed to establish protocol connection %v", err) default: } - if peer1.serving.Load() && peer2.serving.Load() { + if atomic.LoadUint32(&peer1.serving) == 1 && atomic.LoadUint32(&peer2.serving) == 1 { break } time.Sleep(50 * time.Millisecond)