@@ -238,9 +238,17 @@ func (c *PubSub) Unsubscribe(ctx context.Context, channels ...string) error {
238
238
c .mu .Lock ()
239
239
defer c .mu .Unlock ()
240
240
241
- for _ , channel := range channels {
242
- delete (c .channels , channel )
241
+ if len (channels ) > 0 {
242
+ for _ , channel := range channels {
243
+ delete (c .channels , channel )
244
+ }
245
+ } else {
246
+ // Unsubscribe from all channels.
247
+ for channel := range c .channels {
248
+ delete (c .channels , channel )
249
+ }
243
250
}
251
+
244
252
err := c .subscribe (ctx , "unsubscribe" , channels ... )
245
253
return err
246
254
}
@@ -251,9 +259,17 @@ func (c *PubSub) PUnsubscribe(ctx context.Context, patterns ...string) error {
251
259
c .mu .Lock ()
252
260
defer c .mu .Unlock ()
253
261
254
- for _ , pattern := range patterns {
255
- delete (c .patterns , pattern )
262
+ if len (patterns ) > 0 {
263
+ for _ , pattern := range patterns {
264
+ delete (c .patterns , pattern )
265
+ }
266
+ } else {
267
+ // Unsubscribe from all patterns.
268
+ for pattern := range c .patterns {
269
+ delete (c .patterns , pattern )
270
+ }
256
271
}
272
+
257
273
err := c .subscribe (ctx , "punsubscribe" , patterns ... )
258
274
return err
259
275
}
@@ -264,9 +280,17 @@ func (c *PubSub) SUnsubscribe(ctx context.Context, channels ...string) error {
264
280
c .mu .Lock ()
265
281
defer c .mu .Unlock ()
266
282
267
- for _ , channel := range channels {
268
- delete (c .schannels , channel )
283
+ if len (channels ) > 0 {
284
+ for _ , channel := range channels {
285
+ delete (c .schannels , channel )
286
+ }
287
+ } else {
288
+ // Unsubscribe from all channels.
289
+ for channel := range c .schannels {
290
+ delete (c .schannels , channel )
291
+ }
269
292
}
293
+
270
294
err := c .subscribe (ctx , "sunsubscribe" , channels ... )
271
295
return err
272
296
}
0 commit comments