2013-07-02 15:17:31 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-09-28 14:24:22 +04:00
|
|
|
"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 {
|
2013-09-28 14:24:22 +04:00
|
|
|
Channel string
|
|
|
|
Payload string
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
|
|
|
|
2013-09-28 14:24:22 +04:00
|
|
|
type PMessage struct {
|
|
|
|
Channel string
|
|
|
|
Pattern string
|
|
|
|
Payload string
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
|
|
|
|
2013-09-28 14:24:22 +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
|
|
|
|
}
|
2013-09-28 14:24:22 +04:00
|
|
|
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)
|
2013-09-28 14:24:22 +04:00
|
|
|
if err != nil {
|
2013-07-02 15:17:31 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-09-28 14:24:22 +04:00
|
|
|
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 {
|
2013-09-28 14:24:22 +04:00
|
|
|
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-09-28 14:24:22 +04:00
|
|
|
}
|
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 {
|
2013-09-28 14:24:22 +04:00
|
|
|
cn, err := c.conn()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-02 15:17:31 +04:00
|
|
|
|
2013-09-28 14:24:22 +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
|
|
|
|
}
|
|
|
|
|
2013-09-28 14:24:22 +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) 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...)
|
|
|
|
}
|