Ejemplo n.º 1
0
// NewMySQLStorage initialize mysql pool and consistency hash ring.
func NewMySQLStorage() *MySQLStorage {
	dbPool := make(map[string]*sql.DB)
	for n, source := range Conf.MySQLSource {
		db, err := sql.Open("mysql", source)
		if err != nil {
			glog.Errorf("sql.Open(\"mysql\", %s) failed (%v)", source, err)
			panic(err)
		}
		dbPool[n] = db
	}
	s := &MySQLStorage{pool: dbPool, ketama: hash.NewKetama(len(dbPool), 255)}
	go s.clean()
	return s
}
Ejemplo n.º 2
0
// Initialize mysql pool, Initialize consistency hash ring
func NewMYSQL() *MYSQLStorage {
	dbPool := make(map[string]*sql.DB)
	for n, source := range Conf.DBSource {
		db, err := sql.Open("mysql", source)
		if err != nil {
			Log.Error("sql.Open(\"mysql\", %s) failed (%v)", source, err)
			panic(err)
		}

		dbPool[n] = db
	}

	return &MYSQLStorage{Pool: dbPool, Ketama: hash.NewKetama(len(dbPool), 255)}
}
Ejemplo n.º 3
0
// Initialize redis pool, Initialize consistency hash ring
func InitRedis() {
	// Redis pool
	for n, addr := range Conf.RedisAddrs {
		tmp := addr
		// WARN: closures use
		redisPool[n] = &redis.Pool{
			MaxIdle:     Conf.RedisMaxIdle,
			MaxActive:   Conf.RedisMaxActive,
			IdleTimeout: Conf.RedisIdleTimeout,
			Dial: func() (redis.Conn, error) {
				conn, err := redis.Dial("tcp", tmp)
				if err != nil {
					Log.Error("redis.Dial(\"tcp\", \"%s\") error(%v)", tmp, err)
				}
				return conn, err
			},
		}
	}

	// Consistent hashing
	redisHash = hash.NewKetama(len(redisPool), 255)
}
Ejemplo n.º 4
0
// Initialize redis pool, Initialize consistent hash ring
func InitRedis() {
	// Redis pool
	for n, c := range Conf.Redis {
		// WARN: closures use
		tc := c
		redisPool[n] = &redis.Pool{
			MaxIdle:     tc.MaxIdle,
			MaxActive:   tc.MaxActive,
			IdleTimeout: time.Duration(tc.IdleTimeout) * time.Second,
			Dial: func() (redis.Conn, error) {
				conn, err := redis.Dial(tc.Network, tc.Addr)
				if err != nil {
					Log.Error("redis.Dial(\"%s\", \"%s\") failed (%v)", tc.Network, tc.Addr, err)
				}
				return conn, err
			},
		}
	}

	// Consistent hashing
	redisHash = hash.NewKetama(len(redisPool), 255)
}
Ejemplo n.º 5
0
// Initialize redis pool, Initialize consistency hash ring
func NewRedis() *RedisStorage {
	redisPool := map[string]*redis.Pool{}
	// Redis pool
	for n, addr := range Conf.RedisAddrs {
		tmp := addr
		// WARN: closures use
		redisPool[n] = &redis.Pool{
			MaxIdle:     Conf.RedisMaxIdle,
			MaxActive:   Conf.RedisMaxActive,
			IdleTimeout: Conf.RedisIdleTimeout,
			Dial: func() (redis.Conn, error) {
				conn, err := redis.Dial("tcp", tmp)
				if err != nil {
					Log.Error("redis.Dial(\"tcp\", \"%s\") error(%v)", tmp, err)
				}
				return conn, err
			},
		}
	}

	return &RedisStorage{Pool: redisPool, Ketama: hash.NewKetama(len(redisPool), 255)}
}
Ejemplo n.º 6
0
// NewRedis initialize the redis pool and consistency hash ring.
func NewRedisStorage() *RedisStorage {
	redisPool := map[string]*redis.Pool{}
	for n, addr := range Conf.RedisSource {
		tmp := addr
		// WARN: closures use
		redisPool[n] = &redis.Pool{
			MaxIdle:     Conf.RedisMaxIdle,
			MaxActive:   Conf.RedisMaxActive,
			IdleTimeout: Conf.RedisIdleTimeout,
			Dial: func() (redis.Conn, error) {
				conn, err := redis.Dial("tcp", tmp)
				if err != nil {
					glog.Errorf("redis.Dial(\"tcp\", \"%s\") error(%v)", tmp, err)
					return nil, err
				}
				return conn, err
			},
		}
	}
	s := &RedisStorage{pool: redisPool, ketama: hash.NewKetama(len(redisPool), 255), delCH: make(chan *RedisDelMessage, 10240)}
	go s.clean()
	return s
}