github.com/hdt3213/godis@v1.2.9/cluster/pubsub.go (about)

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