@@ -137,6 +137,29 @@ type baseClient struct {
137137 onClose func () error // hook called when client is closed
138138}
139139
140+ func newBaseClient (opt * Options , connPool pool.Pooler ) * baseClient {
141+ return & baseClient {
142+ opt : opt ,
143+ connPool : connPool ,
144+ }
145+ }
146+
147+ func (c * baseClient ) clone () * baseClient {
148+ clone := * c
149+ return & clone
150+ }
151+
152+ func (c * baseClient ) withTimeout (timeout time.Duration ) * baseClient {
153+ opt := c .opt .clone ()
154+ opt .ReadTimeout = timeout
155+ opt .WriteTimeout = timeout
156+
157+ clone := c .clone ()
158+ clone .opt = opt
159+
160+ return clone
161+ }
162+
140163func (c * baseClient ) String () string {
141164 return fmt .Sprintf ("Redis<%s db:%d>" , c .getAddr (), c .opt .DB )
142165}
@@ -481,7 +504,7 @@ func txPipelineReadQueued(rd *proto.Reader, cmds []Cmder) error {
481504// underlying connections. It's safe for concurrent use by multiple
482505// goroutines.
483506type Client struct {
484- baseClient
507+ * baseClient
485508 cmdable
486509 hooks
487510 ctx context.Context
@@ -492,17 +515,27 @@ func NewClient(opt *Options) *Client {
492515 opt .init ()
493516
494517 c := Client {
495- baseClient : baseClient {
496- opt : opt ,
497- connPool : newConnPool (opt ),
498- },
499- ctx : context .Background (),
518+ baseClient : newBaseClient (opt , newConnPool (opt )),
519+ ctx : context .Background (),
500520 }
501521 c .cmdable = c .Process
502522
503523 return & c
504524}
505525
526+ func (c * Client ) clone () * Client {
527+ clone := * c
528+ clone .cmdable = clone .Process
529+ clone .hooks .Lock ()
530+ return & clone
531+ }
532+
533+ func (c * Client ) WithTimeout (timeout time.Duration ) * Client {
534+ clone := c .clone ()
535+ clone .baseClient = c .baseClient .withTimeout (timeout )
536+ return clone
537+ }
538+
506539func (c * Client ) Context () context.Context {
507540 return c .ctx
508541}
@@ -511,11 +544,9 @@ func (c *Client) WithContext(ctx context.Context) *Client {
511544 if ctx == nil {
512545 panic ("nil context" )
513546 }
514- clone := * c
515- clone .cmdable = clone .Process
516- clone .hooks .Lock ()
547+ clone := c .clone ()
517548 clone .ctx = ctx
518- return & clone
549+ return clone
519550}
520551
521552func (c * Client ) Conn () * Conn {
0 commit comments