Core\RateLimit::consumeLocally
Core\RateLimit::consumeLocally(
string $key,
uint $limit,
Core\Time\Duration $per,
{burst?: uint, cost?: uint},
): Core\RateLimit\DecisionLimits how often something may happen for one key, counting in this server process only.
A key is what the limit is for, such as one IP address or one user. You say how many units the key may use in a period, such as 2 per minute. Each call uses one unit, or the number you give as cost. The result is a Core\RateLimit\Decision. It says if the call was allowed, how many units are left, and how long to wait.
The count is in the memory of this server process, so consumeLocally is fast and needs no network store. Two server processes with a limit of 100 allow up to 200. The count can also be forgotten when the server needs the memory for other values. Use consumeLocally to protect a server from too much load. For a limit you promised to a customer, use Core\RateLimit::consume.
The examples below show a fourth call that is not allowed, then a call that costs several units, then a slow page that returns status 429 and a Retry-After header.
Parameters
$keystringWho the limit is for. Ataintedvalue is allowed. Asecretvalue is not allowed, the same as forconsume.$limituintHow many units are allowed in each$per, in this server process. Every request this process serves counts against the same limit. Two server processes with a limit of 100 allow up to 200, so a number you promised belongs toconsume.$perCore\Time\DurationThe period in which$limitunits are allowed.
Options
burstuintoptionalHow many units may arrive at the same moment. The default is$limit.costuintoptionalHow many units this call uses. The default is 1.
Return value
Returns Core\RateLimit\Decision. A Core\RateLimit\Decision. It uses no external store, so it does not throw an IOError. The count is kept in the same memory as Core\Cache::memory(), which can forget an entry at any time. When a count is forgotten, the key allows a burst again.
Errors
RuntimeError$limit,$peror$burstis zero, the period is too short to divide into$limitunits, or$costis larger than$burst. These are the same errors thatconsumethrows.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\RateLimit::consumeLocally counts a request in the memory of this server process. Core\RateLimit\Decision->isAllowed() checks the result. The old names were shed and allowed. |
Examples
The fourth call is refused
<?nvs// Shows a limit of 3 calls per minute for one key. The fourth call is not allowed.
uint $call = 1;while ($call <= 4) { var $decision = Core\RateLimit::consumeLocally('report:daily', 3, 1m); // `isAllowed` returns `true` for the first three calls and `false` for the fourth. // `remaining` is how many calls are left, so this prints 2, 1, 0 and 0. echo 'call ', $call, ': ', $decision->isAllowed() ? 'allowed' : 'refused'; echo ', remaining ', $decision->remaining(), "\n"; $call = $call + 1;}call 1: allowed, remaining 2call 2: allowed, remaining 1call 3: allowed, remaining 0call 4: refused, remaining 0An expensive call costs more
<?nvs// Shows how `cost` makes one call use several units of the same limit.
class Work{ public static function run(string $task, uint $cost): void { // Each user may use 10 units per minute. var $decision = Core\RateLimit::consumeLocally('user:77', 10, 1m, { cost: $cost }); if ($decision->isAllowed()) { echo $task, ': done, ', $decision->remaining(), " units left\n"; } else { echo $task, ": refused\n"; } }}
// A search costs 1 unit and an export costs 4. This prints 9, 5 and 1 units left.// The last export needs 4 units and only 1 is left, so it is refused.Work::run('search', 1);Work::run('export', 4);Work::run('export', 4);Work::run('export', 4);search: done, 9 units leftexport: done, 5 units leftexport: done, 1 units leftexport: refusedProtect a slow page
<?nvs// Shows how a server protects a slow page. Each IP address may load it// twice per minute. A third load gets status 429 and a `Retry-After` header.
class SlowPage{ public static function handle(string $ip): void { var $decision = Core\RateLimit::consumeLocally('search:' . $ip, 2, 1m); var $wait = $decision->retryAfter(); if ($wait == null) { echo '200 for ', $ip, "\n"; return; } // `retryAfter` is the exact wait. This rounds it up to whole seconds, // because `Retry-After` counts in seconds. var $seconds = $wait->toSeconds(); if ($wait->toMilliseconds() % 1000 != 0) { $seconds = $seconds + 1; } echo '429 for ', $ip, ', Retry-After: ', $seconds, "\n"; }}
// Each unit takes 30 seconds to come back, so the third load waits 30 seconds.// The second address has its own limit, so its load is allowed.SlowPage::handle('203.0.113.5');SlowPage::handle('203.0.113.5');SlowPage::handle('203.0.113.5');SlowPage::handle('198.51.100.20');200 for 203.0.113.5200 for 203.0.113.5429 for 203.0.113.5, Retry-After: 30200 for 198.51.100.20