7
7
"fmt"
8
8
"math"
9
9
"net/http"
10
- "sync"
11
10
12
11
"github.com/libp2p/go-libp2p/core/host"
13
12
"github.com/libp2p/go-libp2p/core/network"
@@ -34,16 +33,11 @@ var (
34
33
)
35
34
36
35
type WakuFilterLightNode struct {
37
- sync.RWMutex
38
- started bool
39
-
40
- cancel context.CancelFunc
41
- ctx context.Context
36
+ * protocol.CommonService
42
37
h host.Host
43
38
broadcaster relay.Broadcaster //TODO: Move the broadcast functionality outside of relay client to a higher SDK layer.s
44
39
timesource timesource.Timesource
45
40
metrics Metrics
46
- wg * sync.WaitGroup
47
41
log * zap.Logger
48
42
subscriptions * SubscriptionsMap
49
43
pm * peermanager.PeerManager
@@ -59,9 +53,6 @@ type WakuFilterPushResult struct {
59
53
PeerID peer.ID
60
54
}
61
55
62
- var errNotStarted = errors .New ("not started" )
63
- var errAlreadyStarted = errors .New ("already started" )
64
-
65
56
// NewWakuFilterLightnode returns a new instance of Waku Filter struct setup according to the chosen parameter and options
66
57
// Note that broadcaster is optional.
67
58
// Takes an optional peermanager if WakuFilterLightnode is being created along with WakuNode.
@@ -72,8 +63,8 @@ func NewWakuFilterLightNode(broadcaster relay.Broadcaster, pm *peermanager.PeerM
72
63
wf .log = log .Named ("filterv2-lightnode" )
73
64
wf .broadcaster = broadcaster
74
65
wf .timesource = timesource
75
- wf .wg = & sync.WaitGroup {}
76
66
wf .pm = pm
67
+ wf .CommonService = protocol .NewCommonService ()
77
68
wf .metrics = newMetrics (reg )
78
69
79
70
return wf
@@ -85,59 +76,36 @@ func (wf *WakuFilterLightNode) SetHost(h host.Host) {
85
76
}
86
77
87
78
func (wf * WakuFilterLightNode ) Start (ctx context.Context ) error {
88
- wf .Lock ()
89
- defer wf .Unlock ()
79
+ return wf .CommonService .Start (ctx , wf .start )
90
80
91
- if wf .started {
92
- return errAlreadyStarted
93
- }
94
-
95
- wf .wg .Wait () // Wait for any goroutines to stop
81
+ }
96
82
97
- ctx , cancel := context .WithCancel (ctx )
98
- wf .cancel = cancel
99
- wf .ctx = ctx
83
+ func (wf * WakuFilterLightNode ) start () error {
100
84
wf .subscriptions = NewSubscriptionMap (wf .log )
101
- wf .started = true
102
-
103
- wf .h .SetStreamHandlerMatch (FilterPushID_v20beta1 , protocol .PrefixTextMatch (string (FilterPushID_v20beta1 )), wf .onRequest (ctx ))
85
+ wf .h .SetStreamHandlerMatch (FilterPushID_v20beta1 , protocol .PrefixTextMatch (string (FilterPushID_v20beta1 )), wf .onRequest (wf .Context ()))
104
86
105
87
wf .log .Info ("filter-push protocol started" )
106
-
107
88
return nil
108
89
}
109
90
110
91
// Stop unmounts the filter protocol
111
92
func (wf * WakuFilterLightNode ) Stop () {
112
- wf .Lock ()
113
- defer wf .Unlock ()
114
-
115
- if ! wf .started {
116
- return
117
- }
118
-
119
- wf .cancel ()
120
-
121
- wf .h .RemoveStreamHandler (FilterPushID_v20beta1 )
122
-
123
- res , err := wf .unsubscribeAll (wf .ctx )
124
- if err != nil {
125
- wf .log .Warn ("unsubscribing from full nodes" , zap .Error (err ))
126
- }
127
-
128
- for r := range res {
129
- if r .Err != nil {
130
- wf .log .Warn ("unsubscribing from full nodes" , zap .Error (r .Err ), logging .HostID ("peerID" , r .PeerID ))
93
+ wf .CommonService .Stop (func () {
94
+ wf .h .RemoveStreamHandler (FilterPushID_v20beta1 )
95
+ res , err := wf .unsubscribeAll (wf .Context ())
96
+ if err != nil {
97
+ wf .log .Warn ("unsubscribing from full nodes" , zap .Error (err ))
131
98
}
132
99
133
- }
134
-
135
- wf .subscriptions .Clear ()
136
-
137
- wf .started = false
138
- wf .cancel = nil
100
+ for r := range res {
101
+ if r .Err != nil {
102
+ wf .log .Warn ("unsubscribing from full nodes" , zap .Error (r .Err ), logging .HostID ("peerID" , r .PeerID ))
103
+ }
139
104
140
- wf .wg .Wait ()
105
+ }
106
+ //
107
+ wf .subscriptions .Clear ()
108
+ })
141
109
}
142
110
143
111
func (wf * WakuFilterLightNode ) onRequest (ctx context.Context ) func (s network.Stream ) {
@@ -248,9 +216,8 @@ func (wf *WakuFilterLightNode) request(ctx context.Context, params *FilterSubscr
248
216
func (wf * WakuFilterLightNode ) Subscribe (ctx context.Context , contentFilter ContentFilter , opts ... FilterSubscribeOption ) (* SubscriptionDetails , error ) {
249
217
wf .RLock ()
250
218
defer wf .RUnlock ()
251
-
252
- if ! wf .started {
253
- return nil , errNotStarted
219
+ if err := wf .ErrOnNotRunning (); err != nil {
220
+ return nil , err
254
221
}
255
222
256
223
if contentFilter .Topic == "" {
@@ -285,17 +252,15 @@ func (wf *WakuFilterLightNode) Subscribe(ctx context.Context, contentFilter Cont
285
252
if err != nil {
286
253
return nil , err
287
254
}
288
-
289
255
return wf .subscriptions .NewSubscription (params .selectedPeer , contentFilter .Topic , contentFilter .ContentTopics ), nil
290
256
}
291
257
292
258
// FilterSubscription is used to obtain an object from which you could receive messages received via filter protocol
293
259
func (wf * WakuFilterLightNode ) FilterSubscription (peerID peer.ID , contentFilter ContentFilter ) (* SubscriptionDetails , error ) {
294
260
wf .RLock ()
295
261
defer wf .RUnlock ()
296
-
297
- if ! wf .started {
298
- return nil , errNotStarted
262
+ if err := wf .ErrOnNotRunning (); err != nil {
263
+ return nil , err
299
264
}
300
265
301
266
if ! wf .subscriptions .Has (peerID , contentFilter .Topic , contentFilter .ContentTopics ... ) {
@@ -319,9 +284,8 @@ func (wf *WakuFilterLightNode) getUnsubscribeParameters(opts ...FilterUnsubscrib
319
284
func (wf * WakuFilterLightNode ) Ping (ctx context.Context , peerID peer.ID ) error {
320
285
wf .RLock ()
321
286
defer wf .RUnlock ()
322
-
323
- if ! wf .started {
324
- return errNotStarted
287
+ if err := wf .ErrOnNotRunning (); err != nil {
288
+ return err
325
289
}
326
290
327
291
return wf .request (
@@ -334,9 +298,8 @@ func (wf *WakuFilterLightNode) Ping(ctx context.Context, peerID peer.ID) error {
334
298
func (wf * WakuFilterLightNode ) IsSubscriptionAlive (ctx context.Context , subscription * SubscriptionDetails ) error {
335
299
wf .RLock ()
336
300
defer wf .RUnlock ()
337
-
338
- if ! wf .started {
339
- return errNotStarted
301
+ if err := wf .ErrOnNotRunning (); err != nil {
302
+ return err
340
303
}
341
304
342
305
return wf .Ping (ctx , subscription .PeerID )
@@ -345,8 +308,7 @@ func (wf *WakuFilterLightNode) IsSubscriptionAlive(ctx context.Context, subscrip
345
308
func (wf * WakuFilterLightNode ) Subscriptions () []* SubscriptionDetails {
346
309
wf .RLock ()
347
310
defer wf .RUnlock ()
348
-
349
- if ! wf .started {
311
+ if err := wf .ErrOnNotRunning (); err != nil {
350
312
return nil
351
313
}
352
314
@@ -398,13 +360,11 @@ func (wf *WakuFilterLightNode) cleanupSubscriptions(peerID peer.ID, contentFilte
398
360
}
399
361
400
362
// Unsubscribe is used to stop receiving messages from a peer that match a content filter
401
- func (wf * WakuFilterLightNode ) Unsubscribe (ctx context.Context , contentFilter ContentFilter ,
402
- opts ... FilterUnsubscribeOption ) (<- chan WakuFilterPushResult , error ) {
363
+ func (wf * WakuFilterLightNode ) Unsubscribe (ctx context.Context , contentFilter ContentFilter , opts ... FilterUnsubscribeOption ) (<- chan WakuFilterPushResult , error ) {
403
364
wf .RLock ()
404
365
defer wf .RUnlock ()
405
-
406
- if ! wf .started {
407
- return nil , errNotStarted
366
+ if err := wf .ErrOnNotRunning (); err != nil {
367
+ return nil , err
408
368
}
409
369
410
370
if contentFilter .Topic == "" {
@@ -485,13 +445,11 @@ func (wf *WakuFilterLightNode) Unsubscribe(ctx context.Context, contentFilter Co
485
445
}
486
446
487
447
// Unsubscribe is used to stop receiving messages from a peer that match a content filter
488
- func (wf * WakuFilterLightNode ) UnsubscribeWithSubscription (ctx context.Context , sub * SubscriptionDetails ,
489
- opts ... FilterUnsubscribeOption ) (<- chan WakuFilterPushResult , error ) {
448
+ func (wf * WakuFilterLightNode ) UnsubscribeWithSubscription (ctx context.Context , sub * SubscriptionDetails , opts ... FilterUnsubscribeOption ) (<- chan WakuFilterPushResult , error ) {
490
449
wf .RLock ()
491
450
defer wf .RUnlock ()
492
-
493
- if ! wf .started {
494
- return nil , errNotStarted
451
+ if err := wf .ErrOnNotRunning (); err != nil {
452
+ return nil , err
495
453
}
496
454
497
455
var contentTopics []string
@@ -563,9 +521,8 @@ func (wf *WakuFilterLightNode) unsubscribeAll(ctx context.Context, opts ...Filte
563
521
func (wf * WakuFilterLightNode ) UnsubscribeAll (ctx context.Context , opts ... FilterUnsubscribeOption ) (<- chan WakuFilterPushResult , error ) {
564
522
wf .RLock ()
565
523
defer wf .RUnlock ()
566
-
567
- if ! wf .started {
568
- return nil , errNotStarted
524
+ if err := wf .ErrOnNotRunning (); err != nil {
525
+ return nil , err
569
526
}
570
527
571
528
return wf .unsubscribeAll (ctx , opts ... )
0 commit comments