Пример #1
0
func newRedisConnectionPool(server, password string) *redis.Pool {
	return &redis.Pool{
		MaxIdle:     3,
		IdleTimeout: 240 * time.Second,
		Dial: func() (redis.Conn, error) {
			dialer := &net.Dialer{Timeout: 3 * time.Second}
			c, err := redis.Dial("tcp",
				server,
				redis.DialNetDial(MakeRetryDialer(dialer, 3)))
			if err != nil {
				return nil, err
			}
			if password != "" {
				if _, err := c.Do("AUTH", password); err != nil {
					c.Close()
					return nil, err
				}
			}
			return c, err
		},
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			_, err := c.Do("PING")
			return err
		},
	}
}
Пример #2
0
func TestDialURLHost(t *testing.T) {
	checkHost := func(network, address string) (net.Conn, error) {
		if address != "localhost:6379" {
			t.Errorf("DialURL did not set host to localhost by default (got %v)", address)
		}
		return nil, nil
	}
	_, err := redis.DialURL("redis://:6379", redis.DialNetDial(checkHost))
	if err != nil {
		t.Error("dial error:", err)
	}
}
Пример #3
0
func (t *topology) PublishIPs(name string, localAddrs []string) error {
	if t.cfg.host == "" {
		debugf("Not publishing IPs because, no host configured")
	}

	dialOpts := []redis.DialOption{
		redis.DialPassword(t.cfg.password),
		redis.DialDatabase(t.cfg.db),
	}
	if t.transCfg != nil {
		d, err := transport.MakeDialer(t.transCfg)
		if err != nil {
			return err
		}
		dialOpts = append(dialOpts, redis.DialNetDial(d.Dial))
	}

	conn, err := redis.Dial("tcp", t.cfg.host, dialOpts...)
	if err != nil {
		return err
	}
	defer conn.Close()

	_, err = conn.Do("HSET", name, "ipaddrs", strings.Join(localAddrs, ","))
	if err != nil {
		logp.Err("[%s] Fail to set the IP addresses: %s", name, err)
		return err
	}

	_, err = conn.Do("EXPIRE", name, int(t.cfg.expire.Seconds()))
	if err != nil {
		logp.Err("[%s] Fail to set the expiration time: %s", name, err)
		return err
	}

	t.updateMap(conn)
	return nil
}
Пример #4
0
func TestDialURL(t *testing.T) {
	for _, d := range dialErrors {
		_, err := redis.DialURL(d.rawurl)
		if err == nil || !strings.Contains(err.Error(), d.expectedError) {
			t.Errorf("DialURL did not return expected error (expected %v to contain %s)", err, d.expectedError)
		}
	}

	checkPort := func(network, address string) (net.Conn, error) {
		if address != "localhost:6379" {
			t.Errorf("DialURL did not set port to 6379 by default (got %v)", address)
		}
		return net.Dial(network, address)
	}
	c, err := redis.DialURL("redis://localhost", redis.DialNetDial(checkPort))
	if err != nil {
		t.Error("dial error:", err)
	}
	c.Close()

	checkHost := func(network, address string) (net.Conn, error) {
		if address != "localhost:6379" {
			t.Errorf("DialURL did not set host to localhost by default (got %v)", address)
		}
		return net.Dial(network, address)
	}
	c, err = redis.DialURL("redis://:6379", redis.DialNetDial(checkHost))
	if err != nil {
		t.Error("dial error:", err)
	}
	c.Close()

	// Check that the database is set correctly
	c1, err := redis.DialURL("redis://:6379/8")
	defer c1.Close()
	if err != nil {
		t.Error("Dial error:", err)
	}
	dbSize, _ := redis.Int(c1.Do("DBSIZE"))
	if dbSize > 0 {
		t.Fatal("DB 8 has existing keys; aborting test to avoid overwriting data")
	}
	c1.Do("SET", "var", "val")

	c2, err := redis.Dial("tcp", ":6379")
	defer c2.Close()
	if err != nil {
		t.Error("dial error:", err)
	}
	_, err = c2.Do("SELECT", "8")
	if err != nil {
		t.Error(err)
	}
	got, err := redis.String(c2.Do("GET", "var"))
	if err != nil {
		t.Error(err)
	}
	if got != "val" {
		t.Error("DialURL did not correctly set the db.")
	}
	_, err = c2.Do("DEL", "var")
}
Пример #5
0
func dialTestConn(r io.Reader, w io.Writer) redis.DialOption {
	return redis.DialNetDial(func(net, addr string) (net.Conn, error) {
		return &testConn{Reader: r, Writer: w}, nil
	})
}