MJ::Retry
When to try again, and how long to wait — in one place, because two transports need identical answers and a divergence here is invisible until a launch.
@memo found two defects in the first version of this, both real:
- The provider tells you how long to wait and we ignored it. Runware answers a
capacity rejection with
429+serviceOverloaded, aretryAftervalue in the body and aRetry-Afterheader. Guessing an exponential backoff when the API has stated a number is strictly worse: too short and you add load to something already over capacity, too long and you waste a caller's deadline. - Not every 429 is worth retrying. OpenAI returns 429 for rate limiting AND for
insufficient_quota, which means the account is out of money. Waiting never fixes that, so a blanket 429 retry burns three attempts and the latency to arrive at the same failure. Same shape as the refusal-vs-error trap: the status code is not the signal, the body is.
Constants
How long to wait before attempt N (1-based), honouring whatever the provider said.
Precedence: the Retry-After header, then a retryAfter field anywhere in the body,
then exponential backoff with jitter. Jitter matters because several fibers hitting
the same 429 would otherwise retry in lockstep and rebuild the burst that caused it.
The longest we will ever wait on a provider's instruction. Past this, retrying is
not patience, it is noise.
Phrases that mean "this will fail again however long you wait". Collected from provider responses; do not add one speculatively, because a wrong entry here turns a transient blip into an immediate hard failure.
Transient by status. 429 is included but gated by permanent? below.
Class methods
Kept for callers that just want a number; prefer delay?.
nil means DO NOT RETRY: the provider named a wait longer than we are willing to serve, so the honest move is to fail now with its reason intact rather than burn the attempts at a shorter interval it already told us is useless.
Google's free tier is the case that taught this: an exhausted daily image quota answers 429 with retryDelay "3175s" — 53 minutes. Clamping that to 60s meant two pointless retries and two more rejections, when the answer was simply "not today".
How long the provider asked for, whether or not we are willing to wait it — so an error message can say "it asked for 3175s" instead of leaving the caller guessing.