A model API sometimes refuses a call. Status 429 means "too many requests" and 529 means "overloaded": both get better if you wait and try again. Status 400 means the request itself is wrong. It will be just as wrong the next time, so you never retry it.
Exponential backoff: wait before the next try, and wait longer each time by doubling. A program that starts at 1 second and allows four tries behaves like this:
after try 1 wait 1 second after try 2 wait 2 seconds after try 3 wait 4 seconds after try 4 give up and tell the customer you are busy
Three details make it work in real life. Jitter: add a small random extra to each wait, so that a thousand refused programs don't all come back at the same moment. retry-after: when the API's answer carries this header, it says how many seconds to stay away, and it wins over your formula. A limit: after the last try, stop at once, without one more wait.
This lesson starts at a tenth of a second, so you don't sit and wait. Three customers write: one is answered on the third try, one never, and one sent an empty message. When your program is done, it prints this:
Try 1 failed with status 429 waiting 0.132 seconds Try 2 failed with status 429 waiting 1.0 seconds Yes, we ship to Canada. It costs a flat $15 and takes 7 to 10 business days. Try 1 failed with status 529 waiting 0.165 seconds Try 2 failed with status 529 waiting 0.207 seconds Try 3 failed with status 529 waiting 0.454 seconds Try 4 failed with status 529 Sorry, we are busy right now. Please try again in a minute. Try 1 failed with status 400 Sorry, we could not answer that question.
The check then adds two lines: Requests sent: 8 and Seconds spent waiting: 1.958.
Your task: ask already tries a call up to four times, one try right after the other. Make it give up at once on any status other than 429 and 529, wait 0.1, 0.2, 0.4 seconds between the tries plus jitter from rng, use the retry-after header when it is there, and skip the wait after the last try. Before each wait, print it rounded to 3 decimals, as in the output above.
Assignment
Follow the Coding Tutorial to turn the plain retry loop into retries with backoff, jitter, retry-after and a limit.
Hint
All the new lines go in the except block, after the print. Their order matters: first decide whether to retry at all, then whether this was the last try, then how long to wait.
Free plan: 30 tutor messages. PRO is unlimited.
