44 "context"
55 "errors"
66 "fmt"
7+ "net"
78 "strings"
89 "sync/atomic"
910 "time"
@@ -30,7 +31,7 @@ type Hook interface {
3031}
3132
3233type (
33- DialHook func (ctx context.Context ) error
34+ DialHook func (ctx context.Context , network , addr string ) (net. Conn , error )
3435 ProcessHook func (ctx context.Context , cmd Cmder ) error
3536 ProcessPipelineHook func (ctx context.Context , cmds []Cmder ) error
3637)
@@ -68,6 +69,15 @@ func (hs *hooks) clone() hooks {
6869 return clone
6970}
7071
72+ func (hs * hooks ) setDial (dial DialHook ) {
73+ hs .dial = dial
74+ for _ , h := range hs .slice {
75+ if wrapped := h .DialHook (hs .dial ); wrapped != nil {
76+ hs .dial = wrapped
77+ }
78+ }
79+ }
80+
7181func (hs * hooks ) setProcess (process ProcessHook ) {
7282 hs .process = process
7383 for _ , h := range hs .slice {
@@ -124,13 +134,6 @@ type baseClient struct {
124134 onClose func () error // hook called when client is closed
125135}
126136
127- func newBaseClient (opt * Options , connPool pool.Pooler ) * baseClient {
128- return & baseClient {
129- opt : opt ,
130- connPool : connPool ,
131- }
132- }
133-
134137func (c * baseClient ) clone () * baseClient {
135138 clone := * c
136139 return & clone
@@ -286,6 +289,10 @@ func (c *baseClient) withConn(
286289 return fn (ctx , cn )
287290}
288291
292+ func (c * baseClient ) dial (ctx context.Context , network , addr string ) (net.Conn , error ) {
293+ return c .opt .Dialer (ctx , network , addr )
294+ }
295+
289296func (c * baseClient ) process (ctx context.Context , cmd Cmder ) error {
290297 var lastErr error
291298 for attempt := 0 ; attempt <= c .opt .MaxRetries ; attempt ++ {
@@ -527,15 +534,19 @@ func NewClient(opt *Options) *Client {
527534 opt .init ()
528535
529536 c := Client {
530- baseClient : newBaseClient (opt , newConnPool (opt )),
537+ baseClient : & baseClient {
538+ opt : opt ,
539+ },
531540 }
541+ c .connPool = newConnPool (opt , c .baseClient .dial )
532542 c .init ()
533543
534544 return & c
535545}
536546
537547func (c * Client ) init () {
538548 c .cmdable = c .Process
549+ c .hooks .setDial (c .baseClient .dial )
539550 c .hooks .setProcess (c .baseClient .process )
540551 c .hooks .setProcessPipeline (c .baseClient .processPipeline )
541552 c .hooks .setProcessTxPipeline (c .baseClient .processTxPipeline )
@@ -696,6 +707,7 @@ func newConn(opt *Options, connPool pool.Pooler) *Conn {
696707 c .cmdable = c .Process
697708 c .statefulCmdable = c .Process
698709
710+ c .hooks .setDial (c .baseClient .dial )
699711 c .hooks .setProcess (c .baseClient .process )
700712 c .hooks .setProcessPipeline (c .baseClient .processPipeline )
701713 c .hooks .setProcessTxPipeline (c .baseClient .processTxPipeline )
0 commit comments