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-05 16:09:43 +04:00
|
|
|
func newPubSubClient(client *Client) (*PubSubClient, error) {
|
2012-08-11 18:42:10 +04:00
|
|
|
return &PubSubClient{
|
|
|
|
BaseClient: &BaseClient{
|
|
|
|
ConnPool: NewSingleConnPool(client.ConnPool, false),
|
|
|
|
InitConn: client.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) PubSubClient() (*PubSubClient, error) {
|
|
|
|
return newPubSubClient(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-25 17:00:50 +04:00
|
|
|
req := NewMultiBulkReq()
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
msg := &Message{}
|
|
|
|
|
2012-08-06 18:02:45 +04:00
|
|
|
replyI, err := req.ParseReply(conn.Rd)
|
2012-07-25 17:00:50 +04:00
|
|
|
if err != nil {
|
|
|
|
msg.Err = err
|
|
|
|
c.ch <- msg
|
|
|
|
break
|
|
|
|
}
|
2012-07-26 22:43:21 +04:00
|
|
|
reply := replyI.([]interface{})
|
2012-07-25 17:00:50 +04:00
|
|
|
|
|
|
|
msgName := reply[0].(string)
|
|
|
|
switch msgName {
|
2012-08-09 16:27:06 +04:00
|
|
|
case "subscribe", "unsubscribe", "psubscribe", "punsubscribe":
|
2012-07-25 17:00:50 +04:00
|
|
|
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)
|
2012-08-09 16:27:06 +04:00
|
|
|
case "pmessage":
|
|
|
|
msg.Name = msgName
|
|
|
|
msg.ChannelPattern = reply[1].(string)
|
|
|
|
msg.Channel = reply[2].(string)
|
|
|
|
msg.Message = reply[3].(string)
|
2012-07-25 17:00:50 +04:00
|
|
|
default:
|
|
|
|
msg.Err = fmt.Errorf("Unsupported message name: %q.", msgName)
|
|
|
|
}
|
|
|
|
c.ch <- msg
|
|
|
|
|
2012-08-09 14:12:41 +04:00
|
|
|
if conn.Rd.Buffered() <= 0 {
|
2012-07-25 17:00:50 +04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-25 17:00:50 +04:00
|
|
|
req := NewMultiBulkReq(args...)
|
|
|
|
|
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-07-25 17:00:50 +04:00
|
|
|
req := NewMultiBulkReq(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...)
|
|
|
|
}
|