Core\RateLimit\Decision::retryAfter
$decision->retryAfter(): ?Core\Time\DurationReturns how long to wait before a call that was not allowed can be made again.
The result is null when the call was allowed. When the call was not allowed, the result is a Core\Time\Duration. It is the exact time until the same call would be allowed, so a client that waits this long does not have to guess.
Use it to write a Retry-After header, or to tell a user how many minutes to wait. The header needs whole seconds. Round the duration up, so that a client does not come back a moment too early.
Good to know: the wait is calculated for each call. Two clients that were stopped at different moments get different wait times, so they do not all come back at the same moment.
Return value
Returns ?Core\Time\Duration. null when the call is allowed. Otherwise, the Core\Time\Duration until the call would be allowed. A Retry-After header with this value tells each client its own time to try again. So the rejected clients do not all retry at the same moment.
Examples
Null when allowed
<?nvs// Shows that `retryAfter` is `null` when a call is allowed, and a wait time// when it is refused.
// The limit is 1 call per 15 minutes. The first call is allowed,// so this prints `no wait`.var $first = Core\RateLimit::consumeLocally('feedback:form', 1, 15m);var $firstWait = $first->retryAfter();echo $firstWait == null ? 'no wait' : 'wait', "\n";
// The second call is refused. The wait is just under 15 minutes,// so rounded up to whole seconds this prints `wait 900 seconds`.var $second = Core\RateLimit::consumeLocally('feedback:form', 1, 15m);var $secondWait = $second->retryAfter();if ($secondWait != null) { echo 'wait ', Core\Math::quotient($secondWait->toMilliseconds() + 999, 1000), " seconds\n";}no waitwait 900 secondsA retry after header
<?nvs// Shows how an API writes a `Retry-After` header when it refuses a call.
class Api{ public static function answer(string $client): string { // Each client may make 10 calls per hour. var $decision = Core\RateLimit::consumeLocally('api:' . $client, 10, 1h); var $wait = $decision->retryAfter(); if ($wait == null) { return '200 OK'; } // The header needs whole seconds. Rounding up means the client // never comes back too early. uint $seconds = Core\Math::quotient($wait->toMilliseconds() + 999, 1000) as uint; return "429 Too Many Requests\nRetry-After: " . $seconds; }}
// The first 10 calls are allowed. One call drains every 6 minutes,// so the 11th call gets `Retry-After: 360`.string $last = '';uint $call = 0;while ($call < 11) { $last = Api::answer('reports-app'); $call = $call + 1;}echo $last, "\n";429 Too Many RequestsRetry-After: 360Try again in minutes
<?nvs// Shows a login form that tells a user how many minutes to wait after too// many failed attempts.
class Login{ public static function failed(string $user): string { // Each user may fail 5 times per 15 minutes. var $decision = Core\RateLimit::consumeLocally('login:' . $user, 5, 15m); var $wait = $decision->retryAfter(); if ($wait == null) { return 'Wrong password.'; } // The wait is rounded up to whole minutes. int $minutes = Core\Math::quotient($wait->toMilliseconds() + 59999, 60000); return 'Too many attempts. Try again in ' . $minutes . ' minutes.'; }}
// The first 5 failures are allowed. One attempt drains every 3 minutes,// so the 6th failure prints `Try again in 3 minutes.`uint $attempt = 0;while ($attempt < 6) { echo Login::failed('sam'), "\n"; $attempt = $attempt + 1;}Wrong password.Wrong password.Wrong password.Wrong password.Wrong password.Too many attempts. Try again in 3 minutes.