@@ -137,6 +137,29 @@ type baseClient struct {
137
137
onClose func () error // hook called when client is closed
138
138
}
139
139
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
+
140
163
func (c * baseClient ) String () string {
141
164
return fmt .Sprintf ("Redis<%s db:%d>" , c .getAddr (), c .opt .DB )
142
165
}
@@ -481,7 +504,7 @@ func txPipelineReadQueued(rd *proto.Reader, cmds []Cmder) error {
481
504
// underlying connections. It's safe for concurrent use by multiple
482
505
// goroutines.
483
506
type Client struct {
484
- baseClient
507
+ * baseClient
485
508
cmdable
486
509
hooks
487
510
ctx context.Context
@@ -492,17 +515,27 @@ func NewClient(opt *Options) *Client {
492
515
opt .init ()
493
516
494
517
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 (),
500
520
}
501
521
c .cmdable = c .Process
502
522
503
523
return & c
504
524
}
505
525
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
+
506
539
func (c * Client ) Context () context.Context {
507
540
return c .ctx
508
541
}
@@ -511,11 +544,9 @@ func (c *Client) WithContext(ctx context.Context) *Client {
511
544
if ctx == nil {
512
545
panic ("nil context" )
513
546
}
514
- clone := * c
515
- clone .cmdable = clone .Process
516
- clone .hooks .Lock ()
547
+ clone := c .clone ()
517
548
clone .ctx = ctx
518
- return & clone
549
+ return clone
519
550
}
520
551
521
552
func (c * Client ) Conn () * Conn {
0 commit comments