redis/v2/pubsub.go

125 lines
2.5 KiB
Go
Raw Normal View History

2013-07-02 15:17:31 +04:00
package redis
import (
"fmt"
"time"
2013-07-02 15:17:31 +04:00
)
2013-11-07 18:20:15 +04:00
// Not thread-safe.
2013-09-29 10:37:09 +04:00
type PubSub struct {
2013-07-02 15:17:31 +04:00
*baseClient
}
2013-09-29 10:37:09 +04:00
func (c *Client) PubSub() *PubSub {
return &PubSub{
2013-07-02 15:17:31 +04:00
baseClient: &baseClient{
2013-09-11 20:22:10 +04:00
opt: c.opt,
2013-07-02 15:17:31 +04:00
connPool: newSingleConnPool(c.connPool, nil, false),
},
2013-09-29 10:37:09 +04:00
}
2013-07-02 15:17:31 +04:00
}
2013-09-29 12:06:49 +04:00
func (c *Client) Publish(channel, message string) *IntCmd {
req := NewIntCmd("PUBLISH", channel, message)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
type Message struct {
Channel string
Payload string
2013-07-02 15:17:31 +04:00
}
type PMessage struct {
Channel string
Pattern string
Payload string
2013-07-02 15:17:31 +04:00
}
type Subscription struct {
Kind string
Channel string
Count int
}
2013-07-02 15:17:31 +04:00
2013-09-29 10:37:09 +04:00
func (c *PubSub) Receive() (interface{}, error) {
return c.ReceiveTimeout(0)
}
func (c *PubSub) ReceiveTimeout(timeout time.Duration) (interface{}, error) {
2013-07-02 15:17:31 +04:00
cn, err := c.conn()
if err != nil {
return nil, err
}
cn.readTimeout = timeout
2013-07-02 15:17:31 +04:00
2013-09-29 12:11:18 +04:00
replyIface, err := NewSliceCmd().parseReply(cn.rd)
if err != nil {
2013-07-02 15:17:31 +04:00
return nil, err
}
reply, ok := replyIface.([]interface{})
if !ok {
return nil, fmt.Errorf("redis: unexpected reply type %T", replyIface)
}
2013-12-30 16:29:28 +04:00
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
}
2013-12-30 16:29:28 +04:00
return nil, fmt.Errorf("redis: unsupported message name: %q", msgName)
}
2013-07-02 15:17:31 +04:00
2013-09-29 10:37:09 +04:00
func (c *PubSub) subscribe(cmd string, channels ...string) error {
cn, err := c.conn()
if err != nil {
return err
}
2013-07-02 15:17:31 +04:00
args := append([]string{cmd}, channels...)
2013-09-29 12:06:49 +04:00
req := NewSliceCmd(args...)
return c.writeCmd(cn, req)
2013-07-02 15:17:31 +04:00
}
2013-09-29 10:37:09 +04:00
func (c *PubSub) Subscribe(channels ...string) error {
2013-07-02 15:17:31 +04:00
return c.subscribe("SUBSCRIBE", channels...)
}
2013-09-29 10:37:09 +04:00
func (c *PubSub) PSubscribe(patterns ...string) error {
2013-07-02 15:17:31 +04:00
return c.subscribe("PSUBSCRIBE", patterns...)
}
2013-09-29 10:37:09 +04:00
func (c *PubSub) unsubscribe(cmd string, channels ...string) error {
2013-07-02 15:17:31 +04:00
cn, err := c.conn()
if err != nil {
return err
}
args := append([]string{cmd}, channels...)
2013-09-29 12:06:49 +04:00
req := NewSliceCmd(args...)
return c.writeCmd(cn, req)
2013-07-02 15:17:31 +04:00
}
2013-09-29 10:37:09 +04:00
func (c *PubSub) Unsubscribe(channels ...string) error {
2013-07-02 15:17:31 +04:00
return c.unsubscribe("UNSUBSCRIBE", channels...)
}
2013-09-29 10:37:09 +04:00
func (c *PubSub) PUnsubscribe(patterns ...string) error {
2013-07-02 15:17:31 +04:00
return c.unsubscribe("PUNSUBSCRIBE", patterns...)
}