From 110f44093e4f0202dc2308490ecd1ac3ab43b881 Mon Sep 17 00:00:00 2001 From: sukun Date: Mon, 13 Feb 2023 17:42:21 +0530 Subject: [PATCH] use atomic.Bool instead of int32 operations --- p2p/host/eventbus/basic.go | 6 +++--- p2p/net/connmgr/decay.go | 10 +++++----- p2p/net/swarm/dial_sync_test.go | 4 ++-- p2p/protocol/circuitv1/relay/relay.go | 4 ++-- p2p/protocol/circuitv2/relay/relay.go | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/p2p/host/eventbus/basic.go b/p2p/host/eventbus/basic.go index 5e11fad875..ae4778873b 100644 --- a/p2p/host/eventbus/basic.go +++ b/p2p/host/eventbus/basic.go @@ -27,13 +27,13 @@ type emitter struct { n *node w *wildcardNode typ reflect.Type - closed int32 + closed atomic.Bool dropper func(reflect.Type) metricsTracer MetricsTracer } func (e *emitter) Emit(evt interface{}) error { - if atomic.LoadInt32(&e.closed) != 0 { + if e.closed.Load() { return fmt.Errorf("emitter is closed") } @@ -47,7 +47,7 @@ func (e *emitter) Emit(evt interface{}) error { } func (e *emitter) Close() error { - if !atomic.CompareAndSwapInt32(&e.closed, 0, 1) { + if !e.closed.CompareAndSwap(false, true) { return fmt.Errorf("closed an emitter more than once") } if atomic.AddInt32(&e.n.nEmitters, -1) == 0 { diff --git a/p2p/net/connmgr/decay.go b/p2p/net/connmgr/decay.go index c10214cb8a..ca39de8693 100644 --- a/p2p/net/connmgr/decay.go +++ b/p2p/net/connmgr/decay.go @@ -291,8 +291,8 @@ type decayingTag struct { bumpFn connmgr.BumpFn // closed marks this tag as closed, so that if it's bumped after being - // closed, we can return an error. 0 = false; 1 = true; guarded by atomic. - closed int32 + // closed, we can return an error. + closed atomic.Bool } var _ connmgr.DecayingTag = (*decayingTag)(nil) @@ -307,7 +307,7 @@ func (t *decayingTag) Interval() time.Duration { // Bump bumps a tag for this peer. func (t *decayingTag) Bump(p peer.ID, delta int) error { - if atomic.LoadInt32(&t.closed) == 1 { + if t.closed.Load() { return fmt.Errorf("decaying tag %s had been closed; no further bumps are accepted", t.name) } @@ -324,7 +324,7 @@ func (t *decayingTag) Bump(p peer.ID, delta int) error { } func (t *decayingTag) Remove(p peer.ID) error { - if atomic.LoadInt32(&t.closed) == 1 { + if t.closed.Load() { return fmt.Errorf("decaying tag %s had been closed; no further removals are accepted", t.name) } @@ -341,7 +341,7 @@ func (t *decayingTag) Remove(p peer.ID) error { } func (t *decayingTag) Close() error { - if !atomic.CompareAndSwapInt32(&t.closed, 0, 1) { + if !t.closed.CompareAndSwap(false, true) { log.Warnf("duplicate decaying tag closure: %s; skipping", t.name) return nil } diff --git a/p2p/net/swarm/dial_sync_test.go b/p2p/net/swarm/dial_sync_test.go index e2b8e88dac..d7131a21ed 100644 --- a/p2p/net/swarm/dial_sync_test.go +++ b/p2p/net/swarm/dial_sync_test.go @@ -162,7 +162,7 @@ func TestDialSyncAllCancel(t *testing.T) { } func TestFailFirst(t *testing.T) { - var count int32 + var handledFirst atomic.Bool dialErr := fmt.Errorf("gophers ate the modem") f := func(p peer.ID, reqch <-chan dialRequest) { go func() { @@ -172,7 +172,7 @@ func TestFailFirst(t *testing.T) { return } - if atomic.CompareAndSwapInt32(&count, 0, 1) { + if handledFirst.CompareAndSwap(false, true) { req.resch <- dialResponse{err: dialErr} } else { req.resch <- dialResponse{conn: new(Conn)} diff --git a/p2p/protocol/circuitv1/relay/relay.go b/p2p/protocol/circuitv1/relay/relay.go index 3b6f7adc85..296ef306d3 100644 --- a/p2p/protocol/circuitv1/relay/relay.go +++ b/p2p/protocol/circuitv1/relay/relay.go @@ -37,7 +37,7 @@ const ( ) type Relay struct { - closed int32 + closed atomic.Bool ctx context.Context cancel context.CancelFunc @@ -83,7 +83,7 @@ func NewRelay(h host.Host, opts ...Option) (*Relay, error) { } func (r *Relay) Close() error { - if atomic.CompareAndSwapInt32(&r.closed, 0, 1) { + if r.closed.CompareAndSwap(false, true) { r.host.RemoveStreamHandler(ProtoID) r.scope.Done() r.cancel() diff --git a/p2p/protocol/circuitv2/relay/relay.go b/p2p/protocol/circuitv2/relay/relay.go index 5e2d9f1835..e24cd7f327 100644 --- a/p2p/protocol/circuitv2/relay/relay.go +++ b/p2p/protocol/circuitv2/relay/relay.go @@ -41,7 +41,7 @@ var log = logging.Logger("relay") // Relay is the (limited) relay service object. type Relay struct { - closed uint32 + closed atomic.Bool ctx context.Context cancel func() @@ -104,7 +104,7 @@ func New(h host.Host, opts ...Option) (*Relay, error) { } func (r *Relay) Close() error { - if atomic.CompareAndSwapUint32(&r.closed, 0, 1) { + if r.closed.CompareAndSwap(false, true) { r.host.RemoveStreamHandler(proto.ProtoIDv2Hop) r.scope.Done() r.cancel()