MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / RetryLoop

Function RetryLoop

base/util.go:451–489  ·  view source on GitHub ↗
(ctx context.Context, description string, worker RetryWorker[T], sleeper RetrySleeper)

Source from the content-addressed store, hash-verified

449}
450
451func RetryLoop[T any](ctx context.Context, description string, worker RetryWorker[T], sleeper RetrySleeper) (error, T) {
452
453 numAttempts := 1
454
455 for {
456 shouldRetry, err, value := worker()
457 if !shouldRetry {
458 if err != nil {
459 return err, *new(T)
460 }
461 return nil, value
462 }
463 shouldContinue, sleepMs := sleeper(numAttempts)
464 if !shouldContinue {
465 if err == nil {
466 err = NewRetryTimeoutError(description, numAttempts)
467 }
468 WarnfCtx(ctx, "RetryLoop for %v giving up after %v attempts", description, numAttempts)
469 return err, value
470 }
471 DebugfCtx(ctx, KeyAll, "RetryLoop retrying %v after %v ms.", description, sleepMs)
472
473 select {
474 case <-ctx.Done():
475 verb := "closed"
476 ctxErr := ctx.Err()
477 if errors.Is(ctxErr, context.Canceled) {
478 verb = "canceled"
479 } else if errors.Is(ctxErr, context.DeadlineExceeded) {
480 verb = "timed out"
481 }
482 return fmt.Errorf("Retry loop for %v %s based on context: %w", description, verb, context.Cause(ctx)), *new(T)
483 case <-time.After(time.Millisecond * time.Duration(sleepMs)):
484 }
485
486 numAttempts += 1
487
488 }
489}
490
491// A version of RetryLoop that returns a strongly typed cas as uint64, to avoid interface conversion overhead for
492// high throughput operations.

Callers 15

mainFunction · 0.92
WaitForRESTAPIsMethod · 0.92
initializeGoCBAgentMethod · 0.92
doHTTPAuthRequestFunction · 0.92
MakeUserFunction · 0.92
DeleteUserFunction · 0.92
TestLogFlushFunction · 0.92
WaitForNViewResultsMethod · 0.92

Calls 7

NewRetryTimeoutErrorFunction · 0.85
WarnfCtxFunction · 0.85
DebugfCtxFunction · 0.85
DoneMethod · 0.80
ErrMethod · 0.80
ErrorfMethod · 0.80
AfterMethod · 0.80

Tested by 14

TestLogFlushFunction · 0.74
writeDocumentMethod · 0.74
waitForProcessedTotalMethod · 0.74
TestShardedDCPUpgradeFunction · 0.74
TestRetryLoopFunction · 0.68
TestRetryLoopFastFailFunction · 0.68
TestViewFunction · 0.68