예제 #1
0
파일: db_sentinel.go 프로젝트: mc0/okq
func newSentinelDB() (DBer, error) {
	clients := make([]*sentinel.Client, len(config.RedisSentinels))
	for i, server := range config.RedisSentinels {
		log.L.Printf("connecting to redis sentinel at %s", server)
		c, err := sentinel.NewClient("tcp", server, config.RedisPoolSize, config.RedisSentinelGroup)
		if err != nil {
			log.L.Fatal(err)
		}
		clients[i] = c
	}

	return &sentinelDB{clients}, nil
}
예제 #2
0
func (t *Transport) Connect() error {

	// TODO add retries in case of failures

	if t.isSentinel() {

		client, err := sentinel.NewClient("tcp", t.Address, t.MaxConnections, t.SentinelMaster)
		if err != nil {
			msg := fmt.Sprintf("Cannot connect to Redis host '%s': %s", t.Address, err)
			log.Errorln(msg)
			return err
		}
		t.sentinelClient = client

	} else { // Redis standalone

		// create redis pool
		pool, err := pool.New("tcp", t.Address, t.MaxConnections)
		if err != nil {
			msg := fmt.Sprintf("Cannot connect to Redis host '%s': %s", t.Address, err)
			log.Errorln(msg)
			return err
		}
		t.pool = pool
	}

	// ping to ensure we are really connected
	conn, err := t.getConnection()
	defer conn.Close()

	if err != nil {
		log.Fatalf("Cannot ping to Redis host: %s", err.Error())
		return err
	}
	t.pingRedis(conn)

	return nil
}