Bound retries by evidence and budget
Select recorded attempts without exceeding an attempt or elapsed-time budget.
Retrying is a policy decision
Some outcomes justify another attempt; others do not. In this exercise, open and refused are final observations. timed_out and error permit another recorded attempt, subject to both an attempt limit and a cumulative elapsed-time budget. Retrying everything can waste time or hide a meaningful refusal behind a later unrelated result.
Check the next attempt's cost before admitting it. Equality is allowed: an attempt ending exactly on the budget fits. An attempt that would exceed the budget is not counted or added to elapsed time. The recorded durations are supplied evidence; the lesson does not sleep, contact a network or pretend to measure wall-clock performance.
Distinguish unavailable evidence
If the table ends after an inconclusive attempt, keep that last outcome. Do not invent a success or claim budget exhaustion without an actual next attempt being rejected. With no records, the result is missing with zero attempts and zero elapsed time.
This model intentionally separates the retry decision from the probe implementation. A live tool also needs per-attempt deadlines, cancellation and an overall execution deadline, because a hung probe cannot be bounded merely by counting attempts after it returns. The downloadable scanner terminates its own connection worker at the configured deadline. Never turn a timeout into proof that a service is closed or protected.
awk -F '\t' 'NR>1 {total+=$3} END {printf "recorded duration=%d ms\n",total}' attempts.tsv
Put it into practice.
attempts.tsv has header attempt<TAB>state<TAB>elapsed_ms and ordered, contiguous attempts with validated states open/refused/timed_out/error and nonnegative integer duration. main.sh arguments are a positive max-attempt count (default 3) and nonnegative millisecond budget (default 100). Start missing/0/0. Before consuming a row, if count is already max or adding its duration exceeds budget, end as budget_exhausted without counting it. Otherwise consume it, update state/time and stop immediately on open or refused. If records end, keep the last state (missing if empty). Print state<TAB>attempts<TAB>elapsed_ms and one result row. Do not sleep.
See an explained solution
max=${1:-3};budget=${2:-100}
awk -F '\t' -v max="$max" -v budget="$budget" '
BEGIN{state="missing";attempts=0;elapsed=0}
NR>1 {
if(done)next
if(attempts>=max || elapsed+$3>budget){state="budget_exhausted";done=1;next}
attempts++;elapsed+=$3;state=$2
if(state=="open" || state=="refused")done=1
}
END{printf "state\tattempts\telapsed_ms\n%s\t%d\t%d\n",state,attempts,elapsed}
' attempts.tsv
The retry policy accounts only for accepted recorded attempts. It preserves terminal observations and incomplete evidence, while distinguishing an exhausted budget from a table that simply has no later observation.