@@ -238,9 +238,17 @@ func (c *PubSub) Unsubscribe(ctx context.Context, channels ...string) error {
238238 c .mu .Lock ()
239239 defer c .mu .Unlock ()
240240
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+ }
243250 }
251+
244252 err := c .subscribe (ctx , "unsubscribe" , channels ... )
245253 return err
246254}
@@ -251,9 +259,17 @@ func (c *PubSub) PUnsubscribe(ctx context.Context, patterns ...string) error {
251259 c .mu .Lock ()
252260 defer c .mu .Unlock ()
253261
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+ }
256271 }
272+
257273 err := c .subscribe (ctx , "punsubscribe" , patterns ... )
258274 return err
259275}
@@ -264,9 +280,17 @@ func (c *PubSub) SUnsubscribe(ctx context.Context, channels ...string) error {
264280 c .mu .Lock ()
265281 defer c .mu .Unlock ()
266282
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+ }
269292 }
293+
270294 err := c .subscribe (ctx , "sunsubscribe" , channels ... )
271295 return err
272296}
0 commit comments