forked from mirror/redis
Pass network and addr to dialer
This commit is contained in:
parent
90febb9820
commit
3da4357c0c
|
@ -53,6 +53,8 @@ type ClusterOptions struct {
|
||||||
|
|
||||||
// Following options are copied from Options struct.
|
// Following options are copied from Options struct.
|
||||||
|
|
||||||
|
Dialer func(network, addr string) (net.Conn, error)
|
||||||
|
|
||||||
OnConnect func(*Conn) error
|
OnConnect func(*Conn) error
|
||||||
|
|
||||||
Password string
|
Password string
|
||||||
|
@ -122,6 +124,7 @@ func (opt *ClusterOptions) clientOptions() *Options {
|
||||||
const disableIdleCheck = -1
|
const disableIdleCheck = -1
|
||||||
|
|
||||||
return &Options{
|
return &Options{
|
||||||
|
Dialer: opt.Dialer,
|
||||||
OnConnect: opt.OnConnect,
|
OnConnect: opt.OnConnect,
|
||||||
|
|
||||||
MaxRetries: opt.MaxRetries,
|
MaxRetries: opt.MaxRetries,
|
||||||
|
|
10
options.go
10
options.go
|
@ -34,7 +34,7 @@ type Options struct {
|
||||||
|
|
||||||
// Dialer creates new network connection and has priority over
|
// Dialer creates new network connection and has priority over
|
||||||
// Network and Addr options.
|
// 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.
|
// Hook that is called when new connection is established.
|
||||||
OnConnect func(*Conn) error
|
OnConnect func(*Conn) error
|
||||||
|
@ -105,13 +105,13 @@ func (opt *Options) init() {
|
||||||
opt.Addr = "localhost:6379"
|
opt.Addr = "localhost:6379"
|
||||||
}
|
}
|
||||||
if opt.Dialer == nil {
|
if opt.Dialer == nil {
|
||||||
opt.Dialer = func() (net.Conn, error) {
|
opt.Dialer = func(network, addr string) (net.Conn, error) {
|
||||||
netDialer := &net.Dialer{
|
netDialer := &net.Dialer{
|
||||||
Timeout: opt.DialTimeout,
|
Timeout: opt.DialTimeout,
|
||||||
KeepAlive: 5 * time.Minute,
|
KeepAlive: 5 * time.Minute,
|
||||||
}
|
}
|
||||||
if opt.TLSConfig == nil {
|
if opt.TLSConfig == nil {
|
||||||
return netDialer.Dial(opt.Network, opt.Addr)
|
return netDialer.Dial(network, addr)
|
||||||
} else {
|
} else {
|
||||||
return tls.DialWithDialer(netDialer, opt.Network, opt.Addr, opt.TLSConfig)
|
return tls.DialWithDialer(netDialer, opt.Network, opt.Addr, opt.TLSConfig)
|
||||||
}
|
}
|
||||||
|
@ -215,7 +215,9 @@ func ParseURL(redisURL string) (*Options, error) {
|
||||||
|
|
||||||
func newConnPool(opt *Options) *pool.ConnPool {
|
func newConnPool(opt *Options) *pool.ConnPool {
|
||||||
return pool.NewConnPool(&pool.Options{
|
return pool.NewConnPool(&pool.Options{
|
||||||
Dialer: opt.Dialer,
|
Dialer: func() (net.Conn, error) {
|
||||||
|
return opt.Dialer(opt.Network, opt.Addr)
|
||||||
|
},
|
||||||
PoolSize: opt.PoolSize,
|
PoolSize: opt.PoolSize,
|
||||||
MinIdleConns: opt.MinIdleConns,
|
MinIdleConns: opt.MinIdleConns,
|
||||||
MaxConnAge: opt.MaxConnAge,
|
MaxConnAge: opt.MaxConnAge,
|
||||||
|
|
|
@ -39,9 +39,10 @@ var _ = Describe("Client", func() {
|
||||||
|
|
||||||
It("should support custom dialers", func() {
|
It("should support custom dialers", func() {
|
||||||
custom := redis.NewClient(&redis.Options{
|
custom := redis.NewClient(&redis.Options{
|
||||||
Addr: ":1234",
|
Network: "tcp",
|
||||||
Dialer: func() (net.Conn, error) {
|
Addr: redisAddr,
|
||||||
return net.Dial("tcp", redisAddr)
|
Dialer: func(network, addr string) (net.Conn, error) {
|
||||||
|
return net.Dial(network, addr)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ type FailoverOptions struct {
|
||||||
|
|
||||||
// Following options are copied from Options struct.
|
// Following options are copied from Options struct.
|
||||||
|
|
||||||
|
Dialer func(network, addr string) (net.Conn, error)
|
||||||
OnConnect func(*Conn) error
|
OnConnect func(*Conn) error
|
||||||
|
|
||||||
Password string
|
Password string
|
||||||
|
@ -51,7 +52,7 @@ type FailoverOptions struct {
|
||||||
func (opt *FailoverOptions) options() *Options {
|
func (opt *FailoverOptions) options() *Options {
|
||||||
return &Options{
|
return &Options{
|
||||||
Addr: "FailoverClient",
|
Addr: "FailoverClient",
|
||||||
|
Dialer: opt.Dialer,
|
||||||
OnConnect: opt.OnConnect,
|
OnConnect: opt.OnConnect,
|
||||||
|
|
||||||
DB: opt.DB,
|
DB: opt.DB,
|
||||||
|
@ -304,7 +305,7 @@ func (c *sentinelFailover) Pool() *pool.ConnPool {
|
||||||
return c.pool
|
return c.pool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *sentinelFailover) dial() (net.Conn, error) {
|
func (c *sentinelFailover) dial(network, addr string) (net.Conn, error) {
|
||||||
addr, err := c.MasterAddr()
|
addr, err := c.MasterAddr()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -3,6 +3,7 @@ package redis
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"net"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -19,6 +20,7 @@ type UniversalOptions struct {
|
||||||
|
|
||||||
// Common options.
|
// Common options.
|
||||||
|
|
||||||
|
Dialer func(network, addr string) (net.Conn, error)
|
||||||
OnConnect func(*Conn) error
|
OnConnect func(*Conn) error
|
||||||
Password string
|
Password string
|
||||||
MaxRetries int
|
MaxRetries int
|
||||||
|
@ -54,6 +56,7 @@ func (o *UniversalOptions) cluster() *ClusterOptions {
|
||||||
|
|
||||||
return &ClusterOptions{
|
return &ClusterOptions{
|
||||||
Addrs: o.Addrs,
|
Addrs: o.Addrs,
|
||||||
|
Dialer: o.Dialer,
|
||||||
OnConnect: o.OnConnect,
|
OnConnect: o.OnConnect,
|
||||||
|
|
||||||
Password: o.Password,
|
Password: o.Password,
|
||||||
|
@ -89,6 +92,8 @@ func (o *UniversalOptions) failover() *FailoverOptions {
|
||||||
return &FailoverOptions{
|
return &FailoverOptions{
|
||||||
SentinelAddrs: o.Addrs,
|
SentinelAddrs: o.Addrs,
|
||||||
MasterName: o.MasterName,
|
MasterName: o.MasterName,
|
||||||
|
|
||||||
|
Dialer: o.Dialer,
|
||||||
OnConnect: o.OnConnect,
|
OnConnect: o.OnConnect,
|
||||||
|
|
||||||
DB: o.DB,
|
DB: o.DB,
|
||||||
|
@ -121,6 +126,7 @@ func (o *UniversalOptions) simple() *Options {
|
||||||
|
|
||||||
return &Options{
|
return &Options{
|
||||||
Addr: addr,
|
Addr: addr,
|
||||||
|
Dialer: o.Dialer,
|
||||||
OnConnect: o.OnConnect,
|
OnConnect: o.OnConnect,
|
||||||
|
|
||||||
DB: o.DB,
|
DB: o.DB,
|
||||||
|
|
Loading…
Reference in New Issue