func listen() { c := *dial() defer c.Close() var wg sync.WaitGroup wg.Add(1) psc := redis.PubSubConn{c} go func() { psc.Subscribe("landlord.request") defer wg.Done() for { switch v := psc.Receive().(type) { case redis.Message: log.Printf("%s: message %s", v.Channel, v.Data) if instr := readInstruction(&v); instr != nil { go handleInstruction(instr) } // If we can't read the instruction, we don't know to whom we // should respond. This is a slight problem, which could be solved // by using a pattern subscription and embedding the ReplyTo in // the channel name. case error: log.Printf("Receive fail; %v", v) return } } }() wg.Wait() return }
// SubscribeHandler : Subsribes to redis to fetch messages func SubscribeHandler(subChannel chan string) { // create a subscribe connection to RedisDB subscribeConn, err := redisurl.ConnectToURL("redis://localhost:6379") if err != nil { fmt.Println(err) os.Exit(1) } // Before function exits close the connection defer subscribeConn.Close() pubsubConn := redis.PubSubConn{Conn: subscribeConn} pubsubConn.Subscribe("messages") // Subscribed to messages list in redis DB for { switch val := pubsubConn.Receive().(type) { case redis.Message: // If the data being received is a text message then push it to the channel subChannel <- string(val.Data) case redis.Subscription: //Handle Subscription here case error: return } } }
func Test_Redis(t *testing.T) { pool := newPool("123.56.98.103:6379", "") conn := pool.Get() fmt.Println(conn) // go func() { cc, err := redis.Dial("tcp", "123.56.98.103:6379") if err != nil { panic(err) } cc.Do("select", "9") psc := redis.PubSubConn{Conn: cc} psc.Subscribe("products") time.Sleep(1 * time.Second) for { switch v := psc.Receive().(type) { case redis.Subscription: fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count) case redis.Message: //单个订阅subscribe fmt.Printf("%s: message: %s\n", v.Channel, v.Data) case redis.PMessage: //模式订阅psubscribe fmt.Printf("PMessage: %s %s %s\n", v.Pattern, v.Channel, v.Data) case error: fmt.Println("error", v) time.Sleep(1 * time.Second) } } // }() }
func TestPublishJson(t *testing.T) { time.Sleep(1 * time.Second) emitter, _ := NewEmitter(&EmitterOpts{ Host: "localhost", Port: 6379, }) if emitter == nil { t.Error("emitter is nil") } c, _ := redis.Dial("tcp", "localhost:6379") defer c.Close() psc := redis.PubSubConn{Conn: c} psc.Subscribe("socket.io#emitter") emitter.Emit("jsondata", []byte(`{"name":"a","age":1,"bin":"abc"}`)) for { switch v := psc.Receive().(type) { case redis.Message: isContain := strings.Contains(string(v.Data), "abc") if !isContain { t.Errorf("%s not contains abc", v.Data) return } else { return } } } }
func (chat *Chat) Subscribe() { psc := redis.PubSubConn{pool.Get()} psc.Subscribe("chat") c := pool.Get() for { switch v := psc.Receive().(type) { case redis.Message: log.Printf("%s: message %s\n", v.Channel, v.Data) id, err := redis.Int(v.Data, nil) if err != nil { log.Println(err) return } result, err := redis.Values(c.Do("HGETALL", "message:"+strconv.Itoa(id))) if err != nil { log.Println(err) return } var message Message err = redis.ScanStruct(result, &message) if err != nil { log.Println(err) return } chat.outgoing <- &message case redis.Subscription: log.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count) case error: log.Println(v) return } } }
func (group_manager *GroupManager) RunOnce() bool { c, err := redis.Dial("tcp", config.redis_address) if err != nil { log.Info("dial redis error:", err) return false } psc := redis.PubSubConn{c} psc.Subscribe("group_create", "group_disband", "group_member_add", "group_member_remove") group_manager.Reload() for { switch v := psc.Receive().(type) { case redis.Message: if v.Channel == "group_create" { group_manager.HandleCreate(string(v.Data)) } else if v.Channel == "group_disband" { group_manager.HandleDisband(string(v.Data)) } else if v.Channel == "group_member_add" { group_manager.HandleMemberAdd(string(v.Data)) } else if v.Channel == "group_member_remove" { group_manager.HandleMemberRemove(string(v.Data)) } else { log.Infof("%s: message: %s\n", v.Channel, v.Data) } case redis.Subscription: log.Infof("%s: %s %d\n", v.Channel, v.Kind, v.Count) case error: log.Info("error:", v) return true } } }
func TestPublishBinary(t *testing.T) { time.Sleep(1 * time.Second) emitter, _ := NewEmitter(&EmitterOpts{ Host: "localhost", Port: 6379, }) if emitter == nil { t.Error("emitter is nil") } c, _ := redis.Dial("tcp", "localhost:6379") defer c.Close() psc := redis.PubSubConn{Conn: c} psc.Subscribe("socket.io#emitter") val := bytes.NewBufferString("aaabbbccc") emitter.EmitBinary("bin", val.Bytes()) for { switch v := psc.Receive().(type) { case redis.Message: isContain := strings.Contains(string(v.Data), "aaabbbccc") if !isContain { t.Errorf("%s not contains aaabbbccc", v.Data) return } else { return } } } }
func (self *Pump) subscribeRedis(channel string, msger *messenger.Messenger) { c, err := redis.Dial("tcp", os.Getenv("REDIS_ADDR")) if err != nil { panic(err) } defer c.Close() psc := redis.PubSubConn{c} psc.Subscribe(channel) for { switch v := psc.Receive().(type) { case redis.Message: fmt.Printf("PMessage: channel:%s data:%s\n", v.Channel, v.Data) msger.SendMessage(string(v.Data)) case redis.Subscription: log.Printf("Subscription: kind:%s channel:%s count:%d\n", v.Kind, v.Channel, v.Count) if v.Count == 0 { return } case error: log.Printf("error: %v\n", v) return } } }
func SubscribeToSentinel() { sentinel := GetSentinel() c, err := redis.Dial("tcp", sentinel) if err != nil { Fatal("Cannot connect to redis sentinel:", sentinel) } err = ValidateCurrentMaster() if err != nil { Fatal("Cannot switch to current master") } psc := redis.PubSubConn{c} Debug("Subscribing to sentinel (+switch-master).") psc.Subscribe("+switch-master") for { switch v := psc.Receive().(type) { case redis.Message: Debug(fmt.Sprintf("%s: message: %s", v.Channel, v.Data)) data := strings.Split(string(v.Data), string(' ')) SwitchMaster(data[0], data[3], data[4]) case redis.Subscription: Debug(fmt.Sprintf("%s: %s %d", v.Channel, v.Kind, v.Count)) case error: Fatal("Error with redis connection:", psc) } } }
func slaveHandler(s *websocket.Conn, sessionID int, dbStore *Store, redisAddr string) { xlog.Debugf("entering SlaveHandler") c, err := redis.Dial("tcp", redisAddr) if err != nil { xlog.Errorf("redis.Dial failed: %v", err) return } defer c.Close() psc := redis.PubSubConn{Conn: c} topic := fmt.Sprintf("session.%d", sessionID) psc.Subscribe(topic) defer psc.Unsubscribe(topic) for { switch v := psc.Receive().(type) { case redis.Message: StatCount("command for slave", 1) var cmd Command if err := json.Unmarshal(v.Data, &cmd); err != nil { break } if err := websocket.JSON.Send(s, cmd); err != nil { xlog.Errorf("slaveHandler: JSON.Send failed: %v", err) return } if cmd.Cmd == "close" { return } case redis.Subscription: xlog.Debugf("mkay... redis.Subscription received: %#v", v) } } }
func (this *RedisStore) Subscribe(c chan []byte, channel string) (redis.Conn, error) { conn, err := this.GetConn() if err != nil { return nil, err } psc := redis.PubSubConn{Conn: conn} psc.Subscribe(channel) go func() { defer conn.Close() for { switch v := psc.Receive().(type) { case redis.Message: c <- v.Data case redis.Subscription: case error: log.Printf("Error receiving: %s. Reconnecting...", v.Error()) conn, err = this.GetConn() if err != nil { log.Println(err) } psc = redis.PubSubConn{Conn: conn} psc.Subscribe(channel) } } }() return conn, nil }
// GetTaskResult fetchs task result for the specified taskID func (b *Broker) GetTaskResult(taskID string) <-chan *broker.Message { msg := make(chan *broker.Message) // fetch messages log.Debug("Waiting for Task Result Messages: ", taskID) conn := b.pool.Get() psc := redis.PubSubConn{Conn: conn} psc.Subscribe(taskID) go func() { for { switch v := psc.Receive().(type) { case redis.Message: log.Info("message: ", string(v.Data)) m := &broker.Message{} err := json.Unmarshal(v.Data, &m) if err != nil { log.Error("Failed to unmarshal message.") } else { log.Debug("Task Result message: ", string(m.Body)) msg <- m } psc.Unsubscribe() conn.Close() close(msg) break } } }() log.Debug("Subscribed to Task Result") return msg }
func TestPublishEnd(t *testing.T) { time.Sleep(1 * time.Second) emitter, _ := NewEmitter(&EmitterOpts{ Host: "localhost", Port: 6379, }) defer emitter.Close() c, _ := redis.Dial("tcp", "localhost:6379") defer c.Close() psc := redis.PubSubConn{Conn: c} psc.Subscribe("socket.io#emitter") emitter.Emit("finish") for { switch v := psc.Receive().(type) { case redis.Message: isContain := strings.Contains(string(v.Data), "finish") if !isContain { t.Errorf("%s not contains end", v.Data) return } else { return } } } }
// ReceiveMessages : Receive messages fron master_messages redis channel func ReceiveMessages(newSlaveChannel chan string, ipAddress string) { conn, err := redisurl.ConnectToURL(redisURL) if err != nil { fmt.Println(err) os.Exit(1) } // Close only when function exits defer conn.Close() // Creating a pubsubConn for master messages pubsubConn := redis.PubSubConn{Conn: conn} pubsubConn.Subscribe(masterMessageQueue) for { switch val := pubsubConn.Receive().(type) { case redis.Message: // If the data being received is a text message then push it to the channel newSlaveChannel <- string(val.Data) case redis.Subscription: //Handle Subscription here case error: return } } }
func ProcessNewBlock(conf *Config, rpool *redis.Pool, spool *redis.Pool) { log.Println("ProcessNewBlock startup") conn := rpool.Get() defer conn.Close() psc := redis.PubSubConn{Conn: conn} psc.Subscribe("btcplex:blocknotify") for { switch v := psc.Receive().(type) { case redis.Message: hash := string(v.Data) log.Printf("Processing new block: %v\n", hash) c := rpool.Get() newblock, err := SaveBlockFromRPC(conf, spool, hash) if err != nil { log.Printf("Error processing new block: %v\n", err) } else { // Once the block is processed, we can publish it as btcplex own blocknotify c.Do("PUBLISH", "btcplex:blocknotify2", hash) newblockjson, _ := json.Marshal(newblock) c.Do("PUBLISH", "btcplex:newblock", string(newblockjson)) } c.Close() } } }
func SubModifiedPasswd() error { r := Redix[_SubModifiedPasswd] RedixMu[_SubModifiedPasswd].Lock() defer RedixMu[_SubModifiedPasswd].Unlock() psc := redis.PubSubConn{Conn: r} err := psc.Subscribe(SubModifiedPasswdKey) if err != nil { return err } ch := make(chan []byte, 128) go func() { defer psc.Close() for { data := psc.Receive() switch n := data.(type) { case redis.Message: ch <- n.Data case redis.Subscription: if n.Count == 0 { glog.Fatalf("Subscription: %s %s %d, %v\n", n.Kind, n.Channel, n.Count, n) return } case error: glog.Errorf("[modifypwd|redis] sub of error: %v\n", n) return } } }() go HandleModifiedPasswd(ch) return nil }
func (self *Server) ReadFrames() { c, err := redis.Dial("tcp", ":6379") if err != nil { panic(err) } psc := redis.PubSubConn{c} psc.Subscribe("pokemon.streams.frames") for { switch v := psc.Receive().(type) { case redis.Message: frame := &Frame{} err := json.Unmarshal(v.Data, &frame) if err != nil { continue } self.sendAll <- frame.Delta self.frame = "0\t" + frame.Dithered case redis.Subscription: fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count) case error: panic(v) } } }
// run receives pubsub messages from Redis after establishing a connection. // When a valid message is received it is broadcast to all connected websockets func (rr *redisReceiver) run() { conn := rr.pool.Get() defer conn.Close() psc := redis.PubSubConn{conn} psc.Subscribe(CHANNEL) for { switch v := psc.Receive().(type) { case redis.Message: log.WithFields(log.Fields{ "channel": v.Channel, "message": string(v.Data), }).Println("Redis Message Received") msg, err := validateMessage(v.Data) if err != nil { log.WithFields(log.Fields{ "err": err, "data": v.Data, "msg": msg, }).Error("Error unmarshalling message from Redis") continue } rr.broadcast(v.Data) case redis.Subscription: log.WithFields(log.Fields{ "channel": v.Channel, "kind": v.Kind, "count": v.Count, }).Println("Redis Subscription Received") case error: log.WithField("err", v).Errorf("Error while subscribed to Redis channel %s", CHANNEL) default: log.WithField("v", v).Println("Unknown Redis receive during subscription") } } }
func (logger *RedisLogger) Subscribe(psc *redis.PubSubConn, groups ...string) error { var channels []interface{} for _, group := range groups { channels = append(channels, redisPubSubGroup+group) } return psc.Subscribe(channels...) }
func (c *connection) readFromRedis() { conn := pool.Get() defer conn.Close() psc := redis.PubSubConn{conn} if err := psc.Subscribe(c.subscription); err != nil { log.Fatalf("Failed to subscribe to %v: %v", c.subscription, err) return } log.Printf("Connected to redis channel %v", c.subscription) for { switch v := psc.Receive().(type) { case redis.Message: log.Printf("Got a redis message: %v", v) c.send <- v.Data case redis.Subscription: log.Print("Got a redis subscription") // XXX nop? case error: log.Fatalf("Error reading messages: %v", v) default: log.Fatalf("Got an unknown redis message type: %v", v) } } }
func TestPublish(t *testing.T) { emitter, _ := NewEmitter(&EmitterOpts{ Host: "localhost", Port: 6379, }) if emitter == nil { t.Error("emitter is nil") } c, _ := redis.Dial("tcp", "localhost:6379") defer c.Close() psc := redis.PubSubConn{Conn: c} psc.Subscribe("socket.io#emitter") emitter.Emit("text", "hogefuga") for { switch v := psc.Receive().(type) { case redis.Message: isContain := strings.Contains(string(v.Data), "hogefuga") if !isContain { t.Errorf("%s not contains hogefuga", v.Data) return } else { return } } } }
// respondToPings continuously listens for pings from other worker pools and // immediately responds with a pong. It will only return if there is an error. func (p *Pool) respondToPings() error { pong := redisPool.Get() ping := redis.PubSubConn{redisPool.Get()} defer func() { pong.Close() ping.Close() }() // Subscribe to the ping key for this pool to receive pings. if err := ping.Subscribe(p.pingKey()); err != nil { return err } for { // Whenever we recieve a ping, reply immediately with a pong by // publishing to the pong key for this pool. switch reply := ping.Receive().(type) { case redis.Message: if _, err := pong.Do("PUBLISH", p.pongKey(), 0); err != nil { return err } case error: err := reply.(error) return err } time.Sleep(1 * time.Millisecond) } }
// StartPubSubHandler will listen for a signal and run the callback with the message func (r *RedisClusterStorageManager) StartPubSubHandler(channel string, callback func(redis.Message)) error { if r.db == nil { return errors.New("Redis connection failed") } handle := r.db.RandomRedisHandle() if handle == nil { return errors.New("Redis connection failed") } psc := redis.PubSubConn{r.db.RandomRedisHandle().Pool.Get()} psc.Subscribe(channel) for { switch v := psc.Receive().(type) { case redis.Message: callback(v) case redis.Subscription: log.Debug("Subscription started: ", v.Channel) case error: log.Error("Redis disconnected or error received, attempting to reconnect: ", v) return v } } return errors.New("Connection closed.") return nil }
func redisSubscriber() { conn, err := redis.Dial("tcp", "redis:6379") if err != nil { panic("I don't want to live on this planet anymore") } psc := redis.PubSubConn{conn} psc.Subscribe("candy") for { switch v := psc.Receive().(type) { case redis.Message: fmt.Printf("%s: message: %s\n", v.Channel, v.Data) if v.Channel == "candy" { var c Candy err := json.Unmarshal(v.Data, &c) if err != nil { log.Printf("Seems our redis is sick! In the evening we'll get some schnaps to ease the pain!") continue } redisChan <- c } case redis.Subscription: fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count) case error: log.Println(v.Error()) } } }
func (cs *CustomerService) RunOnce() bool { c, err := redis.Dial("tcp", config.redis_address) if err != nil { log.Info("dial redis error:", err) return false } psc := redis.PubSubConn{c} psc.Subscribe("application_update") cs.Clear() for { switch v := psc.Receive().(type) { case redis.Message: if v.Channel == "application_update" { cs.HandleUpdate(string(v.Data)) } else { log.Infof("%s: message: %s\n", v.Channel, v.Data) } case redis.Subscription: log.Infof("%s: %s %d\n", v.Channel, v.Kind, v.Count) case error: log.Info("error:", v) return true } } }
func main() { c, err := redis.Dial("tcp", ":6379") if err != nil { panic(err) } defer c.Close() psc := redis.PubSubConn{Conn: c} psc.Subscribe("example") psc.PSubscribe("p*") for { switch n := psc.Receive().(type) { case redis.Message: fmt.Printf("Message: %s %s\n", n.Channel, n.Data) case redis.PMessage: fmt.Printf("PMessage: %s %s %s\n", n.Pattern, n.Channel, n.Data) case redis.Subscription: fmt.Printf("Subscription: %s %s %d\n", n.Kind, n.Channel, n.Count) if n.Count == 0 { return } case error: fmt.Printf("error: %v\n", n) return } } }
func (c *Cache) ListenToChannel(channel string, callback func(line string)) error { // Listening on the "dead" channel to get dead notifications by Hipache // Format received on the channel is: // -> frontend_key;backend_url;backend_id;number_of_backends // Example: "localhost;http://localhost:4242;0;1" conn := c.pool.Get() psc := redis.PubSubConn{conn} psc.Subscribe(channel) go func() { defer conn.Close() for { switch v := psc.Receive().(type) { case redis.Message: callback(string(v.Data[:])) case error: conn.Close() conn := c.pool.Get() time.Sleep(10 * time.Second) psc = redis.PubSubConn{conn} psc.Subscribe(channel) } } }() return nil }
func main() { addr := flag.String("addr", ":6379", "redis address") format := flag.String("fmt", `{"%s":"%s"}`, "data format") flag.Parse() args := flag.Args() conn, err := redis.Dial("tcp", *addr) if err != nil { log.Fatal(err) return } psc := redis.PubSubConn{conn} for _, channel := range args { psc.Subscribe(channel) } for { switch v := psc.Receive().(type) { case redis.Message: fmt.Println(fmt.Sprintf(*format, v.Channel, v.Data)) case redis.Subscription: fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count) case error: log.Println(v) } } }
func TestPushed(t *testing.T) { pc := dialt(t) defer pc.Close() nc, err := net.Dial("tcp", ":6379") if err != nil { t.Fatal(err) } defer nc.Close() nc.SetReadDeadline(time.Now().Add(4 * time.Second)) c := redis.PubSubConn{Conn: redis.NewConn(nc, 0, 0)} c.Subscribe("c1") expectPushed(t, c, "Subscribe(c1)", redis.Subscription{Kind: "subscribe", Channel: "c1", Count: 1}) c.Subscribe("c2") expectPushed(t, c, "Subscribe(c2)", redis.Subscription{Kind: "subscribe", Channel: "c2", Count: 2}) c.PSubscribe("p1") expectPushed(t, c, "PSubscribe(p1)", redis.Subscription{Kind: "psubscribe", Channel: "p1", Count: 3}) c.PSubscribe("p2") expectPushed(t, c, "PSubscribe(p2)", redis.Subscription{Kind: "psubscribe", Channel: "p2", Count: 4}) c.PUnsubscribe() expectPushed(t, c, "Punsubscribe(p1)", redis.Subscription{Kind: "punsubscribe", Channel: "p1", Count: 3}) expectPushed(t, c, "Punsubscribe()", redis.Subscription{Kind: "punsubscribe", Channel: "p2", Count: 2}) pc.Do("PUBLISH", "c1", "hello") expectPushed(t, c, "PUBLISH c1 hello", redis.Message{Channel: "c1", Data: []byte("hello")}) }
// NewReceiverFunc returns the function that // listens of redis for start/stop commands func NewReceiverFunc(redisAddr string, redisDB int) pingd.Receiver { return func(startHostCh, stopHostCh chan<- pingd.Host) { conPubSub, err := redis.Dial("tcp", redisAddr) if err != nil { log.Panicln(err) } connKV, err := redis.Dial("tcp", redisAddr) if err != nil { log.Panicln(err) } servername, _ := os.Hostname() conPubSub.Do("CLIENT", "SETNAME", "receive-"+servername) conPubSub.Do("SELECT", redisDB) connKV.Do("CLIENT", "SETNAME", "receive-"+servername) connKV.Do("SELECT", redisDB) psc := redis.PubSubConn{conPubSub} psc.Subscribe(startRK, stopRK) for { switch n := psc.Receive().(type) { case redis.Message: if n.Channel == startRK { host := string(n.Data) down := false if strings.HasSuffix(host, downSuffix) { down = true host = strings.Replace(host, downSuffix, "", 1) } // Add to the list of pinged hosts _, err := connKV.Do("SADD", hostListRK, host) if err != nil { log.Panicln(err) } startHostCh <- pingd.Host{Host: host, Down: down} } else if n.Channel == stopRK { host := string(n.Data) // Remove from the list of pinged hosts _, err := connKV.Do("SREM", hostListRK, host) if err != nil { log.Panicln(err) } stopHostCh <- pingd.Host{Host: host} } case redis.PMessage: case redis.Subscription: log.Println("BOOT Listening to " + n.Channel) case error: log.Printf("error: %v\n", n) return } } } }