Skip to content

Commit

Permalink
Pass network and addr to dialer
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Jun 4, 2019
1 parent 90febb9 commit 3da4357
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
3 changes: 3 additions & 0 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type ClusterOptions struct {

// Following options are copied from Options struct.

Dialer func(network, addr string) (net.Conn, error)

OnConnect func(*Conn) error

Password string
Expand Down Expand Up @@ -122,6 +124,7 @@ func (opt *ClusterOptions) clientOptions() *Options {
const disableIdleCheck = -1

return &Options{
Dialer: opt.Dialer,
OnConnect: opt.OnConnect,

MaxRetries: opt.MaxRetries,
Expand Down
10 changes: 6 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Options struct {

// Dialer creates new network connection and has priority over
// Network and Addr options.
Dialer func() (net.Conn, error)
Dialer func(network, addr string) (net.Conn, error)

// Hook that is called when new connection is established.
OnConnect func(*Conn) error
Expand Down Expand Up @@ -105,13 +105,13 @@ func (opt *Options) init() {
opt.Addr = "localhost:6379"
}
if opt.Dialer == nil {
opt.Dialer = func() (net.Conn, error) {
opt.Dialer = func(network, addr string) (net.Conn, error) {
netDialer := &net.Dialer{
Timeout: opt.DialTimeout,
KeepAlive: 5 * time.Minute,
}
if opt.TLSConfig == nil {
return netDialer.Dial(opt.Network, opt.Addr)
return netDialer.Dial(network, addr)
} else {
return tls.DialWithDialer(netDialer, opt.Network, opt.Addr, opt.TLSConfig)
}
Expand Down Expand Up @@ -215,7 +215,9 @@ func ParseURL(redisURL string) (*Options, error) {

func newConnPool(opt *Options) *pool.ConnPool {
return pool.NewConnPool(&pool.Options{
Dialer: opt.Dialer,
Dialer: func() (net.Conn, error) {
return opt.Dialer(opt.Network, opt.Addr)
},
PoolSize: opt.PoolSize,
MinIdleConns: opt.MinIdleConns,
MaxConnAge: opt.MaxConnAge,
Expand Down
7 changes: 4 additions & 3 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ var _ = Describe("Client", func() {

It("should support custom dialers", func() {
custom := redis.NewClient(&redis.Options{
Addr: ":1234",
Dialer: func() (net.Conn, error) {
return net.Dial("tcp", redisAddr)
Network: "tcp",
Addr: redisAddr,
Dialer: func(network, addr string) (net.Conn, error) {
return net.Dial(network, addr)
},
})

Expand Down
7 changes: 4 additions & 3 deletions sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type FailoverOptions struct {

// Following options are copied from Options struct.

Dialer func(network, addr string) (net.Conn, error)
OnConnect func(*Conn) error

Password string
Expand All @@ -50,8 +51,8 @@ type FailoverOptions struct {

func (opt *FailoverOptions) options() *Options {
return &Options{
Addr: "FailoverClient",

Addr: "FailoverClient",
Dialer: opt.Dialer,
OnConnect: opt.OnConnect,

DB: opt.DB,
Expand Down Expand Up @@ -304,7 +305,7 @@ func (c *sentinelFailover) Pool() *pool.ConnPool {
return c.pool
}

func (c *sentinelFailover) dial() (net.Conn, error) {
func (c *sentinelFailover) dial(network, addr string) (net.Conn, error) {
addr, err := c.MasterAddr()
if err != nil {
return nil, err
Expand Down
8 changes: 7 additions & 1 deletion universal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package redis
import (
"context"
"crypto/tls"
"net"
"time"
)

Expand All @@ -19,6 +20,7 @@ type UniversalOptions struct {

// Common options.

Dialer func(network, addr string) (net.Conn, error)
OnConnect func(*Conn) error
Password string
MaxRetries int
Expand Down Expand Up @@ -54,6 +56,7 @@ func (o *UniversalOptions) cluster() *ClusterOptions {

return &ClusterOptions{
Addrs: o.Addrs,
Dialer: o.Dialer,
OnConnect: o.OnConnect,

Password: o.Password,
Expand Down Expand Up @@ -89,7 +92,9 @@ func (o *UniversalOptions) failover() *FailoverOptions {
return &FailoverOptions{
SentinelAddrs: o.Addrs,
MasterName: o.MasterName,
OnConnect: o.OnConnect,

Dialer: o.Dialer,
OnConnect: o.OnConnect,

DB: o.DB,
Password: o.Password,
Expand Down Expand Up @@ -121,6 +126,7 @@ func (o *UniversalOptions) simple() *Options {

return &Options{
Addr: addr,
Dialer: o.Dialer,
OnConnect: o.OnConnect,

DB: o.DB,
Expand Down

0 comments on commit 3da4357

Please sign in to comment.