Skip to content

Commit b573f55

Browse files
authored
Merge pull request #1028 from go-redis/fix/dialer-net-addr
Pass network and addr to dialer
2 parents 6ccc9b8 + 59735f3 commit b573f55

File tree

6 files changed

+26
-11
lines changed

6 files changed

+26
-11
lines changed

cluster.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type ClusterOptions struct {
5353

5454
// Following options are copied from Options struct.
5555

56+
Dialer func(network, addr string) (net.Conn, error)
57+
5658
OnConnect func(*Conn) error
5759

5860
Password string
@@ -122,6 +124,7 @@ func (opt *ClusterOptions) clientOptions() *Options {
122124
const disableIdleCheck = -1
123125

124126
return &Options{
127+
Dialer: opt.Dialer,
125128
OnConnect: opt.OnConnect,
126129

127130
MaxRetries: opt.MaxRetries,

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ require (
44
github.com/onsi/ginkgo v1.8.0
55
github.com/onsi/gomega v1.5.0
66
)
7+
8+
go 1.13

options.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Options struct {
3434

3535
// Dialer creates new network connection and has priority over
3636
// Network and Addr options.
37-
Dialer func() (net.Conn, error)
37+
Dialer func(network, addr string) (net.Conn, error)
3838

3939
// Hook that is called when new connection is established.
4040
OnConnect func(*Conn) error
@@ -105,13 +105,13 @@ func (opt *Options) init() {
105105
opt.Addr = "localhost:6379"
106106
}
107107
if opt.Dialer == nil {
108-
opt.Dialer = func() (net.Conn, error) {
108+
opt.Dialer = func(network, addr string) (net.Conn, error) {
109109
netDialer := &net.Dialer{
110110
Timeout: opt.DialTimeout,
111111
KeepAlive: 5 * time.Minute,
112112
}
113113
if opt.TLSConfig == nil {
114-
return netDialer.Dial(opt.Network, opt.Addr)
114+
return netDialer.Dial(network, addr)
115115
} else {
116116
return tls.DialWithDialer(netDialer, opt.Network, opt.Addr, opt.TLSConfig)
117117
}
@@ -215,7 +215,9 @@ func ParseURL(redisURL string) (*Options, error) {
215215

216216
func newConnPool(opt *Options) *pool.ConnPool {
217217
return pool.NewConnPool(&pool.Options{
218-
Dialer: opt.Dialer,
218+
Dialer: func() (net.Conn, error) {
219+
return opt.Dialer(opt.Network, opt.Addr)
220+
},
219221
PoolSize: opt.PoolSize,
220222
MinIdleConns: opt.MinIdleConns,
221223
MaxConnAge: opt.MaxConnAge,

redis_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ var _ = Describe("Client", func() {
3939

4040
It("should support custom dialers", func() {
4141
custom := redis.NewClient(&redis.Options{
42-
Addr: ":1234",
43-
Dialer: func() (net.Conn, error) {
44-
return net.Dial("tcp", redisAddr)
42+
Network: "tcp",
43+
Addr: redisAddr,
44+
Dialer: func(network, addr string) (net.Conn, error) {
45+
return net.Dial(network, addr)
4546
},
4647
})
4748

sentinel.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type FailoverOptions struct {
2424

2525
// Following options are copied from Options struct.
2626

27+
Dialer func(network, addr string) (net.Conn, error)
2728
OnConnect func(*Conn) error
2829

2930
Password string
@@ -49,8 +50,8 @@ type FailoverOptions struct {
4950

5051
func (opt *FailoverOptions) options() *Options {
5152
return &Options{
52-
Addr: "FailoverClient",
53-
53+
Addr: "FailoverClient",
54+
Dialer: opt.Dialer,
5455
OnConnect: opt.OnConnect,
5556

5657
DB: opt.DB,
@@ -227,7 +228,7 @@ func (c *sentinelFailover) Pool() *pool.ConnPool {
227228
return c.pool
228229
}
229230

230-
func (c *sentinelFailover) dial() (net.Conn, error) {
231+
func (c *sentinelFailover) dial(network, addr string) (net.Conn, error) {
231232
addr, err := c.MasterAddr()
232233
if err != nil {
233234
return nil, err

universal.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package redis
22

33
import (
44
"crypto/tls"
5+
"net"
56
"time"
67
)
78

@@ -18,6 +19,7 @@ type UniversalOptions struct {
1819

1920
// Common options.
2021

22+
Dialer func(network, addr string) (net.Conn, error)
2123
OnConnect func(*Conn) error
2224
Password string
2325
MaxRetries int
@@ -53,6 +55,7 @@ func (o *UniversalOptions) cluster() *ClusterOptions {
5355

5456
return &ClusterOptions{
5557
Addrs: o.Addrs,
58+
Dialer: o.Dialer,
5659
OnConnect: o.OnConnect,
5760

5861
Password: o.Password,
@@ -88,7 +91,9 @@ func (o *UniversalOptions) failover() *FailoverOptions {
8891
return &FailoverOptions{
8992
SentinelAddrs: o.Addrs,
9093
MasterName: o.MasterName,
91-
OnConnect: o.OnConnect,
94+
95+
Dialer: o.Dialer,
96+
OnConnect: o.OnConnect,
9297

9398
DB: o.DB,
9499
Password: o.Password,
@@ -120,6 +125,7 @@ func (o *UniversalOptions) simple() *Options {
120125

121126
return &Options{
122127
Addr: addr,
128+
Dialer: o.Dialer,
123129
OnConnect: o.OnConnect,
124130

125131
DB: o.DB,

0 commit comments

Comments
 (0)