Merge pull request #1997 from nkcmr/feature/nick/default-dialer

feat: extract dialer to `DefaultDialer` to allow wrapping
This commit is contained in:
Vladimir Mihailenco 2022-10-06 10:23:38 +03:00 committed by GitHub
commit a9287a16d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 10 deletions

View File

@ -133,16 +133,7 @@ func (opt *Options) init() {
opt.DialTimeout = 5 * time.Second opt.DialTimeout = 5 * time.Second
} }
if opt.Dialer == nil { if opt.Dialer == nil {
opt.Dialer = func(ctx context.Context, network, addr string) (net.Conn, error) { opt.Dialer = DefaultDialer(opt)
netDialer := &net.Dialer{
Timeout: opt.DialTimeout,
KeepAlive: 5 * time.Minute,
}
if opt.TLSConfig == nil {
return netDialer.DialContext(ctx, network, addr)
}
return tls.DialWithDialer(netDialer, network, addr, opt.TLSConfig)
}
} }
if opt.PoolSize == 0 { if opt.PoolSize == 0 {
opt.PoolSize = 10 * runtime.GOMAXPROCS(0) opt.PoolSize = 10 * runtime.GOMAXPROCS(0)
@ -198,6 +189,21 @@ func (opt *Options) clone() *Options {
return &clone return &clone
} }
// DefaultDialer returns a function that will be used as the default dialer
// when none is specified in Options.Dialer.
func DefaultDialer(opt *Options) func(context.Context, string, string) (net.Conn, error) {
return func(ctx context.Context, network, addr string) (net.Conn, error) {
netDialer := &net.Dialer{
Timeout: opt.DialTimeout,
KeepAlive: 5 * time.Minute,
}
if opt.TLSConfig == nil {
return netDialer.DialContext(ctx, network, addr)
}
return tls.DialWithDialer(netDialer, network, addr, opt.TLSConfig)
}
}
// ParseURL parses an URL into Options that can be used to connect to Redis. // ParseURL parses an URL into Options that can be used to connect to Redis.
// Scheme is required. // Scheme is required.
// There are two connection types: by tcp socket and by unix socket. // There are two connection types: by tcp socket and by unix socket.