@@ -239,6 +239,7 @@ type Backoffer struct {
239
239
errors []error
240
240
types []backoffType
241
241
vars * kv.Variables
242
+ noop bool
242
243
}
243
244
244
245
// txnStartKey is a key for transaction start_ts info in context.Context.
@@ -253,6 +254,11 @@ func NewBackoffer(ctx context.Context, maxSleep int) *Backoffer {
253
254
}
254
255
}
255
256
257
+ // NewNoopBackoff create a Backoffer do nothing just return error directly
258
+ func NewNoopBackoff (ctx context.Context ) * Backoffer {
259
+ return & Backoffer {ctx : ctx , noop : true }
260
+ }
261
+
256
262
// WithVars sets the kv.Variables to the Backoffer and return it.
257
263
func (b * Backoffer ) WithVars (vars * kv.Variables ) * Backoffer {
258
264
if vars != nil {
@@ -284,6 +290,20 @@ func (b *Backoffer) BackoffWithMaxSleep(typ backoffType, maxSleepMs int, err err
284
290
default :
285
291
}
286
292
293
+ b .types = append (b .types , typ )
294
+ if b .noop || (b .maxSleep > 0 && b .totalSleep >= b .maxSleep ) {
295
+ errMsg := fmt .Sprintf ("%s backoffer.maxSleep %dms is exceeded, errors:" , typ .String (), b .maxSleep )
296
+ for i , err := range b .errors {
297
+ // Print only last 3 errors for non-DEBUG log levels.
298
+ if log .GetLevel () == zapcore .DebugLevel || i >= len (b .errors )- 3 {
299
+ errMsg += "\n " + err .Error ()
300
+ }
301
+ }
302
+ logutil .Logger (context .Background ()).Warn (errMsg )
303
+ // Use the first backoff type to generate a MySQL error.
304
+ return b .types [0 ].TError ()
305
+ }
306
+
287
307
backoffCounter , backoffDuration := typ .metric ()
288
308
backoffCounter .Inc ()
289
309
// Lazy initialize.
@@ -299,7 +319,6 @@ func (b *Backoffer) BackoffWithMaxSleep(typ backoffType, maxSleepMs int, err err
299
319
realSleep := f (b .ctx , maxSleepMs )
300
320
backoffDuration .Observe (float64 (realSleep ) / 1000 )
301
321
b .totalSleep += realSleep
302
- b .types = append (b .types , typ )
303
322
304
323
var startTs interface {}
305
324
if ts := b .ctx .Value (txnStartKey ); ts != nil {
@@ -313,18 +332,6 @@ func (b *Backoffer) BackoffWithMaxSleep(typ backoffType, maxSleepMs int, err err
313
332
zap .Reflect ("txnStartTS" , startTs ))
314
333
315
334
b .errors = append (b .errors , errors .Errorf ("%s at %s" , err .Error (), time .Now ().Format (time .RFC3339Nano )))
316
- if b .maxSleep > 0 && b .totalSleep >= b .maxSleep {
317
- errMsg := fmt .Sprintf ("%s backoffer.maxSleep %dms is exceeded, errors:" , typ .String (), b .maxSleep )
318
- for i , err := range b .errors {
319
- // Print only last 3 errors for non-DEBUG log levels.
320
- if log .GetLevel () == zapcore .DebugLevel || i >= len (b .errors )- 3 {
321
- errMsg += "\n " + err .Error ()
322
- }
323
- }
324
- logutil .Logger (context .Background ()).Warn (errMsg )
325
- // Use the first backoff type to generate a MySQL error.
326
- return b .types [0 ].TError ()
327
- }
328
335
return nil
329
336
}
330
337
0 commit comments