@@ -124,8 +124,11 @@ func (c *Client) CAS(ctx context.Context, key string, f func(in interface{}) (ou
124
124
var revision int64
125
125
var lastErr error
126
126
127
+ opsCtx , cancel := c .opsContext (ctx )
128
+ defer cancel ()
129
+
127
130
for i := 0 ; i < c .cfg .MaxRetries ; i ++ {
128
- resp , err := c .cli .Get (ctx , key )
131
+ resp , err := c .cli .Get (opsCtx , key )
129
132
if err != nil {
130
133
level .Error (c .logger ).Log ("msg" , "error getting key" , "key" , key , "err" , err )
131
134
lastErr = err
@@ -165,7 +168,7 @@ func (c *Client) CAS(ctx context.Context, key string, f func(in interface{}) (ou
165
168
continue
166
169
}
167
170
168
- result , err := c .cli .Txn (ctx ).
171
+ result , err := c .cli .Txn (opsCtx ).
169
172
If (clientv3 .Compare (clientv3 .Version (key ), "=" , revision )).
170
173
Then (clientv3 .OpPut (key , string (buf ))).
171
174
Commit ()
@@ -198,7 +201,12 @@ func (c *Client) WatchKey(ctx context.Context, key string, f func(interface{}) b
198
201
199
202
// Ensure the context used by the Watch is always cancelled.
200
203
watchCtx , cancel := context .WithCancel (ctx )
201
- defer cancel ()
204
+ defer func () {
205
+ cancel ()
206
+ level .Debug (c .logger ).Log ("msg" , "Finished watching key" , "key" , key )
207
+ }()
208
+
209
+ level .Debug (c .logger ).Log ("msg" , "Watching key" , "key" , key )
202
210
203
211
outer:
204
212
for backoff .Ongoing () {
@@ -234,7 +242,12 @@ func (c *Client) WatchPrefix(ctx context.Context, key string, f func(string, int
234
242
235
243
// Ensure the context used by the Watch is always cancelled.
236
244
watchCtx , cancel := context .WithCancel (ctx )
237
- defer cancel ()
245
+ defer func () {
246
+ cancel ()
247
+ level .Debug (c .logger ).Log ("msg" , "Finished watching prefix" , "key" , key )
248
+ }()
249
+
250
+ level .Debug (c .logger ).Log ("msg" , "Watching prefix" , "key" , key )
238
251
239
252
outer:
240
253
for backoff .Ongoing () {
@@ -268,7 +281,10 @@ outer:
268
281
269
282
// List implements kv.Client.
270
283
func (c * Client ) List (ctx context.Context , prefix string ) ([]string , error ) {
271
- resp , err := c .cli .Get (ctx , prefix , clientv3 .WithPrefix (), clientv3 .WithKeysOnly ())
284
+ opsCtx , cancel := c .opsContext (ctx )
285
+ defer cancel ()
286
+
287
+ resp , err := c .cli .Get (opsCtx , prefix , clientv3 .WithPrefix (), clientv3 .WithKeysOnly ())
272
288
if err != nil {
273
289
return nil , err
274
290
}
@@ -281,7 +297,10 @@ func (c *Client) List(ctx context.Context, prefix string) ([]string, error) {
281
297
282
298
// Get implements kv.Client.
283
299
func (c * Client ) Get (ctx context.Context , key string ) (interface {}, error ) {
284
- resp , err := c .cli .Get (ctx , key )
300
+ opsCtx , cancel := c .opsContext (ctx )
301
+ defer cancel ()
302
+
303
+ resp , err := c .cli .Get (opsCtx , key )
285
304
if err != nil {
286
305
return nil , err
287
306
}
@@ -295,10 +314,17 @@ func (c *Client) Get(ctx context.Context, key string) (interface{}, error) {
295
314
296
315
// Delete implements kv.Client.
297
316
func (c * Client ) Delete (ctx context.Context , key string ) error {
298
- _ , err := c .cli .Delete (ctx , key )
317
+ opsCtx , cancel := c .opsContext (ctx )
318
+ defer cancel ()
319
+
320
+ _ , err := c .cli .Delete (opsCtx , key )
299
321
return err
300
322
}
301
323
302
324
func (c * Client ) LastUpdateTime (_ string ) time.Time {
303
325
return time .Now ().UTC ()
304
326
}
327
+
328
+ func (c * Client ) opsContext (parent context.Context ) (context.Context , context.CancelFunc ) {
329
+ return context .WithTimeout (parent , time .Duration (float64 (c .cfg .DialTimeout )* float64 (c .cfg .MaxRetries )))
330
+ }
0 commit comments