// NewMySQLStorage initialize mysql pool and consistency hash ring. func NewMySQLStorage() *MySQLStorage { var ( err error w int nw []string db *sql.DB ) dbPool := make(map[string]*sql.DB) ring := ketama.NewRing(Conf.MySQLKetamaBase) for n, source := range Conf.MySQLSource { nw = strings.Split(n, ":") if len(nw) != 2 { err = errors.New("node config error, it's nodeN:W") logger.Errorf("strings.Split(\"%s\", :) failed (%v)", n, err) panic(err) } w, err = strconv.Atoi(nw[1]) if err != nil { logger.Errorf("strconv.Atoi(\"%s\") failed (%v)", nw[1], err) panic(err) } db, err = sql.Open("mysql", source) if err != nil { logger.Errorf("sql.Open(\"mysql\", %s) failed (%v)", source, err) panic(err) } dbPool[nw[0]] = db ring.AddNode(nw[0], w) } ring.Bake() s := &MySQLStorage{pool: dbPool, ring: ring} go s.clean() return s }
// initRedisStorage initialize the redis pool and consistency hash ring. func InitRedisStorage() { logger.Info("Connecting Redis....") var ( err error w int nw []string ) redisPool := map[string]*redis.Pool{} ring := ketama.NewRing(Conf.RedisKetamaBase) for n, addr := range Conf.RedisSource { nw = strings.Split(n, ":") if len(nw) != 2 { err = errors.New("node config error, it's nodeN:W") logger.Errorf("strings.Split(\"%s\", :) failed (%v)", n, err) panic(err) } w, err = strconv.Atoi(nw[1]) if err != nil { logger.Errorf("strconv.Atoi(\"%s\") failed (%v)", nw[1], err) panic(err) } tmp := addr redisPool[nw[0]] = &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 { logger.Errorf("redis.Dial(\"tcp\", \"%s\") error(%v)", tmp, err) return nil, err } return conn, err }, } ring.AddNode(nw[0], w) } ring.Bake() rs = &redisStorage{pool: redisPool, ring: ring} logger.Info("Redis connected") }
// NewRedis initialize the redis pool and consistency hash ring. func NewRedisStorage() *RedisStorage { var ( err error w int nw []string ) redisPool := map[string]*redis.Pool{} ring := ketama.NewRing(Conf.RedisKetamaBase) for n, addr := range Conf.RedisSource { nw = strings.Split(n, ":") if len(nw) != 2 { err = errors.New("node config error, it's nodeN:W") logger.Errorf("strings.Split(\"%s\", :) failed (%v)", n, err) panic(err) } w, err = strconv.Atoi(nw[1]) if err != nil { logger.Errorf("strconv.Atoi(\"%s\") failed (%v)", nw[1], err) panic(err) } tmp := addr // WARN: closures use redisPool[nw[0]] = &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 { logger.Errorf("redis.Dial(\"tcp\", \"%s\") error(%v)", tmp, err) return nil, err } return conn, err }, } ring.AddNode(nw[0], w) } ring.Bake() s := &RedisStorage{pool: redisPool, ring: ring, delCH: make(chan *RedisDelMessage, 10240)} go s.clean() return s }