-
Notifications
You must be signed in to change notification settings - Fork 553
/
pubsub.go
47 lines (40 loc) · 1.51 KB
/
pubsub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package cluster
import (
"github.com/hdt3213/godis/interface/redis"
"github.com/hdt3213/godis/lib/logger"
"github.com/hdt3213/godis/redis/reply"
)
const (
relayPublish = "_publish"
publish = "publish"
)
var (
publishRelayCmd = []byte(relayPublish)
publishCmd = []byte(publish)
)
// Publish broadcasts msg to all peers in cluster when receive publish command from client
func Publish(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
var count int64 = 0
results := cluster.broadcast(c, args)
for _, val := range results {
if errReply, ok := val.(reply.ErrorReply); ok {
logger.Error("publish occurs error: " + errReply.Error())
} else if intReply, ok := val.(*reply.IntReply); ok {
count += intReply.Code
}
}
return reply.MakeIntReply(count)
}
// onRelayedPublish receives publish command from peer, just publish to local subscribing clients, do not relay to peers
func onRelayedPublish(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
args[0] = publishCmd
return cluster.db.Exec(c, args) // let local db.hub handle publish
}
// Subscribe puts the given connection into the given channel
func Subscribe(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
return cluster.db.Exec(c, args) // let local db.hub handle subscribe
}
// UnSubscribe removes the given connection from the given channel
func UnSubscribe(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
return cluster.db.Exec(c, args) // let local db.hub handle subscribe
}