MCPcopy Create free account
hub / github.com/cloud-native-go/examples / withExponentialBackoffAndJitter

Function withExponentialBackoffAndJitter

ch09/backoff-simulator/main.go:136–152  ·  view source on GitHub ↗

Using withExponentialBackoff as the backoff function sends retries initially with a 1-second delay, but doubling after each attempt to a maximum delay of 1-minute. Each backoff time gets an additional

()

Source from the content-addressed store, hash-verified

134// initially with a 1-second delay, but doubling after each attempt to
135// a maximum delay of 1-minute. Each backoff time gets an additional
136func withExponentialBackoffAndJitter() string {
137 res, err := sendRequest()
138 base, cap := time.Second, time.Minute
139
140 for backoff := base; err != nil; backoff <<= 1 {
141 if backoff > cap {
142 backoff = cap
143 }
144
145 jitter := rand.Int63n(int64(backoff * 3))
146 sleep := base + time.Duration(jitter)
147 time.Sleep(sleep)
148 res, err = sendRequest()
149 }
150
151 return res
152}
153
154// sendRequest simulates sending a request. It always returns an
155// error after a short delay.

Callers

nothing calls this directly

Calls 1

sendRequestFunction · 0.85

Tested by

no test coverage detected