示例#1
0
func newwriter(addr string, pswd string, db int64, channel string) (*Writer, error) {

	client := redis.NewClient(&redis.Options{
		Addr:     addr,
		Password: pswd,
		DB:       db,
	})

	_, err := client.Ping().Result()

	if err != nil {
		return nil, err
	}

	pubsub, err := client.Subscribe(channel)

	if err != nil {
		return nil, err
	}

	defer pubsub.Close()

	w := Writer{
		Channel: channel,
		Client:  client,
		Pubsub:  pubsub,
	}

	return &w, nil
}
示例#2
0
文件: shard.go 项目: d0ngw/go
func (self *jedisShard) add(nodes []RedisNodeOpt) {
	for i, nodeOpt := range nodes {
		weight := nodeOpt.Weight
		if weight <= 0 {
			weight = 1
		}
		client := r.NewClient(nodeOpt.opt())
		//检测是否连通
		if nodeOpt.PingOnInit {
			status := client.Ping()
			if status.Err() != nil {
				panic(status.Err())
			}
		}
		self.clients = append(self.clients, client)
		for n := 0; n < int(160*weight); n++ {
			var name string
			if len(nodeOpt.ShardName) == 0 {
				name = fmt.Sprintf("SHARD-%d-NODE-%d", i, n)
			} else {
				name = fmt.Sprintf("%s*%d%d", nodeOpt.ShardName, weight, n)
			}
			hashKey := self.hashFunc([]byte(name))
			treeNode := &jedisTreeNode{key: hashKey, clientIndex: i, nodeOpt: nodeOpt}
			self.tree.Insert(treeNode)
		}
	}
}
示例#3
0
文件: shard.go 项目: d0ngw/go
//添加Redis节点,如果重复添加相同的节点会报错
func (self *redisSliceShard) add(nodes []RedisNodeOpt) {
	for _, nodeOpt := range nodes {
		client := r.NewClient(nodeOpt.opt())
		//检测是否连通
		if nodeOpt.PingOnInit {
			status := client.Ping()
			if status.Err() != nil {
				panic(status.Err())
			}
		}
		shardOpt := nodeOpt
		shard := &sliceShard{
			nodeOpt: &shardOpt,
			c:       client}
		self.shards = append(self.shards, shard)
	}
}
示例#4
0
func init() {
	// check rsa
	var signBytes []byte
	var verifyBytes []byte
	var err error
	log.Print("opening app.rsa keys")
	signBytes, err = ioutil.ReadFile(privKeyPath)
	if err != nil {
		log.Print(err)
	}
	signKey, err = jwt.ParseRSAPrivateKeyFromPEM(signBytes)
	if err != nil {
		log.Print(err)
	}
	verifyBytes, err = ioutil.ReadFile(pubKeyPath)
	if err != nil {
		log.Print(err)
	}
	verifyKey, err = jwt.ParseRSAPublicKeyFromPEM(verifyBytes)
	if err != nil {
		log.Print(err)
	}

	log.Print("testing postgres connection")
	// check postgres
	db, err = sql.Open("postgres", "user=thoriumnet password=thoriumtest dbname=thoriumnet host=localhost")
	if err != nil {
		log.Fatal(err)
	}

	log.Print("testing redis connection")
	// check redis
	kvstore = redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "",
		DB:       0,
	})

	_, err = kvstore.Ping().Result()
	if err != nil {
		log.Fatal(err)
	}

	log.Print("thordb initialization complete")
}
示例#5
0
func FetchData(c *cli.Context) error {
	client := redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%s", host, port),
		Password: password,
		DB:       0, // use default DB
	})
	info := client.Info().String()
	s := strings.NewReader(info)
	buf := bufio.NewReader(s)
	metrics := []Metric{}
	for {
		line, err := buf.ReadString('\n')
		if err == io.EOF {
			break
		}
		kv := strings.Split(strings.TrimSpace(line), ":")
		if len(kv) != 2 {
			continue
		}
		key, value := kv[0], kv[1]
		if t, ok := infoKeys[key]; ok {
			metric := Metric{
				Metric:   fmt.Sprintf("redis.%s", strings.ToLower(key)),
				DataType: t,
				Value:    0,
				Tags: map[string]string{
					"port": port,
				},
			}
			if value == "up" {
				metric.Value = 1
			} else {
				metric.Value, _ = strconv.ParseFloat(value, 64)
			}
			metrics = append(metrics, metric)
		}
	}
	res, err := json.MarshalIndent(metrics, "", "  ")
	if err != nil {
		return err
	}
	fmt.Println(string(res))
	return nil
}