@@ -11,6 +11,7 @@ import (
11
11
pb "github.com/libp2p/go-libp2p-pubsub/pb"
12
12
13
13
"github.com/libp2p/go-libp2p-core/peer"
14
+ "go.uber.org/atomic"
14
15
)
15
16
16
17
// ErrTopicClosed is returned if a Topic is utilized after it has been closed
@@ -30,8 +31,7 @@ type Topic struct {
30
31
evtHandlerMux sync.RWMutex
31
32
evtHandlers map [* TopicEventHandler ]struct {}
32
33
33
- mux sync.RWMutex
34
- closed bool
34
+ closed atomic.Bool
35
35
}
36
36
37
37
// String returns the topic associated with t
@@ -47,10 +47,7 @@ func (t *Topic) SetScoreParams(p *TopicScoreParams) error {
47
47
return fmt .Errorf ("invalid topic score parameters: %w" , err )
48
48
}
49
49
50
- t .mux .Lock ()
51
- defer t .mux .Unlock ()
52
-
53
- if t .closed {
50
+ if t .closed .Load () {
54
51
return ErrTopicClosed
55
52
}
56
53
@@ -84,9 +81,7 @@ func (t *Topic) SetScoreParams(p *TopicScoreParams) error {
84
81
// EventHandler creates a handle for topic specific events
85
82
// Multiple event handlers may be created and will operate independently of each other
86
83
func (t * Topic ) EventHandler (opts ... TopicEventHandlerOpt ) (* TopicEventHandler , error ) {
87
- t .mux .RLock ()
88
- defer t .mux .RUnlock ()
89
- if t .closed {
84
+ if t .closed .Load () {
90
85
return nil , ErrTopicClosed
91
86
}
92
87
@@ -141,9 +136,7 @@ func (t *Topic) sendNotification(evt PeerEvent) {
141
136
// Note that subscription is not an instantaneous operation. It may take some time
142
137
// before the subscription is processed by the pubsub main loop and propagated to our peers.
143
138
func (t * Topic ) Subscribe (opts ... SubOpt ) (* Subscription , error ) {
144
- t .mux .RLock ()
145
- defer t .mux .RUnlock ()
146
- if t .closed {
139
+ if t .closed .Load () {
147
140
return nil , ErrTopicClosed
148
141
}
149
142
@@ -184,9 +177,7 @@ func (t *Topic) Subscribe(opts ...SubOpt) (*Subscription, error) {
184
177
// cancel function. Subsequent calls increase the reference counter.
185
178
// To completely disable the relay, all references must be cancelled.
186
179
func (t * Topic ) Relay () (RelayCancelFunc , error ) {
187
- t .mux .RLock ()
188
- defer t .mux .RUnlock ()
189
- if t .closed {
180
+ if t .closed .Load () {
190
181
return nil , ErrTopicClosed
191
182
}
192
183
@@ -215,16 +206,14 @@ type ProvideKey func() (crypto.PrivKey, peer.ID)
215
206
type PublishOptions struct {
216
207
ready RouterReady
217
208
customKey ProvideKey
218
- local bool
209
+ local bool
219
210
}
220
211
221
212
type PubOpt func (pub * PublishOptions ) error
222
213
223
214
// Publish publishes data to topic.
224
215
func (t * Topic ) Publish (ctx context.Context , data []byte , opts ... PubOpt ) error {
225
- t .mux .RLock ()
226
- defer t .mux .RUnlock ()
227
- if t .closed {
216
+ if t .closed .Load () {
228
217
return ErrTopicClosed
229
218
}
230
219
@@ -347,9 +336,7 @@ func WithSecretKeyAndPeerId(key crypto.PrivKey, pid peer.ID) PubOpt {
347
336
// Close closes down the topic. Will return an error unless there are no active event handlers or subscriptions.
348
337
// Does not error if the topic is already closed.
349
338
func (t * Topic ) Close () error {
350
- t .mux .Lock ()
351
- defer t .mux .Unlock ()
352
- if t .closed {
339
+ if t .closed .Load () {
353
340
return nil
354
341
}
355
342
@@ -364,17 +351,15 @@ func (t *Topic) Close() error {
364
351
err := <- req .resp
365
352
366
353
if err == nil {
367
- t .closed = true
354
+ t .closed . Swap ( true )
368
355
}
369
356
370
357
return err
371
358
}
372
359
373
360
// ListPeers returns a list of peers we are connected to in the given topic.
374
361
func (t * Topic ) ListPeers () []peer.ID {
375
- t .mux .RLock ()
376
- defer t .mux .RUnlock ()
377
- if t .closed {
362
+ if t .closed .Load () {
378
363
return []peer.ID {}
379
364
}
380
365
0 commit comments