diff --git a/commands.go b/commands.go index db9d8061..81a691e8 100644 --- a/commands.go +++ b/commands.go @@ -1618,6 +1618,13 @@ func (c *cmdable) DebugObject(key string) *StringCmd { //------------------------------------------------------------------------------ +// Publish posts the message to the channel. +func (c *cmdable) Publish(channel, message string) *IntCmd { + cmd := NewIntCmd("PUBLISH", channel, message) + c.process(cmd) + return cmd +} + func (c *cmdable) PubSubChannels(pattern string) *StringSliceCmd { args := []interface{}{"pubsub", "channels"} if pattern != "*" { diff --git a/pool_test.go b/pool_test.go index 31bf9687..c1d2f681 100644 --- a/pool_test.go +++ b/pool_test.go @@ -81,8 +81,8 @@ var _ = Describe("pool", func() { connPool.(*pool.ConnPool).DialLimiter = nil perform(1000, func(id int) { - pubsub := client.PubSub() - Expect(pubsub.Subscribe()).NotTo(HaveOccurred()) + pubsub, err := client.Subscribe() + Expect(err).NotTo(HaveOccurred()) Expect(pubsub.Close()).NotTo(HaveOccurred()) }) diff --git a/pubsub.go b/pubsub.go index 6c72b8f7..1d1fc939 100644 --- a/pubsub.go +++ b/pubsub.go @@ -10,13 +10,6 @@ import ( "gopkg.in/redis.v4/internal/pool" ) -// Posts a message to the given channel. -func (c *Client) Publish(channel, message string) *IntCmd { - req := NewIntCmd("PUBLISH", channel, message) - c.Process(req) - return req -} - // PubSub implements Pub/Sub commands as described in // http://redis.io/topics/pubsub. It's NOT safe for concurrent use by // multiple goroutines. @@ -29,28 +22,6 @@ type PubSub struct { nsub int // number of active subscriptions } -// Deprecated. Use Subscribe/PSubscribe instead. -func (c *Client) PubSub() *PubSub { - return &PubSub{ - base: baseClient{ - opt: c.opt, - connPool: pool.NewStickyConnPool(c.connPool.(*pool.ConnPool), false), - }, - } -} - -// Subscribes the client to the specified channels. -func (c *Client) Subscribe(channels ...string) (*PubSub, error) { - pubsub := c.PubSub() - return pubsub, pubsub.Subscribe(channels...) -} - -// Subscribes the client to the given patterns. -func (c *Client) PSubscribe(channels ...string) (*PubSub, error) { - pubsub := c.PubSub() - return pubsub, pubsub.PSubscribe(channels...) -} - func (c *PubSub) subscribe(redisCmd string, channels ...string) error { cn, err := c.base.conn() if err != nil { diff --git a/redis.go b/redis.go index b67d4227..b8ceab55 100644 --- a/redis.go +++ b/redis.go @@ -215,3 +215,24 @@ func (c *Client) pipelineExec(cmds []Cmder) error { } return retErr } + +func (c *Client) pubSub() *PubSub { + return &PubSub{ + base: baseClient{ + opt: c.opt, + connPool: pool.NewStickyConnPool(c.connPool.(*pool.ConnPool), false), + }, + } +} + +// Subscribe subscribes the client to the specified channels. +func (c *Client) Subscribe(channels ...string) (*PubSub, error) { + pubsub := c.pubSub() + return pubsub, pubsub.Subscribe(channels...) +} + +// PSubscribe subscribes the client to the given patterns. +func (c *Client) PSubscribe(channels ...string) (*PubSub, error) { + pubsub := c.pubSub() + return pubsub, pubsub.PSubscribe(channels...) +} diff --git a/redis_test.go b/redis_test.go index bbb00e0a..4b62f3ed 100644 --- a/redis_test.go +++ b/redis_test.go @@ -57,10 +57,10 @@ var _ = Describe("Client", func() { }) It("should close pubsub without closing the client", func() { - pubsub := client.PubSub() + pubsub, err := client.Subscribe() Expect(pubsub.Close()).NotTo(HaveOccurred()) - _, err := pubsub.Receive() + _, err = pubsub.Receive() Expect(err).To(MatchError("redis: client is closed")) Expect(client.Ping().Err()).NotTo(HaveOccurred()) }) @@ -90,8 +90,12 @@ var _ = Describe("Client", func() { }) It("should close pubsub when client is closed", func() { - pubsub := client.PubSub() + pubsub, err := client.Subscribe() Expect(client.Close()).NotTo(HaveOccurred()) + + _, err = pubsub.Receive() + Expect(err).To(HaveOccurred()) + Expect(pubsub.Close()).NotTo(HaveOccurred()) })