2012-07-25 17:00:50 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-05-11 11:42:40 +04:00
|
|
|
"time"
|
2012-07-25 17:00:50 +04:00
|
|
|
)
|
|
|
|
|
2015-05-23 14:15:05 +03:00
|
|
|
// PubSub implements Pub/Sub commands as described in
|
|
|
|
// http://redis.io/topics/pubsub.
|
2014-05-11 11:42:40 +04:00
|
|
|
type PubSub struct {
|
|
|
|
*baseClient
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *Client) PubSub() *PubSub {
|
|
|
|
return &PubSub{
|
|
|
|
baseClient: &baseClient{
|
|
|
|
opt: c.opt,
|
2014-06-28 15:47:37 +04:00
|
|
|
connPool: newSingleConnPool(c.connPool, false),
|
2012-08-06 18:02:45 +04:00
|
|
|
},
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *Client) Publish(channel, message string) *IntCmd {
|
|
|
|
req := NewIntCmd("PUBLISH", channel, message)
|
2012-08-11 18:42:10 +04:00
|
|
|
c.Process(req)
|
|
|
|
return req
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2015-07-11 13:12:47 +03:00
|
|
|
func (c *PubSub) Ping(payload string) error {
|
|
|
|
cn, err := c.conn()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []interface{}{"PING"}
|
|
|
|
if payload != "" {
|
|
|
|
args = append(args, payload)
|
|
|
|
}
|
|
|
|
cmd := NewCmd(args...)
|
|
|
|
return cn.writeCmds(cmd)
|
|
|
|
}
|
|
|
|
|
2015-05-23 18:17:45 +03:00
|
|
|
// Message received as result of a PUBLISH command issued by another client.
|
2012-07-25 17:00:50 +04:00
|
|
|
type Message struct {
|
2014-05-11 11:42:40 +04:00
|
|
|
Channel string
|
|
|
|
Payload string
|
|
|
|
}
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
func (m *Message) String() string {
|
|
|
|
return fmt.Sprintf("Message<%s: %s>", m.Channel, m.Payload)
|
|
|
|
}
|
|
|
|
|
2015-05-23 18:17:45 +03:00
|
|
|
// Message matching a pattern-matching subscription received as result
|
|
|
|
// of a PUBLISH command issued by another client.
|
2014-05-11 11:42:40 +04:00
|
|
|
type PMessage struct {
|
|
|
|
Channel string
|
|
|
|
Pattern string
|
|
|
|
Payload string
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
func (m *PMessage) String() string {
|
|
|
|
return fmt.Sprintf("PMessage<%s: %s>", m.Channel, m.Payload)
|
|
|
|
}
|
|
|
|
|
2015-07-11 13:12:47 +03:00
|
|
|
// Pong received as result of a PING command issued by another client.
|
|
|
|
type Pong struct {
|
|
|
|
Payload string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pong) String() string {
|
|
|
|
if p.Payload != "" {
|
|
|
|
return fmt.Sprintf("Pong<%s>", p.Payload)
|
|
|
|
}
|
|
|
|
return "Pong"
|
|
|
|
}
|
|
|
|
|
2015-05-23 18:17:45 +03:00
|
|
|
// Message received after a successful subscription to channel.
|
2014-05-11 11:42:40 +04:00
|
|
|
type Subscription struct {
|
2015-05-23 18:17:45 +03:00
|
|
|
// Can be "subscribe", "unsubscribe", "psubscribe" or "punsubscribe".
|
|
|
|
Kind string
|
|
|
|
// Channel name we have subscribed to.
|
2014-05-11 11:42:40 +04:00
|
|
|
Channel string
|
2015-05-23 18:17:45 +03:00
|
|
|
// Number of channels we are currently subscribed to.
|
|
|
|
Count int
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
func (m *Subscription) String() string {
|
|
|
|
return fmt.Sprintf("%s: %s", m.Kind, m.Channel)
|
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *PubSub) Receive() (interface{}, error) {
|
|
|
|
return c.ReceiveTimeout(0)
|
|
|
|
}
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2015-07-11 13:12:47 +03:00
|
|
|
func newMessage(reply []interface{}) (interface{}, error) {
|
|
|
|
switch kind := reply[0].(string); kind {
|
2014-05-11 11:42:40 +04:00
|
|
|
case "subscribe", "unsubscribe", "psubscribe", "punsubscribe":
|
|
|
|
return &Subscription{
|
2015-05-28 15:51:19 +03:00
|
|
|
Kind: kind,
|
2014-05-11 11:42:40 +04:00
|
|
|
Channel: reply[1].(string),
|
|
|
|
Count: int(reply[2].(int64)),
|
|
|
|
}, nil
|
|
|
|
case "message":
|
|
|
|
return &Message{
|
|
|
|
Channel: reply[1].(string),
|
|
|
|
Payload: reply[2].(string),
|
|
|
|
}, nil
|
|
|
|
case "pmessage":
|
|
|
|
return &PMessage{
|
|
|
|
Pattern: reply[1].(string),
|
|
|
|
Channel: reply[2].(string),
|
|
|
|
Payload: reply[3].(string),
|
|
|
|
}, nil
|
2015-07-11 13:12:47 +03:00
|
|
|
case "pong":
|
|
|
|
return &Pong{
|
|
|
|
Payload: reply[1].(string),
|
|
|
|
}, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("redis: unsupported pubsub notification: %q", kind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PubSub) ReceiveTimeout(timeout time.Duration) (interface{}, error) {
|
|
|
|
cn, err := c.conn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
2015-07-11 13:12:47 +03:00
|
|
|
cn.ReadTimeout = timeout
|
2015-05-10 15:33:04 +03:00
|
|
|
|
2015-07-11 13:12:47 +03:00
|
|
|
cmd := NewSliceCmd()
|
|
|
|
if err := cmd.parseReply(cn.rd); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newMessage(cmd.Val())
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *PubSub) subscribe(cmd string, channels ...string) error {
|
|
|
|
cn, err := c.conn()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-28 15:51:19 +03:00
|
|
|
args := make([]interface{}, 1+len(channels))
|
|
|
|
args[0] = cmd
|
|
|
|
for i, channel := range channels {
|
|
|
|
args[1+i] = channel
|
|
|
|
}
|
2014-05-11 11:42:40 +04:00
|
|
|
req := NewSliceCmd(args...)
|
2015-01-24 15:12:48 +03:00
|
|
|
return cn.writeCmds(req)
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *PubSub) Subscribe(channels ...string) error {
|
2012-08-09 16:27:06 +04:00
|
|
|
return c.subscribe("SUBSCRIBE", channels...)
|
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *PubSub) PSubscribe(patterns ...string) error {
|
2012-08-09 16:27:06 +04:00
|
|
|
return c.subscribe("PSUBSCRIBE", patterns...)
|
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *PubSub) Unsubscribe(channels ...string) error {
|
2015-05-28 15:51:19 +03:00
|
|
|
return c.subscribe("UNSUBSCRIBE", channels...)
|
2012-08-09 16:27:06 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *PubSub) PUnsubscribe(patterns ...string) error {
|
2015-05-28 15:51:19 +03:00
|
|
|
return c.subscribe("PUNSUBSCRIBE", patterns...)
|
2012-08-09 16:27:06 +04:00
|
|
|
}
|