Bounded Retry Loops

A retry loop repeats an operation only for selected outcomes and needs a clear attempt budget and stopping rule.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
2 min
In this article4 chapters
  1. Count actual calls
  2. Test a sequence of outcomes
  3. Count attempts from the first call
  4. Choose failures that can clear before another call

Count actual calls

With a limit of three attempts, an initial failure and two retries consume the full budget. Define whether a named limit means attempts or additional retries, and use counter names and behavior that match. A permanent validation failure can exit immediately.

Test a sequence of outcomes

Supply temporary failure, temporary failure, then success to a fake operation. The loop should make exactly three calls and return success. Test three temporary failures to verify exhaustion, and consider the operation's repeat safety before applying the loop to an effectful command.[1]

Count attempts from the first call

A budget of three attempts schedules the original call and at most two further calls. Write down the sequence with a failing operation and record each time the callable executes. A loop named for retry in range(3) can accidentally permit four calls when someone places the first attempt outside it. On success during attempt two, return immediately and check that the scheduled delay for attempt three never starts. Treat “maximum attempts” and “maximum retries” as separate terms when documenting a public interface. A counter-based while loop makes the boundary clear if execution can leave early.

Choose failures that can clear before another call

A temporary connection failure on a read might justify another attempt. A malformed record generally remains malformed until its data changes. Test a scripted sequence of transient error, transient error, success, and record both the number of calls and delay positions. For a call that writes data, the absence of a response leaves open the possibility that the write succeeded. Retrying such a call safely depends on the operation's idempotency contract. Also consider elapsed time where callers have their own deadline. If the next delay exceeds the remaining time, stop and preserve the latest cause for the caller.

References

  1. Python Software Foundation, Python Tutorial: Control Flow ↩