Timeouts and Retries
Lesson 45 of 47 in Coddy's Networking with the Terminal course.
By default curl waits a very long time. In a script that is a bug: one unreachable host and your job hangs until someone notices.
curl -s --max-time 5 --connect-timeout 2 URL--connect-timeout limits how long to spend getting connected. --max-time limits the whole operation. Exceeding either gives exit code 28.
Retrying needs more care than it looks. Repeating a failed GET or PUT is safe, because both are idempotent. Repeating a POST may create a second record, and a timeout is the worst case for it: you never learned whether the first one was processed.
So retry reads freely, and think before retrying writes. When you must, ask the server for a way to make it safe, such as an idempotency key.
Challenge
MediumPut a bound on a request that will never be answered.
Request http://10.255.255.1/, an address that silently swallows traffic, giving up after 2 seconds, and print gave up: followed by curl's exit code.
Expected output:
gave up: 28Without a timeout this request would hang for well over a minute.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Networking with the Terminal
6curl in Depth
Seeing the HeadersSaving ResponsesCustom HeadersStatus and Timingcurl Exit CodesRecap: curl Flags9Auth and Debugging
API Keys and BearerThe User AgentTimeouts and RetriesReading a FailureRecap: DebuggingPractice on your own: Terminal playground