Core\RateLimit\Decision::limit
$decision->limit(): uintReturns the limit that a rate limit decision was made against.
When you call Core\RateLimit::consumeLocally or Core\RateLimit::consume, you pass the number of units a key may use in a period. The Core\RateLimit\Decision you get back contains that number, and limit returns it unchanged. It is the same for a call that was allowed and for one that was not.
This is useful when the code that writes the response does not know which limit was used. A function can write the RateLimit-Limit header from the decision alone, whatever plan the user has.
Return value
Returns uint. The limit as it was passed.
Examples
The same for every decision
<?nvs// Shows how `limit` returns the limit that a decision was made against.
// The limit is 1 call per hour. The first call is allowed and the second// is not, but both decisions have the same limit. This prints `1` twice.var $first = Core\RateLimit::consumeLocally('password:reset', 1, 1h);var $second = Core\RateLimit::consumeLocally('password:reset', 1, 1h);echo $first->limit(), "\n";echo $second->limit(), "\n";11A limit for each plan
<?nvs// Shows a function that reports a limit from the decision alone. It does not// need to know which plan the user has.
class Quota{ public static function check(string $user, uint $perHour): Core\RateLimit\Decision { return Core\RateLimit::consumeLocally('api:' . $user, $perHour, 1h); }
public static function report(Core\RateLimit\Decision $decision): void { echo 'limit: ', $decision->limit(), " calls per hour\n"; }}
// A free user may make 100 calls per hour, and a paid user 5000.// This prints `100` and then `5000`.Quota::report(Quota::check('free:12', 100));Quota::report(Quota::check('paid:7', 5000));limit: 100 calls per hourlimit: 5000 calls per hourRatelimit headers
<?nvs// Shows how an API writes the `RateLimit-Limit` and `RateLimit-Remaining`// headers of a response from one decision.
class Headers{ public static function of(Core\RateLimit\Decision $decision): string { return 'RateLimit-Limit: ' . $decision->limit() . "\n" . 'RateLimit-Remaining: ' . $decision->remaining() . "\n"; }}
// Each API key may make 3 calls per minute. The limit is 3 on every call.// The remaining count goes down by one each time.uint $call = 0;while ($call < 3) { echo Headers::of(Core\RateLimit::consumeLocally('key:alpha', 3, 1m)); $call = $call + 1;}RateLimit-Limit: 3RateLimit-Remaining: 2RateLimit-Limit: 3RateLimit-Remaining: 1RateLimit-Limit: 3RateLimit-Remaining: 0