Skip to content

TimeoutError

The error for something that did not answer in time: a connection that never opened, a query still running when its limit was reached, a cache that went quiet. It sits under RuntimeError, beside the other refusals that come from outside the program.

It is the one failure that tells you nothing about what happened. A refused connection did not happen; a timed-out one may have arrived, been carried out, and only lost its answer on the way back. So the safe answer to a timeout is rarely to do the thing again — it is to ask whether it was already done, unless doing it twice is harmless.

A deadline is also worth setting in your own code, so that work nobody is waiting for any more stops instead of holding a request open. An error raised that way carries what it was waiting for in its message, like any other.

The examples below show a helper that gives an operation a budget and gives up when it is spent, a payment whose timeout is answered by asking rather than by paying again, and one deadline shared across the several steps of a single request.

Examples

A deadline of your own

<?nvs
// Work nobody is waiting for any more should stop rather than hold a request
// open. A helper that carries the time left raises a TimeoutError as soon as a
// step would go past it.
class Budget
{
public int $left;
public function constructor(int $milliseconds)
{
$this->left = $milliseconds;
}
public function spend(string $step, int $cost): void
{
if ($cost > $this->left) {
throw new TimeoutError(
$step
. ' needs '
. $cost as string
. 'ms and only '
. $this->left as string
. 'ms are left',
);
}
$this->left = $this->left - $cost;
echo $step, ' took ', $cost, "ms\n";
}
}
var $budget = new Budget(100);
try {
$budget->spend('looking up the customer', 30);
$budget->spend('reading their orders', 40);
$budget->spend('asking the shipper', 60);
echo "all three finished\n";
} catch (TimeoutError $late) {
echo 'gave up: ', $late->message, "\n";
}
Output
looking up the customer took 30ms
reading their orders took 40ms
gave up: asking the shipper needs 60ms and only 30ms are left

What a timeout does not tell you

<?nvs
// A timeout is the one failure that says nothing about what happened. The
// charge may have gone through and only the answer been lost, so the safe move
// is to ask rather than to charge again.
class Payments
{
public static function charge(string $reference, int $pence): string
{
// Stands in for the call to the payment company. No answer came back,
// which is not the same as nothing having happened.
throw new TimeoutError('no answer from the payment company after 5000ms');
}
public static function stateOf(string $reference): string
{
// Asking about a reference changes nothing, so it is always safe to
// repeat.
return 'taken';
}
}
try {
echo Payments::charge('order-1042', 2599), "\n";
} catch (TimeoutError $silence) {
echo $silence->message, "\n";
string $state = Payments::stateOf('order-1042');
if ($state == 'taken') {
echo "it went through after all — not charging twice\n";
} else {
echo "nothing was taken — safe to try once more\n";
}
}
Output
no answer from the payment company after 5000ms
it went through after all — not charging twice

A job that stops inside its slot

<?nvs
// A job that runs on a schedule has a slot, and running past it is worse than
// finishing later. Stopping with a TimeoutError leaves the work in a state the
// next run can pick up.
class Slot
{
public int $left;
public function constructor(int $milliseconds)
{
$this->left = $milliseconds;
}
public function take(int $cost): void
{
if ($cost > $this->left) {
throw new TimeoutError('the slot is spent');
}
$this->left = $this->left - $cost;
}
}
array<int> $invoices = [40, 35, 50, 30, 45];
var $slot = new Slot(120);
uint $done = 0;
try {
foreach ($invoices as int $cost) {
$slot->take($cost);
$done = $done + 1;
echo 'sent invoice ', $done, "\n";
}
echo "the whole batch went out\n";
} catch (TimeoutError $spent) {
// Not a failure of the batch: the work that was done is done, and the run
// says where to start again.
echo $spent->message, ', ', $done, ' of ', Core\Arr::count($invoices), " sent\n";
echo 'the next run starts at invoice ', $done + 1, "\n";
}
Output
sent invoice 1
sent invoice 2
the slot is spent, 2 of 5 sent
the next run starts at invoice 3