Example #1
0
// Init redis channel, such as init redis pool, init consistent hash ring
func InitRedisChannel() error {
	if Conf.Redis == nil || len(Conf.Redis) == 0 {
		return ConfigRedisErr
	}

	// redis pool
	for n, c := range Conf.Redis {
		redisPool[n] = &redis.Pool{
			MaxIdle:     c.Pool,
			IdleTimeout: time.Duration(c.Timeout) * time.Second,
			Dial: func() (redis.Conn, error) {
				conn, err := redis.Dial(c.Network, c.Addr)
				if err != nil {
					Log.Printf("redis.Dial(\"%s\", \"%s\") failed (%s)", c.Network, c.Addr, err.Error())
				}
				return conn, err
			},
		}
	}

	// consistent hashing
	redisHash = hash.NewKetama(len(redisPool), 255)

	return nil
}
Example #2
0
// Init redis channel, such as init redis pool, init consistent hash ring
func InitRedisChannel() error {
	if Conf.Redis == nil || len(Conf.Redis) == 0 {
		LogError(LogLevelWarn, "not configure redis node in config file")
		return ConfigRedisErr
	}

	// redis pool
	for n, c := range Conf.Redis {
		// WARN: closures use
		tc := c
		redisPool[n] = &redis.Pool{
			MaxIdle:     tc.Idle,
			MaxActive:   tc.Active,
			IdleTimeout: time.Duration(tc.Timeout) * time.Second,
			Dial: func() (redis.Conn, error) {
				conn, err := redis.Dial(tc.Network, tc.Addr)
				if err != nil {
					LogError(LogLevelErr, "redis.Dial(\"%s\", \"%s\") failed (%s)", tc.Network, tc.Addr, err.Error())
				}
				return conn, err
			},
		}
	}

	// consistent hashing
	redisHash = hash.NewKetama(len(redisPool), 255)
	return nil
}