2012-07-25 17:00:50 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2012-08-05 16:09:43 +04:00
|
|
|
"sync"
|
2012-07-25 17:00:50 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type PubSubClient struct {
|
2012-08-11 18:42:10 +04:00
|
|
|
*BaseClient
|
2012-08-05 16:09:43 +04:00
|
|
|
ch chan *Message
|
|
|
|
once sync.Once
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2012-08-17 22:36:48 +04:00
|
|
|
func (c *Client) PubSubClient() (*PubSubClient, error) {
|
2012-08-11 18:42:10 +04:00
|
|
|
return &PubSubClient{
|
|
|
|
BaseClient: &BaseClient{
|
2012-08-17 22:36:48 +04:00
|
|
|
ConnPool: NewSingleConnPool(c.ConnPool, false),
|
|
|
|
InitConn: c.InitConn,
|
2012-08-06 18:02:45 +04:00
|
|
|
},
|
|
|
|
ch: make(chan *Message),
|
2012-08-11 18:42:10 +04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Publish(channel, message string) *IntReq {
|
|
|
|
req := NewIntReq("PUBLISH", channel, message)
|
|
|
|
c.Process(req)
|
|
|
|
return req
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Message struct {
|
2012-08-09 16:27:06 +04:00
|
|
|
Name, Channel, ChannelPattern, Message string
|
|
|
|
Number int64
|
2012-07-25 17:00:50 +04:00
|
|
|
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
func (c *PubSubClient) consumeMessages(conn *Conn) {
|
2012-08-17 22:36:48 +04:00
|
|
|
req := NewIfaceSliceReq()
|
2012-07-25 17:00:50 +04:00
|
|
|
|
|
|
|
for {
|
2012-08-25 22:39:35 +04:00
|
|
|
msg := &Message{}
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2012-08-25 22:39:35 +04:00
|
|
|
replyIface, err := req.ParseReply(conn.Rd)
|
|
|
|
if err != nil {
|
|
|
|
msg.Err = err
|
|
|
|
c.ch <- msg
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reply, ok := replyIface.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
msg.Err = fmt.Errorf("redis: unexpected reply type %T", replyIface)
|
2012-07-25 17:00:50 +04:00
|
|
|
c.ch <- msg
|
2012-08-25 22:39:35 +04:00
|
|
|
return
|
|
|
|
}
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2012-08-25 22:39:35 +04:00
|
|
|
msgName := reply[0].(string)
|
|
|
|
switch msgName {
|
|
|
|
case "subscribe", "unsubscribe", "psubscribe", "punsubscribe":
|
|
|
|
msg.Name = msgName
|
|
|
|
msg.Channel = reply[1].(string)
|
|
|
|
msg.Number = reply[2].(int64)
|
|
|
|
case "message":
|
|
|
|
msg.Name = msgName
|
|
|
|
msg.Channel = reply[1].(string)
|
|
|
|
msg.Message = reply[2].(string)
|
|
|
|
case "pmessage":
|
|
|
|
msg.Name = msgName
|
|
|
|
msg.ChannelPattern = reply[1].(string)
|
|
|
|
msg.Channel = reply[2].(string)
|
|
|
|
msg.Message = reply[3].(string)
|
|
|
|
default:
|
|
|
|
msg.Err = fmt.Errorf("Unsupported message name: %q.", msgName)
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
2012-08-25 22:39:35 +04:00
|
|
|
c.ch <- msg
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-09 16:27:06 +04:00
|
|
|
func (c *PubSubClient) subscribe(cmd string, channels ...string) (chan *Message, error) {
|
|
|
|
args := append([]string{cmd}, channels...)
|
2012-08-17 22:36:48 +04:00
|
|
|
req := NewIfaceSliceReq(args...)
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2012-08-06 18:02:45 +04:00
|
|
|
conn, err := c.conn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2012-08-14 19:20:22 +04:00
|
|
|
if err := c.WriteReq(conn, req); err != nil {
|
2012-07-25 17:00:50 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2012-08-05 16:09:43 +04:00
|
|
|
c.once.Do(func() {
|
2012-08-11 18:42:10 +04:00
|
|
|
go c.consumeMessages(conn)
|
2012-08-05 16:09:43 +04:00
|
|
|
})
|
2012-07-25 17:00:50 +04:00
|
|
|
|
|
|
|
return c.ch, nil
|
|
|
|
}
|
|
|
|
|
2012-08-09 16:27:06 +04:00
|
|
|
func (c *PubSubClient) Subscribe(channels ...string) (chan *Message, error) {
|
|
|
|
return c.subscribe("SUBSCRIBE", channels...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PubSubClient) PSubscribe(patterns ...string) (chan *Message, error) {
|
|
|
|
return c.subscribe("PSUBSCRIBE", patterns...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PubSubClient) unsubscribe(cmd string, channels ...string) error {
|
|
|
|
args := append([]string{cmd}, channels...)
|
2012-08-17 22:36:48 +04:00
|
|
|
req := NewIfaceSliceReq(args...)
|
2012-08-06 18:02:45 +04:00
|
|
|
|
|
|
|
conn, err := c.conn()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2012-08-14 19:20:22 +04:00
|
|
|
return c.WriteReq(conn, req)
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
2012-08-09 16:27:06 +04:00
|
|
|
|
|
|
|
func (c *PubSubClient) Unsubscribe(channels ...string) error {
|
|
|
|
return c.unsubscribe("UNSUBSCRIBE", channels...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PubSubClient) PUnsubscribe(patterns ...string) error {
|
|
|
|
return c.unsubscribe("PUNSUBSCRIBE", patterns...)
|
|
|
|
}
|