mirror of https://github.com/go-redis/redis.git
Add Dialer option.
This commit is contained in:
parent
593d5d1d77
commit
2508d0a57e
25
redis.go
25
redis.go
|
@ -152,7 +152,13 @@ type options struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Addr string
|
Network string
|
||||||
|
Addr string
|
||||||
|
|
||||||
|
// Dialer creates new network connection and has priority over
|
||||||
|
// Network and Addr options.
|
||||||
|
Dialer func() (net.Conn, error)
|
||||||
|
|
||||||
Password string
|
Password string
|
||||||
DB int64
|
DB int64
|
||||||
|
|
||||||
|
@ -196,10 +202,13 @@ type Client struct {
|
||||||
*baseClient
|
*baseClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func newClient(clOpt *Options, network string) *Client {
|
func NewClient(clOpt *Options) *Client {
|
||||||
opt := clOpt.options()
|
opt := clOpt.options()
|
||||||
dialer := func() (net.Conn, error) {
|
dialer := clOpt.Dialer
|
||||||
return net.DialTimeout(network, clOpt.Addr, opt.DialTimeout)
|
if dialer == nil {
|
||||||
|
dialer = func() (net.Conn, error) {
|
||||||
|
return net.DialTimeout(clOpt.Network, clOpt.Addr, opt.DialTimeout)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return &Client{
|
return &Client{
|
||||||
baseClient: &baseClient{
|
baseClient: &baseClient{
|
||||||
|
@ -209,10 +218,14 @@ func newClient(clOpt *Options, network string) *Client {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated. Use NewClient instead.
|
||||||
func NewTCPClient(opt *Options) *Client {
|
func NewTCPClient(opt *Options) *Client {
|
||||||
return newClient(opt, "tcp")
|
opt.Network = "tcp"
|
||||||
|
return NewClient(opt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated. Use NewClient instead.
|
||||||
func NewUnixClient(opt *Options) *Client {
|
func NewUnixClient(opt *Options) *Client {
|
||||||
return newClient(opt, "unix")
|
opt.Network = "unix"
|
||||||
|
return NewClient(opt)
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,6 +69,18 @@ func (t *RedisConnectorTest) TestNewUnixClient(c *C) {
|
||||||
c.Assert(client.Close(), IsNil)
|
c.Assert(client.Close(), IsNil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *RedisConnectorTest) TestDialer(c *C) {
|
||||||
|
client := redis.NewClient(&redis.Options{
|
||||||
|
Dialer: func() (net.Conn, error) {
|
||||||
|
return net.Dial("tcp", redisAddr)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
ping := client.Ping()
|
||||||
|
c.Check(ping.Err(), IsNil)
|
||||||
|
c.Check(ping.Val(), Equals, "PONG")
|
||||||
|
c.Assert(client.Close(), IsNil)
|
||||||
|
}
|
||||||
|
|
||||||
func (t *RedisConnectorTest) TestClose(c *C) {
|
func (t *RedisConnectorTest) TestClose(c *C) {
|
||||||
client := redis.NewTCPClient(&redis.Options{
|
client := redis.NewTCPClient(&redis.Options{
|
||||||
Addr: redisAddr,
|
Addr: redisAddr,
|
||||||
|
|
Loading…
Reference in New Issue