mirror of
https://github.com/redis/go-redis.git
synced 2025-07-31 05:04:23 +03:00
all: switch to gopkg.in.
This commit is contained in:
162
pubsub.go
162
pubsub.go
@ -2,123 +2,121 @@ package redis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PubSubClient struct {
|
||||
*BaseClient
|
||||
ch chan *Message
|
||||
once sync.Once
|
||||
// Not thread-safe.
|
||||
type PubSub struct {
|
||||
*baseClient
|
||||
}
|
||||
|
||||
func (c *Client) PubSubClient() (*PubSubClient, error) {
|
||||
return &PubSubClient{
|
||||
BaseClient: &BaseClient{
|
||||
ConnPool: NewSingleConnPool(c.ConnPool, false),
|
||||
InitConn: c.InitConn,
|
||||
func (c *Client) PubSub() *PubSub {
|
||||
return &PubSub{
|
||||
baseClient: &baseClient{
|
||||
opt: c.opt,
|
||||
connPool: newSingleConnPool(c.connPool, nil, false),
|
||||
},
|
||||
ch: make(chan *Message),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Publish(channel, message string) *IntReq {
|
||||
req := NewIntReq("PUBLISH", channel, message)
|
||||
func (c *Client) Publish(channel, message string) *IntCmd {
|
||||
req := NewIntCmd("PUBLISH", channel, message)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Name, Channel, ChannelPattern, Message string
|
||||
Number int64
|
||||
|
||||
Err error
|
||||
Channel string
|
||||
Payload string
|
||||
}
|
||||
|
||||
func (c *PubSubClient) consumeMessages(conn *Conn) {
|
||||
req := NewIfaceSliceReq()
|
||||
|
||||
for {
|
||||
msg := &Message{}
|
||||
|
||||
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)
|
||||
c.ch <- msg
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
c.ch <- msg
|
||||
}
|
||||
type PMessage struct {
|
||||
Channel string
|
||||
Pattern string
|
||||
Payload string
|
||||
}
|
||||
|
||||
func (c *PubSubClient) subscribe(cmd string, channels ...string) (chan *Message, error) {
|
||||
args := append([]string{cmd}, channels...)
|
||||
req := NewIfaceSliceReq(args...)
|
||||
type Subscription struct {
|
||||
Kind string
|
||||
Channel string
|
||||
Count int
|
||||
}
|
||||
|
||||
conn, err := c.conn()
|
||||
func (c *PubSub) Receive() (interface{}, error) {
|
||||
return c.ReceiveTimeout(0)
|
||||
}
|
||||
|
||||
func (c *PubSub) ReceiveTimeout(timeout time.Duration) (interface{}, error) {
|
||||
cn, err := c.conn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cn.readTimeout = timeout
|
||||
|
||||
if err := c.WriteReq(conn, req); err != nil {
|
||||
cmd := NewSliceCmd()
|
||||
if err := cmd.parseReply(cn.rd); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.once.Do(func() {
|
||||
go c.consumeMessages(conn)
|
||||
})
|
||||
reply := cmd.Val()
|
||||
|
||||
return c.ch, nil
|
||||
msgName := reply[0].(string)
|
||||
switch 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
|
||||
}
|
||||
return nil, fmt.Errorf("redis: unsupported message name: %q", msgName)
|
||||
}
|
||||
|
||||
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...)
|
||||
req := NewIfaceSliceReq(args...)
|
||||
|
||||
conn, err := c.conn()
|
||||
func (c *PubSub) subscribe(cmd string, channels ...string) error {
|
||||
cn, err := c.conn()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.WriteReq(conn, req)
|
||||
args := append([]string{cmd}, channels...)
|
||||
req := NewSliceCmd(args...)
|
||||
return c.writeCmd(cn, req)
|
||||
}
|
||||
|
||||
func (c *PubSubClient) Unsubscribe(channels ...string) error {
|
||||
func (c *PubSub) Subscribe(channels ...string) error {
|
||||
return c.subscribe("SUBSCRIBE", channels...)
|
||||
}
|
||||
|
||||
func (c *PubSub) PSubscribe(patterns ...string) error {
|
||||
return c.subscribe("PSUBSCRIBE", patterns...)
|
||||
}
|
||||
|
||||
func (c *PubSub) unsubscribe(cmd string, channels ...string) error {
|
||||
cn, err := c.conn()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
args := append([]string{cmd}, channels...)
|
||||
req := NewSliceCmd(args...)
|
||||
return c.writeCmd(cn, req)
|
||||
}
|
||||
|
||||
func (c *PubSub) Unsubscribe(channels ...string) error {
|
||||
return c.unsubscribe("UNSUBSCRIBE", channels...)
|
||||
}
|
||||
|
||||
func (c *PubSubClient) PUnsubscribe(patterns ...string) error {
|
||||
func (c *PubSub) PUnsubscribe(patterns ...string) error {
|
||||
return c.unsubscribe("PUNSUBSCRIBE", patterns...)
|
||||
}
|
||||
|
Reference in New Issue
Block a user