From fd67dd856d1ee83823043a51a4795c1275787d1d Mon Sep 17 00:00:00 2001 From: mikedavidson Date: Tue, 20 Oct 2020 16:17:03 +0300 Subject: [PATCH 1/2] add options for dial when connecting to redis (allowing to control options such as read/write/connect timeout, keep alive and more) --- pool.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pool.go b/pool.go index 7eaa673..b20a416 100644 --- a/pool.go +++ b/pool.go @@ -84,9 +84,9 @@ func (p *MultiHostPool) Get() redis.Conn { return pool.Get() } -func dialFuncWrapper(host string, authPass *string) func() (redis.Conn, error) { +func dialFuncWrapper(host string, authPass *string, options ...redis.DialOption) func() (redis.Conn, error) { return func() (redis.Conn, error) { - conn, err := redis.Dial("tcp", host) + conn, err := redis.Dial("tcp", host, options...) if err != nil { return conn, err } From 0a007ffcbeb50d1bd397e80a712163981b8dbfab Mon Sep 17 00:00:00 2001 From: mikedavidson Date: Tue, 20 Oct 2020 16:34:59 +0300 Subject: [PATCH 2/2] add options for dial when connecting to redis (allowing to control options such as read/write/connect timeout, keep alive and more) --- client.go | 6 +++--- pool.go | 10 ++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index 47f4b65..c58c5b5 100644 --- a/client.go +++ b/client.go @@ -21,13 +21,13 @@ type Client struct { // NewClient creates a new client connecting to the redis host, and using the given name as key prefix. // Addr can be a single host:port pair, or a comma separated list of host:port,host:port... // In the case of multiple hosts we create a multi-pool and select connections at random -func NewClient(addr, name string, authPass *string) *Client { +func NewClient(addr, name string, authPass *string, options ...redis.DialOption) *Client { addrs := strings.Split(addr, ",") var pool ConnPool if len(addrs) == 1 { - pool = NewSingleHostPool(addrs[0], authPass) + pool = NewSingleHostPool(addrs[0], authPass, options...) } else { - pool = NewMultiHostPool(addrs, authPass) + pool = NewMultiHostPool(addrs, authPass, options...) } ret := &Client{ Pool: pool, diff --git a/pool.go b/pool.go index b20a416..33d5a40 100644 --- a/pool.go +++ b/pool.go @@ -23,9 +23,9 @@ type SingleHostPool struct { // s.Pool.Close() //} -func NewSingleHostPool(host string, authPass *string) *SingleHostPool { +func NewSingleHostPool(host string, authPass *string, options ...redis.DialOption) *SingleHostPool { ret := &redis.Pool{ - Dial: dialFuncWrapper(host, authPass), + Dial: dialFuncWrapper(host, authPass, options...), TestOnBorrow: testOnBorrow, MaxIdle: maxConns, } @@ -38,6 +38,7 @@ type MultiHostPool struct { pools map[string]*redis.Pool hosts []string authPass *string + options []redis.DialOption } func (p *MultiHostPool) Close() (err error) { @@ -57,11 +58,12 @@ func (p *MultiHostPool) Close() (err error) { return } -func NewMultiHostPool(hosts []string, authPass *string) *MultiHostPool { +func NewMultiHostPool(hosts []string, authPass *string, options ...redis.DialOption) *MultiHostPool { return &MultiHostPool{ pools: make(map[string]*redis.Pool, len(hosts)), hosts: hosts, authPass: authPass, + options: options, } } @@ -74,7 +76,7 @@ func (p *MultiHostPool) Get() redis.Conn { if !found { pool = &redis.Pool{ - Dial: dialFuncWrapper(host, p.authPass), + Dial: dialFuncWrapper(host, p.authPass, p.options...), TestOnBorrow: testOnBorrow, MaxIdle: maxConns, }