MCPcopy
hub / github.com/grpc/grpc-go / RunF

Function RunF

internal/backoff/backoff.go:86–109  ·  view source on GitHub ↗

RunF provides a convenient way to run a function f repeatedly until the context expires or f returns a non-nil error that is not ErrResetBackoff. When f returns ErrResetBackoff, RunF continues to run f, but resets its backoff state before doing so. backoff accepts an integer representing the number

(ctx context.Context, f func() error, backoff func(int) time.Duration)

Source from the content-addressed store, hash-verified

84// backoff state before doing so. backoff accepts an integer representing the
85// number of retries, and returns the amount of time to backoff.
86func RunF(ctx context.Context, f func() error, backoff func(int) time.Duration) {
87 attempt := 0
88 timer := time.NewTimer(0)
89 for ctx.Err() == nil {
90 select {
91 case <-timer.C:
92 case <-ctx.Done():
93 timer.Stop()
94 return
95 }
96
97 err := f()
98 if errors.Is(err, ErrResetBackoff) {
99 timer.Reset(0)
100 attempt = 0
101 continue
102 }
103 if err != nil {
104 return
105 }
106 timer.Reset(backoff(attempt))
107 attempt++
108 }
109}

Callers 2

runMethod · 0.92
runnerMethod · 0.92

Calls 6

StopMethod · 0.95
ResetMethod · 0.95
NewTimerMethod · 0.80
ErrMethod · 0.80
IsMethod · 0.80
DoneMethod · 0.45

Tested by

no test coverage detected