Esempio n. 1
0
func SubCommonMsg() error {
	r := Redix[_SubCommonMsg]
	RedixMu[_SubCommonMsg].Lock()
	defer RedixMu[_SubCommonMsg].Unlock()

	psc := redis.PubSubConn{Conn: r}
	err := psc.PSubscribe(SubCommonMsgKey)
	if err != nil {
		return err
	}
	ch := make(chan redis.PMessage, 128)
	go func() {
		defer psc.Close()
		for {
			data := psc.Receive()
			switch m := data.(type) {
			case redis.PMessage:
				ch <- m
			case redis.Subscription:
				if m.Count == 0 {
					glog.Fatalf("Subscription: %s %s %d, %v\n", m.Kind, m.Channel, m.Count, m)
					return
				}
			case error:
				glog.Errorf("[modifypwd|redis] sub of error: %v\n", m)
				return
			}
		}
	}()
	go HandleCommonMsg(ch)
	return nil
}
Esempio n. 2
0
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
}
Esempio n. 3
0
func RedisSub(key string) (chan interface{}, redis.PubSubConn, error) {
	mq := make(chan interface{}, Conf.RedisMQSize)
	c := redisPool.Get()
	defer c.Close()
	pc, err := redis.Dial(Conf.RedisNetwork, Conf.RedisAddr)
	if err != nil {
		Log.Printf("redis.Dial(\"%s\", \"%s\") failed (%s)", Conf.RedisNetwork, Conf.RedisAddr, err.Error())
		return nil, redis.PubSubConn{}, err
	}

	psc := redis.PubSubConn{pc}
	// check queue
	err = redisQueue(c, key, mq)
	if err != nil {
		Log.Printf("redisQueue failed (%s)", err.Error())
		return nil, redis.PubSubConn{}, err
	}
	// subscribe
	psc.Subscribe(key)
	if _, ok := psc.Receive().(redis.Subscription); !ok {
		Log.Printf("init sub must redis.Subscription")
		return nil, redis.PubSubConn{}, fmt.Errorf("first sub must init")
	}
	// double check
	err = redisQueue(c, key, mq)
	if err != nil {
		Log.Printf("redisQueue failed (%s)", err.Error())
		return nil, redis.PubSubConn{}, err
	}

	go func() {
		// DEBUG
		Log.Printf("redis routine start")
		// DEBUG
		defer Log.Printf("redis routine exit")
		defer psc.Close()
		for {
			switch n := psc.Receive().(type) {
			case redis.Message:
				mq <- string(n.Data)
			case redis.PMessage:
				mq <- string(n.Data)
			case redis.Subscription:
				// DEBUG
				Log.Printf("redis UnSubscrption")
				return
			case error:
				Log.Printf("psc.Receive() failed (%s)", n.Error())
				mq <- n
				return
			}
		}
	}()

	return mq, psc, nil
}
Esempio n. 4
0
// pingAndPurgeIfNeeded pings other by publishing to others ping key. If it
// does not receive a pong reply within some amount of time, it will
// assume the pool is stale and purge it.
func (p *Pool) pingAndPurgeIfNeeded(other *Pool) error {
	ping := redisPool.Get()
	pong := redis.PubSubConn{redisPool.Get()}
	// Listen for pongs by subscribing to the other pool's pong key
	pong.Subscribe(other.pongKey())
	// Ping the other pool by publishing to its ping key
	ping.Do("PUBLISH", other.pingKey(), 1)
	// Use a select statement to either receive the pong or timeout
	pongChan := make(chan interface{})
	errChan := make(chan error)
	go func() {
		defer func() {
			pong.Close()
			ping.Close()
		}()
		select {
		case <-p.exit:
			return
		default:
		}
		for {
			reply := pong.Receive()
			switch reply.(type) {
			case redis.Message:
				// The pong was received
				pongChan <- reply
				return
			case error:
				// There was some unexpected error
				err := reply.(error)
				errChan <- err
				return
			}
		}
	}()
	timeout := time.After(p.config.StaleTimeout)
	select {
	case <-pongChan:
		// The other pool responded with a pong
		return nil
	case err := <-errChan:
		// Received an error from the pubsub conn
		return err
	case <-timeout:
		// The pool is considered stale and should be purged
		t := newTransaction()
		other.RLock()
		otherId := other.id
		other.RUnlock()
		t.purgeStalePool(otherId)
		if err := t.exec(); err != nil {
			return err
		}
	}
	return nil
}
func subscribe(emitter *emission.Emitter) {
	conn, _ := redis.Dial("tcp", ":6379")
	channel := redis.PubSubConn{conn}
	channel.Subscribe(CHANNEL_NAME)
	for {
		reply := channel.Receive()
		switch parsed := reply.(type) {
		case redis.Message:
			message := string(parsed.Data)
			emitter.Emit("message", message)
		}
	}
}
Esempio n. 6
0
func listen(connection redis.Conn) {
	pubSubConnection := redis.PubSubConn{connection}

	pubSubConnection.Subscribe(redisChannel)
	log.Println("Listening to redis '" + redisChannel + "' channel...")

	for {
		reply, message := pubSubConnection.Receive().(redis.Message)
		if message {
			hit := parseMessage(reply)
			hits <- hit
			log.Print("[" + hit.Code + "] " + hit.Host + hit.Path)
		}
	}
}
Esempio n. 7
0
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)
		}
	}
}
Esempio n. 8
0
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()
		}
	}
}
Esempio n. 9
0
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)
		}
	}
}
Esempio n. 10
0
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
		}
	}
}
Esempio n. 11
0
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
}
Esempio n. 12
0
// 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
}
Esempio n. 13
0
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
		}
	}
}
Esempio n. 14
0
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 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
			}
		}
	}
}
Esempio n. 16
0
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)
		}
	}
}
Esempio n. 17
0
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
		}
	}
}
Esempio n. 18
0
func (rpsi *RedisPubSubInput) Run(ir pipeline.InputRunner, h pipeline.PluginHelper) error {
	var (
		dRunner pipeline.DecoderRunner
		decoder pipeline.Decoder
		pack    *pipeline.PipelinePack
		e       error
		ok      bool
	)
	// Get the InputRunner's chan to receive empty PipelinePacks
	packSupply := ir.InChan()

	if rpsi.conf.DecoderName != "" {
		if dRunner, ok = h.DecoderRunner(rpsi.conf.DecoderName, fmt.Sprintf("%s-%s", ir.Name(), rpsi.conf.DecoderName)); !ok {
			return fmt.Errorf("Decoder not found: %s", rpsi.conf.DecoderName)
		}
		decoder = dRunner.Decoder()
	}

	//Connect to the channel
	psc := redis.PubSubConn{Conn: rpsi.conn}
	psc.PSubscribe(rpsi.conf.Channel)

	for {
		switch n := psc.Receive().(type) {
		case redis.PMessage:
			// Grab an empty PipelinePack from the InputRunner
			pack = <-packSupply
			pack.Message.SetType("redis_pub_sub")
			pack.Message.SetLogger(n.Channel)
			pack.Message.SetPayload(string(n.Data))
			pack.Message.SetTimestamp(time.Now().UnixNano())
			var packs []*pipeline.PipelinePack
			if decoder == nil {
				packs = []*pipeline.PipelinePack{pack}
			} else {
				packs, e = decoder.Decode(pack)
			}
			if packs != nil {
				for _, p := range packs {
					ir.Inject(p)
				}
			} else {
				if e != nil {
					ir.LogError(fmt.Errorf("Couldn't parse Redis message: %s", n.Data))
				}
				pack.Recycle(nil)
			}
		case redis.Subscription:
			ir.LogMessage(fmt.Sprintf("Subscription: %s %s %d\n", n.Kind, n.Channel, n.Count))
			if n.Count == 0 {
				return errors.New("No channel to subscribe")
			}
		case error:
			fmt.Printf("error: %v\n", n)
			return n
		}
	}

	return nil
}
// 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")
		}
	}
}
Esempio n. 20
0
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 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
			}
		}
	}
}
Esempio n. 22
0
// 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)
	}
}
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
			}
		}
	}
}
Esempio n. 24
0
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())
		}
	}
}
Esempio n. 25
0
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
		}
	}
}
Esempio n. 26
0
func main3() {
	//INIT OMIT
	c, err := redis.Dial("tcp", ":6379")
	if err != nil {
		panic(err)
	}
	defer c.Close()

	//set
	c.Do("SET", "message1", "Hello World")

	//get
	world, err := redis.String(c.Do("GET", "message1"))
	if err != nil {
		fmt.Println("key not found")
	}

	fmt.Println(world)
	//ENDINIT OMIT

	psc := redis.PubSubConn{c}
	psc.PSubscribe("bigbluebutton:to-bbb-apps:system")
	for {
		switch v := psc.Receive().(type) {
		case redis.Message:
			fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
		case redis.PMessage:
			fmt.Printf("PMessage: %s %s %s\n", v.Pattern, v.Channel, v.Data)
		case redis.Subscription:
			fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
		case error:
			fmt.Printf("error: %v\n", v)
		}
	}
}
Esempio n. 27
0
// 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
		}
	}

}
Esempio n. 28
0
func (client *RedisStorage) Receive(psc redis.PubSubConn) *Message {
	switch message := psc.Receive().(type) {
	case redis.Message:
		return &Message{"message", message.Channel, string(message.Data)}
	}
	return nil
}
Esempio n. 29
0
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)
		}
	}
	// }()
}
Esempio n. 30
0
// Monitor sentinel
func MonitorSentinel() {
	redisConn := gRedisPool.Get()
	defer redisConn.Close()

	psc := redis.PubSubConn{redisConn}
	psc.PSubscribe("*")
	runflag := true
	for runflag {
		switch v := psc.Receive().(type) {
		case redis.Message:
			log.Infof("Type Message>>channel %s, message: %s", v.Channel, v.Data)
		case redis.Subscription:
			log.Infof("Type Subscribe>>channel %s, kind %s, count %d", v.Channel, v.Kind, v.Count)
			gRecoveryChan <- RECOVERY_TYPE_REDIS
		case error:
			log.Error("MonitorSentinel ERROR")
			runflag = false
			// Should re psubscrebe
		case redis.PMessage:
			log.Infof("Type PMessage>>channel %s, pattern %s, data %s", v.Channel, v.Pattern, v.Data)
			ParsePMessage(v)
		default:
			log.Warnf("Unkown Message Type of psubscribe")
		}
	}
}