From c939d2283e13a7edd37c6ea560120a901e533fcc Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Thu, 22 Dec 2016 13:26:00 +0200 Subject: [PATCH] Allow creating PubSub without channels. --- redis.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/redis.go b/redis.go index 6a3b04c6..3e9ee07e 100644 --- a/redis.go +++ b/redis.go @@ -359,11 +359,23 @@ func (c *Client) pubSub() *PubSub { // Subscribe subscribes the client to the specified channels. func (c *Client) Subscribe(channels ...string) (*PubSub, error) { pubsub := c.pubSub() - return pubsub, pubsub.Subscribe(channels...) + if len(channels) > 0 { + if err := pubsub.Subscribe(channels...); err != nil { + pubsub.Close() + return nil, err + } + } + return pubsub, nil } // PSubscribe subscribes the client to the given patterns. func (c *Client) PSubscribe(channels ...string) (*PubSub, error) { pubsub := c.pubSub() - return pubsub, pubsub.PSubscribe(channels...) + if len(channels) > 0 { + if err := pubsub.PSubscribe(channels...); err != nil { + pubsub.Close() + return nil, err + } + } + return pubsub, nil }