コード例 #1
0
ファイル: redis_bns.go プロジェクト: qiangmzsx/golang
// NewRedisClient(): create a new redisClient with bns support
// Notice:
//    - if resolve bns error, c.Servers will be empty.
// Params:
//    - bnsName: string, bns name of redis server
//    - maxIdleCon: int, max idle connections in connection pool
//    - connectTimeout: int, connect redis server timeout, in ms
//    - readTimeout: int, read redis server timeout, in ms
//    - writeTimeout: int, write redis server timeout, in ms
//    - state: *module_state2.State, module state
// Returns:
//    - *redisClient: a new redis client
func NewRedisClient(bnsName, idc string, maxIdleCon int, connectTimeout, readTimeout, writeTimeout, idleTimeout int,
	state *module_state2.State) *RedisClient {
	var err error
	// create RedisClient
	c := &RedisClient{
		bnsClient: bns.NewClient(),

		// timeout in ms
		ConnectTimeout: time.Duration(connectTimeout) * time.Millisecond,
		ReadTimeout:    time.Duration(readTimeout) * time.Millisecond,
		WriteTimeout:   time.Duration(writeTimeout) * time.Millisecond,
		IdleTimeout:    time.Duration(idleTimeout) * time.Millisecond,
		// max idle connection
		MaxIdle: maxIdleCon,

		// module state
		state: state,
	}

	// if resolve bns error, c.Servers will be empty
	if c.Servers, err = bns.GetAddr(c.bnsClient, bnsName, idc); err != nil {
		//log.Logger.Warn("get instance for %s err %s", bnsName, err.Error())
	}
	go c.checkServerInstance(bnsName, idc) // goroutine to update bns

	// set connection pool
	c.pool = &redis.Pool{
		MaxIdle:     c.MaxIdle,
		IdleTimeout: c.IdleTimeout,
		Dial:        c.dial,
	}

	return c
}
コード例 #2
0
ファイル: redis_bns.go プロジェクト: qiangmzsx/golang
// update bns
func (c *RedisClient) checkServerInstance(name, idc string) {
	for {
		time.Sleep(DF_BNS_UPDATE_INTERVAL)

		// check addresses of redis servers
		servers, err := bns.GetAddr(c.bnsClient, name, idc)
		if err != nil {
			c.state.Inc(REDIS_GET_BNS_INSTANCE_ERR, 1)
			continue
		}
		if len(servers) == 0 {
			c.state.Inc(REDIS_NO_BNS_INSTANCE, 1)
			continue
		}
		if reflect.DeepEqual(servers, c.Servers) {
			continue
		}

		// update addresses of redis servers
		c.UpdateServers(servers)

		// update connection pool
		pool := &redis.Pool{
			MaxIdle: c.MaxIdle,
			Dial:    c.dial,
		}
		oldPool := c.UpdatePool(pool)
		oldPool.Close()
	}
}
コード例 #3
0
ファイル: bns.go プロジェクト: qiangmzsx/golang
func main() {
	bnsName := "group.mmsda.MMS.all:proxy"
	bnsClient := bns.NewClient()
	Servers, err := bns.GetAddr(bnsClient, bnsName, "tc")
	if err != nil {

	}
	fmt.Println(Servers)

}
コード例 #4
0
ファイル: util.go プロジェクト: qiangmzsx/golang
func GetMMSDAServer() []string {
	mmsdaIdc := beego.AppConfig.DefaultString("mmsdaIdc", "tc")
	bnsIP := MemCache.Get(MMSDAConf)
	HostAdd, ok := bnsIP.([]string)
	beego.Notice("MemCache.Get", len(HostAdd))
	if !ok || len(HostAdd) == 0 {
		bnsClient := bns.NewClient()
		bnsName := beego.AppConfig.DefaultString(MMSDAConf, "group.mmsda.MMS.all:proxy")
		HostAdd, _ = bns.GetAddr(bnsClient, bnsName, mmsdaIdc)
		MemCache.Put(MMSDAConf, HostAdd, 120*time.Second)
		beego.Notice("BNS.Get", len(HostAdd))
	}
	return HostAdd
}