Exemplo n.º 1
0
func startLoopRedis() {
	redisClient, err := cluster.NewWithOpts(cluster.Opts{
		Addr:    hostRedis,
		Timeout: 1 * time.Second,
	})
	if err != nil {
		log.Fatal("connect redis error:", err)
		return
	}

	go func() {
		for {
			str, _ := redisClient.Cmd("rpop", NJS).Str()
			if str != "" {
				log.Println(str)
				dec := json.NewDecoder(strings.NewReader(str))
				dec.UseNumber()
				var message map[string]interface{}
				if err := dec.Decode(&message); err == nil {
					if ctxNumber, ok := message["ctx"].(json.Number); ok {
						if ctx, err := ctxNumber.Int64(); err == nil {
							if ct, ok := requestsResponseWaitings[ctx]; ok {
								c := ct.C
								c <- str
								close(c)
								delete(requestsResponseWaitings, ctx)
							}
						}
					}
				}
			}
		}
	}()

	go func() {
		for {
			str, _ := redisClient.Cmd("rpop", fmt.Sprintf("op_%v_%v", UID, IID)).Str()
			if str != "" {
				log.Println(str)
				dec := json.NewDecoder(strings.NewReader(str))
				dec.UseNumber()
				var message map[string]interface{}
				if err := dec.Decode(&message); err == nil {
					if ctxNumber, ok := message["ctx"].(json.Number); ok {
						if ctx, err := ctxNumber.Int64(); err == nil {
							if ct, ok := requestsResponseWaitings[ctx]; ok {
								c := ct.C
								c <- str
								close(c)
								delete(requestsResponseWaitings, ctx)
							}
						}
					}
				}
			}
		}
	}()
}
Exemplo n.º 2
0
func createRedisClient() {
	var err error
	redisClient, err = cluster.NewWithOpts(cluster.Opts{
		Addr:    *hostRedis,
		Timeout: 1 * time.Second,
	})
	if err != nil {
		log.Fatal("connect redis error:", err)
	}
}
Exemplo n.º 3
0
func newClusterDB() (DBer, error) {
	log.L.Printf("connecting to redis cluster at %s", config.RedisAddr)
	c, err := cluster.NewWithOpts(cluster.Opts{
		Addr:     config.RedisAddr,
		PoolSize: config.RedisPoolSize,
	})
	if err != nil {
		log.L.Fatal(err)
	}
	return &clusterDB{c}, err
}
Exemplo n.º 4
0
func main() {
	fmt.Println("Hello World!")
	log.SetFlags(log.Ldate | log.Lmicroseconds)

	redisClient, err := cluster.NewWithOpts(cluster.Opts{
		Addr:    hostRedis,
		Timeout: 1 * time.Second,
	})
	if err != nil {
		log.Fatal("connect redis error:", err)
	}

	//	log.Print("start lpush")

	//	for i := 0; i < 10000; i++ {
	//		redisClient.Cmd("lpush", keyName, "test_content_")
	//	}

	//	log.Print("stop lpush")

	ret := make(chan int)
	times := 10000

	log.Print("start rpop")

	for i := 0; i < times; i++ {
		go func() {
			_, _ = redisClient.Cmd("rpop", keyName).Str()
			//		fmt.Println(s)
			ret <- 1
		}()
	}

	for i := 0; i < times; i++ {
		<-ret
	}

	log.Print("stop rpop")
}