redis/pubsub.go

131 lines
2.6 KiB
Go
Raw Normal View History

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 {
*Client
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) {
pubSubConn, _, err := client.ConnPool.Get()
2012-08-05 16:09:43 +04:00
if err != nil {
return nil, err
}
client.ConnPool.Remove(pubSubConn)
2012-08-05 16:09:43 +04:00
2012-07-25 17:00:50 +04:00
c := &PubSubClient{
Client: &Client{
2012-08-06 18:04:30 +04:00
ConnPool: NewSingleConnPool(pubSubConn),
},
ch: make(chan *Message),
2012-07-25 17:00:50 +04:00
}
2012-08-05 16:09:43 +04:00
return c, nil
2012-07-25 17:00:50 +04:00
}
type Message struct {
Name, Channel, ChannelPattern, Message string
Number int64
2012-07-25 17:00:50 +04:00
Err error
}
func (c *PubSubClient) consumeMessages() {
conn, err := c.conn()
// SignleConnPool never returns error.
if err != nil {
panic(err)
}
2012-07-25 17:00:50 +04:00
req := NewMultiBulkReq()
for {
for {
msg := &Message{}
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 {
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)
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
}
}
}
}
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...)
conn, err := c.conn()
if err != nil {
return nil, err
}
if err := c.WriteReq(req.Req(), conn); 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-07-25 17:00:50 +04:00
go c.consumeMessages()
2012-08-05 16:09:43 +04:00
})
2012-07-25 17:00:50 +04:00
return c.ch, nil
}
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...)
conn, err := c.conn()
if err != nil {
return err
}
return c.WriteReq(req.Req(), conn)
2012-07-25 17:00:50 +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...)
}