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
|
|
|
)
|
|
|
|
|
|
|
|
type PubSubClient struct {
|
|
|
|
*baseClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) PubSubClient() (*PubSubClient, error) {
|
|
|
|
return &PubSubClient{
|
|
|
|
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),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Publish(channel, message string) *IntReq {
|
|
|
|
req := NewIntReq("PUBLISH", channel, message)
|
|
|
|
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-28 14:24:22 +04:00
|
|
|
func (c *PubSubClient) Receive(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-28 14:24:22 +04:00
|
|
|
replyIface, err := NewIfaceSliceReq().ParseReply(cn.Rd)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch msgName := reply[0].(string); 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
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("redis: unsupported message name: %q", msgName)
|
|
|
|
}
|
|
|
|
}
|
2013-07-02 15:17:31 +04:00
|
|
|
|
2013-09-28 14:24:22 +04:00
|
|
|
func (c *PubSubClient) subscribe(cmd string, channels ...string) error {
|
|
|
|
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...)
|
|
|
|
req := NewIfaceSliceReq(args...)
|
|
|
|
return c.writeReq(cn, req)
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
|
|
|
|
2013-09-28 14:24:22 +04:00
|
|
|
func (c *PubSubClient) Subscribe(channels ...string) error {
|
2013-07-02 15:17:31 +04:00
|
|
|
return c.subscribe("SUBSCRIBE", channels...)
|
|
|
|
}
|
|
|
|
|
2013-09-28 14:24:22 +04:00
|
|
|
func (c *PubSubClient) PSubscribe(patterns ...string) error {
|
2013-07-02 15:17:31 +04:00
|
|
|
return c.subscribe("PSUBSCRIBE", patterns...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PubSubClient) unsubscribe(cmd string, channels ...string) error {
|
|
|
|
cn, err := c.conn()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-09-28 14:24:22 +04:00
|
|
|
args := append([]string{cmd}, channels...)
|
|
|
|
req := NewIfaceSliceReq(args...)
|
2013-07-02 15:17:31 +04:00
|
|
|
return c.writeReq(cn, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PubSubClient) Unsubscribe(channels ...string) error {
|
|
|
|
return c.unsubscribe("UNSUBSCRIBE", channels...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PubSubClient) PUnsubscribe(patterns ...string) error {
|
|
|
|
return c.unsubscribe("PUNSUBSCRIBE", patterns...)
|
|
|
|
}
|