@@ -65,8 +65,7 @@ type Subscriber struct {
65
65
appSubActive bool
66
66
hasInitProg bool
67
67
lock sync.RWMutex
68
- running atomic.Bool
69
- closed bool
68
+ closed atomic.Bool
70
69
}
71
70
72
71
type namedSubscription struct {
@@ -94,22 +93,49 @@ func New(opts Options) *Subscriber {
94
93
}
95
94
96
95
func (s * Subscriber ) Run (ctx context.Context ) error {
97
- if ! s .running .CompareAndSwap (false , true ) {
98
- return errors .New ("subscriber is already running" )
96
+ <- ctx .Done ()
97
+ s .closed .Store (true )
98
+ return nil
99
+ }
100
+
101
+ func (s * Subscriber ) StopAllSubscriptionsForever () {
102
+ s .lock .Lock ()
103
+
104
+ s .closed .Store (true )
105
+
106
+ var wg sync.WaitGroup
107
+ for _ , psubs := range s .appSubs {
108
+ wg .Add (len (psubs ))
109
+ for _ , sub := range psubs {
110
+ go func (sub * namedSubscription ) {
111
+ sub .Stop ()
112
+ wg .Done ()
113
+ }(sub )
114
+ }
99
115
}
100
116
101
- <- ctx .Done ()
117
+ for _ , psubs := range s .streamSubs {
118
+ wg .Add (len (psubs ))
119
+ for _ , sub := range psubs {
120
+ go func () {
121
+ sub .Stop ()
122
+ wg .Done ()
123
+ }()
124
+ }
125
+ }
102
126
103
- s .StopAllSubscriptionsForever ()
127
+ s .appSubs = make (map [string ][]* namedSubscription )
128
+ s .streamSubs = make (map [string ]map [rtpubsub.ConnectionID ]* namedSubscription )
129
+ s .lock .Unlock ()
104
130
105
- return nil
131
+ wg . Wait ()
106
132
}
107
133
108
134
func (s * Subscriber ) ReloadPubSub (name string ) error {
109
135
s .lock .Lock ()
110
136
defer s .lock .Unlock ()
111
137
112
- if s .closed {
138
+ if s .closed . Load () {
113
139
return nil
114
140
}
115
141
@@ -133,8 +159,8 @@ func (s *Subscriber) StartStreamerSubscription(subscription *subapi.Subscription
133
159
s .lock .Unlock ()
134
160
}()
135
161
136
- if s .closed {
137
- return fmt . Errorf ( "streaming subscriber %s with ID %d is closed", subscription . Name , connectionID )
162
+ if s .closed . Load () {
163
+ return apierrors . PubSub ( "" ). WithMetadata ( nil ). DeserializeError ( errors . New ( " subscriber is closed") )
138
164
}
139
165
140
166
sub , found := s .compStore .GetStreamSubscription (subscription )
@@ -172,10 +198,6 @@ func (s *Subscriber) StopStreamerSubscription(subscription *subapi.Subscription,
172
198
s .lock .Unlock ()
173
199
}()
174
200
175
- if s .closed {
176
- return
177
- }
178
-
179
201
subscriptions , ok := s .streamSubs [subscription .Spec .Pubsubname ]
180
202
if ! ok {
181
203
return
@@ -197,7 +219,7 @@ func (s *Subscriber) ReloadDeclaredAppSubscription(name, pubsubName string) erro
197
219
s .lock .Lock ()
198
220
defer s .lock .Unlock ()
199
221
200
- if ! s .appSubActive || s .closed {
222
+ if ! s .appSubActive || s .closed . Load () {
201
223
return nil
202
224
}
203
225
@@ -240,12 +262,21 @@ func (s *Subscriber) StopPubSub(name string) {
240
262
s .lock .Lock ()
241
263
defer s .lock .Unlock ()
242
264
265
+ var wg sync.WaitGroup
266
+ wg .Add (len (s .appSubs [name ]) + len (s .streamSubs [name ]))
243
267
for _ , sub := range s .appSubs [name ] {
244
- sub .Stop ()
268
+ go func (sub * namedSubscription ) {
269
+ sub .Stop ()
270
+ wg .Done ()
271
+ }(sub )
245
272
}
246
273
for _ , sub := range s .streamSubs [name ] {
247
- sub .Stop ()
274
+ go func (sub * namedSubscription ) {
275
+ sub .Stop ()
276
+ wg .Done ()
277
+ }(sub )
248
278
}
279
+ wg .Wait ()
249
280
250
281
s .appSubs [name ] = nil
251
282
s .streamSubs [name ] = nil
@@ -255,7 +286,7 @@ func (s *Subscriber) StartAppSubscriptions() error {
255
286
s .lock .Lock ()
256
287
defer s .lock .Unlock ()
257
288
258
- if s .appSubActive || s .closed {
289
+ if s .appSubActive || s .closed . Load () {
259
290
return nil
260
291
}
261
292
@@ -265,11 +296,19 @@ func (s *Subscriber) StartAppSubscriptions() error {
265
296
266
297
s .appSubActive = true
267
298
299
+ var wg sync.WaitGroup
268
300
for _ , subs := range s .appSubs {
301
+ wg .Add (len (subs ))
269
302
for _ , sub := range subs {
270
- sub .Stop ()
303
+ go func (sub * namedSubscription ) {
304
+ sub .Stop ()
305
+ wg .Done ()
306
+ }(sub )
271
307
}
272
308
}
309
+
310
+ wg .Wait ()
311
+
273
312
s .appSubs = make (map [string ][]* namedSubscription )
274
313
275
314
var errs []error
@@ -301,36 +340,19 @@ func (s *Subscriber) StopAppSubscriptions() {
301
340
302
341
s .appSubActive = false
303
342
343
+ var wg sync.WaitGroup
304
344
for _ , psub := range s .appSubs {
345
+ wg .Add (len (psub ))
305
346
for _ , sub := range psub {
306
- sub .Stop ()
307
- }
308
- }
309
-
310
- s .appSubs = make (map [string ][]* namedSubscription )
311
- }
312
-
313
- func (s * Subscriber ) StopAllSubscriptionsForever () {
314
- s .lock .Lock ()
315
- defer func () {
316
- s .lock .Unlock ()
317
- }()
318
-
319
- s .closed = true
320
-
321
- for _ , psubs := range s .appSubs {
322
- for _ , sub := range psubs {
323
- sub .Stop ()
324
- }
325
- }
326
- for _ , psubs := range s .streamSubs {
327
- for _ , sub := range psubs {
328
- sub .Stop ()
347
+ go func (sub * namedSubscription ) {
348
+ sub .Stop ()
349
+ wg .Done ()
350
+ }(sub )
329
351
}
330
352
}
353
+ wg .Wait ()
331
354
332
355
s .appSubs = make (map [string ][]* namedSubscription )
333
- s .streamSubs = make (map [string ]map [rtpubsub.ConnectionID ]* namedSubscription )
334
356
}
335
357
336
358
func (s * Subscriber ) InitProgramaticSubscriptions (ctx context.Context ) error {
@@ -340,12 +362,19 @@ func (s *Subscriber) InitProgramaticSubscriptions(ctx context.Context) error {
340
362
}
341
363
342
364
func (s * Subscriber ) reloadPubSubStream (name string , pubsub * rtpubsub.PubsubItem ) error {
365
+ var wg sync.WaitGroup
366
+ wg .Add (len (s .streamSubs [name ]))
343
367
for _ , sub := range s .streamSubs [name ] {
344
- sub .Stop ()
368
+ go func (sub * namedSubscription ) {
369
+ sub .Stop ()
370
+ wg .Done ()
371
+ }(sub )
345
372
}
373
+ wg .Wait ()
374
+
346
375
s .streamSubs [name ] = nil
347
376
348
- if s .closed || pubsub == nil {
377
+ if s .closed . Load () || pubsub == nil {
349
378
return nil
350
379
}
351
380
@@ -370,13 +399,19 @@ func (s *Subscriber) reloadPubSubStream(name string, pubsub *rtpubsub.PubsubItem
370
399
}
371
400
372
401
func (s * Subscriber ) reloadPubSubApp (name string , pubsub * rtpubsub.PubsubItem ) error {
402
+ var wg sync.WaitGroup
403
+ wg .Add (len (s .appSubs [name ]))
373
404
for _ , sub := range s .appSubs [name ] {
374
- sub .Stop ()
405
+ go func (sub * namedSubscription ) {
406
+ sub .Stop ()
407
+ wg .Done ()
408
+ }(sub )
375
409
}
410
+ wg .Wait ()
376
411
377
412
s .appSubs [name ] = nil
378
413
379
- if ! s .appSubActive || s .closed || pubsub == nil {
414
+ if ! s .appSubActive || s .closed . Load () || pubsub == nil {
380
415
return nil
381
416
}
382
417
0 commit comments