redis/pubsub.go

136 lines
2.6 KiB
Go
Raw Normal View History

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
)
2014-05-11 11:42:40 +04:00
// Not thread-safe.
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),
},
2014-05-11 11:42:40 +04:00
}
}
2014-05-11 11:42:40 +04:00
func (c *Client) Publish(channel, message string) *IntCmd {
req := NewIntCmd("PUBLISH", channel, message)
c.Process(req)
return req
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)
}
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)
}
2014-05-11 11:42:40 +04:00
type Subscription struct {
Kind string
Channel string
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
2014-05-11 11:42:40 +04:00
func (c *PubSub) ReceiveTimeout(timeout time.Duration) (interface{}, error) {
cn, err := c.conn()
if err != nil {
return nil, err
}
cn.ReadTimeout = timeout
2014-05-11 11:42:40 +04:00
cmd := NewSliceCmd()
if err := cmd.parseReply(cn.rd); err != nil {
2012-07-25 17:00:50 +04:00
return nil, err
}
2014-05-11 11:42:40 +04:00
reply := cmd.Val()
msgName := reply[0].(string)
switch msgName {
case "subscribe", "unsubscribe", "psubscribe", "punsubscribe":
return &Subscription{
Kind: msgName,
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
}
2014-05-11 11:42:40 +04:00
return nil, fmt.Errorf("redis: unsupported message name: %q", msgName)
}
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
}
args := append([]string{cmd}, channels...)
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 {
return c.subscribe("SUBSCRIBE", channels...)
}
2014-05-11 11:42:40 +04:00
func (c *PubSub) PSubscribe(patterns ...string) error {
return c.subscribe("PSUBSCRIBE", patterns...)
}
2014-05-11 11:42:40 +04:00
func (c *PubSub) unsubscribe(cmd string, channels ...string) error {
cn, err := c.conn()
if err != nil {
return err
}
2014-05-11 11:42:40 +04:00
args := append([]string{cmd}, channels...)
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) Unsubscribe(channels ...string) error {
return c.unsubscribe("UNSUBSCRIBE", channels...)
}
2014-05-11 11:42:40 +04:00
func (c *PubSub) PUnsubscribe(patterns ...string) error {
return c.unsubscribe("PUNSUBSCRIBE", patterns...)
}