Skip to content

Commit

Permalink
pubsub: improve API and docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Jul 11, 2015
1 parent 2cb6b30 commit d7edae8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 41 deletions.
6 changes: 2 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,11 @@ func ExampleMulti() {
}

func ExamplePubSub() {
pubsub := client.PubSub()
defer pubsub.Close()

err := pubsub.Subscribe("mychannel")
pubsub, err := client.Subscribe("mychannel")
if err != nil {
panic(err)
}
defer pubsub.Close()

err = client.Publish("mychannel", "hello").Err()
if err != nil {
Expand Down
60 changes: 42 additions & 18 deletions pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ import (
"time"
)

// 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.
type PubSub struct {
*baseClient
}

// Deprecated. Use Subscribe/PSubscribe instead.
func (c *Client) PubSub() *PubSub {
return &PubSub{
baseClient: &baseClient{
Expand All @@ -20,10 +28,16 @@ func (c *Client) PubSub() *PubSub {
}
}

func (c *Client) Publish(channel, message string) *IntCmd {
req := NewIntCmd("PUBLISH", channel, message)
c.Process(req)
return req
// 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) Ping(payload string) error {
Expand All @@ -40,6 +54,20 @@ func (c *PubSub) Ping(payload string) error {
return cn.writeCmds(cmd)
}

// Message received after a successful subscription to channel.
type Subscription struct {
// Can be "subscribe", "unsubscribe", "psubscribe" or "punsubscribe".
Kind string
// Channel name we have subscribed to.
Channel string
// Number of channels we are currently subscribed to.
Count int
}

func (m *Subscription) String() string {
return fmt.Sprintf("%s: %s", m.Kind, m.Channel)
}

// Message received as result of a PUBLISH command issued by another client.
type Message struct {
Channel string
Expand Down Expand Up @@ -74,20 +102,8 @@ func (p *Pong) String() string {
return "Pong"
}

// Message received after a successful subscription to channel.
type Subscription struct {
// Can be "subscribe", "unsubscribe", "psubscribe" or "punsubscribe".
Kind string
// Channel name we have subscribed to.
Channel string
// Number of channels we are currently subscribed to.
Count int
}

func (m *Subscription) String() string {
return fmt.Sprintf("%s: %s", m.Kind, m.Channel)
}

// Returns a message as a Subscription, Message, PMessage, Pong or
// error. See PubSub example for details.
func (c *PubSub) Receive() (interface{}, error) {
return c.ReceiveTimeout(0)
}
Expand Down Expand Up @@ -120,6 +136,8 @@ func newMessage(reply []interface{}) (interface{}, error) {
}
}

// ReceiveTimeout acts like Receive but returns an error if message
// is not received in time.
func (c *PubSub) ReceiveTimeout(timeout time.Duration) (interface{}, error) {
cn, err := c.conn()
if err != nil {
Expand Down Expand Up @@ -149,18 +167,24 @@ func (c *PubSub) subscribe(cmd string, channels ...string) error {
return cn.writeCmds(req)
}

// Subscribes the client to the specified channels.
func (c *PubSub) Subscribe(channels ...string) error {
return c.subscribe("SUBSCRIBE", channels...)
}

// Subscribes the client to the given patterns.
func (c *PubSub) PSubscribe(patterns ...string) error {
return c.subscribe("PSUBSCRIBE", patterns...)
}

// Unsubscribes the client from the given channels, or from all of
// them if none is given.
func (c *PubSub) Unsubscribe(channels ...string) error {
return c.subscribe("UNSUBSCRIBE", channels...)
}

// Unsubscribes the client from the given patterns, or from all of
// them if none is given.
func (c *PubSub) PUnsubscribe(patterns ...string) error {
return c.subscribe("PUNSUBSCRIBE", patterns...)
}
47 changes: 28 additions & 19 deletions pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,26 @@ import (

var _ = Describe("PubSub", func() {
var client *redis.Client
var pubsub *redis.PubSub

BeforeEach(func() {
client = redis.NewClient(&redis.Options{
Addr: redisAddr,
})
Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
pubsub = client.PubSub()
})

AfterEach(func() {
Expect(pubsub.Close()).NotTo(HaveOccurred())
Expect(client.Close()).NotTo(HaveOccurred())
})

It("should support pattern matching", func() {
Expect(pubsub.PSubscribe("mychannel*")).NotTo(HaveOccurred())
pubsub, err := client.PSubscribe("mychannel*")
Expect(err).NotTo(HaveOccurred())
defer pubsub.Close()

pub := client.Publish("mychannel1", "hello")
Expect(pub.Err()).NotTo(HaveOccurred())
Expect(pub.Val()).To(Equal(int64(1)))
n, err := client.Publish("mychannel1", "hello").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))

Expect(pubsub.PUnsubscribe("mychannel*")).NotTo(HaveOccurred())

Expand Down Expand Up @@ -75,7 +74,9 @@ var _ = Describe("PubSub", func() {
Expect(err).NotTo(HaveOccurred())
Expect(channels).To(BeEmpty())

Expect(pubsub.Subscribe("mychannel", "mychannel2")).NotTo(HaveOccurred())
pubsub, err := client.Subscribe("mychannel", "mychannel2")
Expect(err).NotTo(HaveOccurred())
defer pubsub.Close()

channels, err = client.PubSubChannels("mychannel*").Result()
Expect(err).NotTo(HaveOccurred())
Expand All @@ -91,7 +92,9 @@ var _ = Describe("PubSub", func() {
})

It("should return the numbers of subscribers", func() {
Expect(pubsub.Subscribe("mychannel", "mychannel2")).NotTo(HaveOccurred())
pubsub, err := client.Subscribe("mychannel", "mychannel2")
Expect(err).NotTo(HaveOccurred())
defer pubsub.Close()

channels, err := client.PubSubNumSub("mychannel", "mychannel2", "mychannel3").Result()
Expect(err).NotTo(HaveOccurred())
Expand All @@ -107,23 +110,27 @@ var _ = Describe("PubSub", func() {
Expect(err).NotTo(HaveOccurred())
Expect(num).To(Equal(int64(0)))

Expect(pubsub.PSubscribe("*")).NotTo(HaveOccurred())
pubsub, err := client.PSubscribe("*")
Expect(err).NotTo(HaveOccurred())
defer pubsub.Close()

num, err = client.PubSubNumPat().Result()
Expect(err).NotTo(HaveOccurred())
Expect(num).To(Equal(int64(1)))
})

It("should pub/sub", func() {
Expect(pubsub.Subscribe("mychannel", "mychannel2")).NotTo(HaveOccurred())
pubsub, err := client.Subscribe("mychannel", "mychannel2")
Expect(err).NotTo(HaveOccurred())
defer pubsub.Close()

pub := client.Publish("mychannel", "hello")
Expect(pub.Err()).NotTo(HaveOccurred())
Expect(pub.Val()).To(Equal(int64(1)))
n, err := client.Publish("mychannel", "hello").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))

pub = client.Publish("mychannel2", "hello2")
Expect(pub.Err()).NotTo(HaveOccurred())
Expect(pub.Val()).To(Equal(int64(1)))
n, err = client.Publish("mychannel2", "hello2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))

Expect(pubsub.Unsubscribe("mychannel", "mychannel2")).NotTo(HaveOccurred())

Expand Down Expand Up @@ -187,8 +194,9 @@ var _ = Describe("PubSub", func() {
})

It("should ping/pong", func() {
err := pubsub.Subscribe("mychannel")
pubsub, err := client.Subscribe("mychannel")
Expect(err).NotTo(HaveOccurred())
defer pubsub.Close()

_, err = pubsub.ReceiveTimeout(time.Second)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -203,8 +211,9 @@ var _ = Describe("PubSub", func() {
})

It("should ping/pong with payload", func() {
err := pubsub.Subscribe("mychannel")
pubsub, err := client.Subscribe("mychannel")
Expect(err).NotTo(HaveOccurred())
defer pubsub.Close()

_, err = pubsub.ReceiveTimeout(time.Second)
Expect(err).NotTo(HaveOccurred())
Expand Down

0 comments on commit d7edae8

Please sign in to comment.