Configurable Retry Strategies
Problem statement
Simulate a retry client whose ordered attempt outcomes are provided in outcomes. Each outcome is either FAILURE or SUCCESS:value.
The client selects one retry strategy:
ONCEpermits at most one retry after the initial attempt.FIXEDpermits at mostmaxRetriesretries and waitsbaseDelaymilliseconds before each retry.EXPONENTIALpermits at mostmaxRetriesretries. The wait before retry numberk, where the first retry isk = 1, isbaseDelay * 2^(k - 1)milliseconds.
Inspect outcomes from left to right, beginning with the initial attempt at elapsed time 0. Stop at the first success or when the selected retry budget is exhausted. The input always contains enough outcomes for every permitted attempt.
Return SUCCESS:value|attempts|elapsed after a success, or FAILED|attempts|elapsed when every permitted attempt fails. attempts includes the initial attempt, and elapsed is the sum of waits before executed retries.
Function
simulateRetries(outcomes: String[], strategy: String, maxRetries: int, baseDelay: long) → StringExamples
Example 1
outcomes = ["FAILURE","SUCCESS:ok"]strategy = "ONCE"maxRetries = 5baseDelay = 10return = "SUCCESS:ok|2|10"The initial attempt fails. ONCE permits one retry after a 10-millisecond wait, and that retry succeeds.
Example 2
outcomes = ["FAILURE","FAILURE","SUCCESS:ready","FAILURE"]strategy = "EXPONENTIAL"maxRetries = 3baseDelay = 5return = "SUCCESS:ready|3|15"The two executed retries wait 5 and 10 milliseconds. The third attempt succeeds after 15 total milliseconds.
Example 3
outcomes = ["FAILURE","FAILURE","SUCCESS:late","FAILURE"]strategy = "ONCE"maxRetries = 3baseDelay = 7return = "FAILED|2|7"ONCE stops after the initial attempt and one retry, so the later success is never inspected.
Constraints
strategyisONCE,FIXED, orEXPONENTIAL.0 <= maxRetries <= 30.1 <= baseDelay <= 10^6.outcomes.lengthis at least the maximum number of attempts allowed by the selected strategy.- Every outcome is exactly
FAILUREor begins withSUCCESS:. - Every elapsed-time result fits in a signed 64-bit integer.