Skip to content

Commit a9287a1

Browse files
authored
Merge pull request #1997 from nkcmr/feature/nick/default-dialer
feat: extract dialer to `DefaultDialer` to allow wrapping
2 parents b0231c6 + 52276c3 commit a9287a1

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

options.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,7 @@ func (opt *Options) init() {
133133
opt.DialTimeout = 5 * time.Second
134134
}
135135
if opt.Dialer == nil {
136-
opt.Dialer = func(ctx context.Context, network, addr string) (net.Conn, error) {
137-
netDialer := &net.Dialer{
138-
Timeout: opt.DialTimeout,
139-
KeepAlive: 5 * time.Minute,
140-
}
141-
if opt.TLSConfig == nil {
142-
return netDialer.DialContext(ctx, network, addr)
143-
}
144-
return tls.DialWithDialer(netDialer, network, addr, opt.TLSConfig)
145-
}
136+
opt.Dialer = DefaultDialer(opt)
146137
}
147138
if opt.PoolSize == 0 {
148139
opt.PoolSize = 10 * runtime.GOMAXPROCS(0)
@@ -198,6 +189,21 @@ func (opt *Options) clone() *Options {
198189
return &clone
199190
}
200191

192+
// DefaultDialer returns a function that will be used as the default dialer
193+
// when none is specified in Options.Dialer.
194+
func DefaultDialer(opt *Options) func(context.Context, string, string) (net.Conn, error) {
195+
return func(ctx context.Context, network, addr string) (net.Conn, error) {
196+
netDialer := &net.Dialer{
197+
Timeout: opt.DialTimeout,
198+
KeepAlive: 5 * time.Minute,
199+
}
200+
if opt.TLSConfig == nil {
201+
return netDialer.DialContext(ctx, network, addr)
202+
}
203+
return tls.DialWithDialer(netDialer, network, addr, opt.TLSConfig)
204+
}
205+
}
206+
201207
// ParseURL parses an URL into Options that can be used to connect to Redis.
202208
// Scheme is required.
203209
// There are two connection types: by tcp socket and by unix socket.

0 commit comments

Comments
 (0)