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
			}
		}
	}
}
Beispiel #2
0
// 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
		}

	}
}
Beispiel #3
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")
		}
	}
}
Beispiel #4
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)
		}
	}
	// }()
}
// 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")
		}
	}
}
Beispiel #6
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
}
Beispiel #7
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)
		}
	}
}
Beispiel #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()
		}
	}
}
Beispiel #9
0
// 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
			}
		}
	}
}
Beispiel #10
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())
		}
	}
}
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)
		}
	}
}
Beispiel #12
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)
		}
	}
}
Beispiel #13
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)
		}
	}
}
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 (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
		}
	}
}
// 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
}
Beispiel #17
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
		}
	}
}
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
			}
		}
	}
}
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
			}
		}
	}
}
Beispiel #20
0
func (logger *RedisLogger) Unsubscribe(psc *redis.PubSubConn, groups ...string) error {
	var channels []interface{}
	for _, group := range groups {
		channels = append(channels, redisPubSubGroup+group)
	}
	return psc.Unsubscribe(channels...)
}
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
			}
		}
	}
}
Beispiel #22
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
		}
	}

}
Beispiel #23
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
		}
	}
}
Beispiel #24
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)
		}
	}
}
Beispiel #25
0
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
}
Beispiel #26
0
func RedisUnSub(key string, psc redis.PubSubConn) error {
	if err := psc.Unsubscribe(key); err != nil {
		Log.Printf("psc.Unsubscribe(\"%s\") faild (%s)", key, err.Error())
		return err
	}

	return nil
}
Beispiel #27
0
func (logger *RedisLogger) PubSub(userid string, groups ...string) *redis.PubSubConn {
	channels := []interface{}{redisPubSubUser + userid}
	for _, group := range groups {
		channels = append(channels, redisPubSubGroup+group)
	}
	conn := redis.PubSubConn{logger.pool.Get()}
	conn.Subscribe(channels...)
	return &conn
}
Beispiel #28
0
func (c *Cache) updateEvents() {
	var disconnected bool = false
connect:
	for {
		rconn, err := redis.DialTimeout("tcp", GetConfig().RedisAddress, redisConnectionTimeout, 0, 200*time.Millisecond)
		if err != nil {
			disconnected = true
			time.Sleep(50 * time.Millisecond)
			continue
		}
		if _, err = rconn.Do("PING"); err != nil {
			// Doing a PING after (re-connection) prevents cases where redis
			// is currently loading the dataset and is still not ready.
			// "LOADING Redis is loading the dataset in memory"
			rconn.Close()
			log.Info("Redis is loading the dataset in memory")
			time.Sleep(500 * time.Millisecond)
			continue
		}
		log.Info("Redis connected, subscribing pubsub.")
		psc := redis.PubSubConn{Conn: rconn}
		psc.Subscribe(FILE_UPDATE)
		psc.Subscribe(MIRROR_UPDATE)
		psc.Subscribe(MIRROR_FILE_UPDATE)
		if disconnected == true {
			// This is a way to keep the cache active while disconnected
			// from redis but still clear the cache (possibly outdated)
			// after a successful reconnection.
			disconnected = false
			c.Clear()
		}
		for {
			switch v := psc.Receive().(type) {
			case redis.Message:
				//if os.Getenv("DEBUG") != "" {
				//	fmt.Printf("Redis message on channel %s: message: %s\n", v.Channel, v.Data)
				//}
				c.handleMessage(v.Channel, v.Data)
			case redis.Subscription:
				if os.Getenv("DEBUG") != "" {
					log.Debug("Redis subscription event on channel %s: %s %d\n", v.Channel, v.Kind, v.Count)
				}
			case error:
				log.Error("UpdateEvents error: %s", v)
				psc.Close()
				rconn.Close()
				time.Sleep(50 * time.Millisecond)
				disconnected = true
				goto connect
			}
		}
	}
}
Beispiel #29
0
func userSubscribe(userId string, channel string) (*redis.PubSubConn, error) {
	c, err := getRedisConnection()
	if err != nil {
		return nil, err
	}
	psc := redis.PubSubConn{c}
	err = psc.Subscribe(userId + ":" + channel)
	if err != nil {
		return nil, err
	}
	return &psc, nil
}
Beispiel #30
0
func NewPollHub(id string, psConn *redis.PubSubConn) *PollHub {
	psConn.Subscribe(fmt.Sprintf("polls:%s", id))
	pollHub := &PollHub{
		pollId:      id,
		psConn:      psConn,
		vote:        make(chan uint),
		connections: make(map[*conn]bool),
		register:    make(chan *conn),
		unregister:  make(chan *conn),
	}
	go pollHub.run()
	return pollHub
}