Skip to content

Commit a579d58

Browse files
authored
Merge pull request #1253 from go-redis/feature/with-timeout
Add WithTimeout
2 parents 2df96f7 + d2e5283 commit a579d58

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

options.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ func (opt *Options) init() {
169169
}
170170
}
171171

172+
func (opt *Options) clone() *Options {
173+
clone := *opt
174+
return &clone
175+
}
176+
172177
// ParseURL parses an URL into Options that can be used to connect to Redis.
173178
func ParseURL(redisURL string) (*Options, error) {
174179
o := &Options{Network: "tcp"}

redis.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
140163
func (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.
483506
type 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+
506539
func (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

521552
func (c *Client) Conn() *Conn {

redis_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ var _ = Describe("Client", func() {
5959
client.Close()
6060
})
6161

62+
It("should Stringer", func() {
63+
Expect(client.String()).To(Equal("Redis<:6380 db:15>"))
64+
})
65+
6266
It("supports WithContext", func() {
6367
c, cancel := context.WithCancel(context.Background())
6468
cancel()
@@ -67,8 +71,15 @@ var _ = Describe("Client", func() {
6771
Expect(err).To(MatchError("context canceled"))
6872
})
6973

70-
It("should Stringer", func() {
71-
Expect(client.String()).To(Equal("Redis<:6380 db:15>"))
74+
It("supports WithTimeout", func() {
75+
err := client.ClientPause(time.Second).Err()
76+
Expect(err).NotTo(HaveOccurred())
77+
78+
err = client.WithTimeout(10 * time.Millisecond).Ping().Err()
79+
Expect(err).To(HaveOccurred())
80+
81+
err = client.Ping().Err()
82+
Expect(err).NotTo(HaveOccurred())
7283
})
7384

7485
It("should ping", func() {

sentinel.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,11 @@ func NewFailoverClient(failoverOpt *FailoverOptions) *Client {
9494
}
9595

9696
c := Client{
97-
baseClient: baseClient{
98-
opt: opt,
99-
connPool: failover.Pool(),
100-
onClose: failover.Close,
101-
},
102-
ctx: context.Background(),
97+
baseClient: newBaseClient(opt, failover.Pool()),
98+
ctx: context.Background(),
10399
}
104100
c.cmdable = c.Process
101+
c.onClose = failover.Close
105102

106103
return &c
107104
}

0 commit comments

Comments
 (0)