Skip to main content

Laravel's own limits

Behind the gateway, Laravel can keep limits of its own as a second line, counted with the numbers of the tiers the product sheet carries. Register the limiter in the boot method of a service provider, with the way to tell the tier of a request's caller, and name it on the routes of the API host:

use Illuminate\Http\Request;
use Pushery\FrontlineApi\Limits\SecondLine;

SecondLine::register(fn (Request $request): ?string => $request->user()?->tier);
use Illuminate\Support\Facades\Route;
use Pushery\FrontlineApi\Http\Middleware\VerifyFrontlineSignature;

Route::domain('api.example.com')
->middleware([VerifyFrontlineSignature::class, 'throttle:frontline'])
->frontline(limitClass: 'standard')
->group(base_path('routes/api.php'));

It counts the way the gateway counts, in calendar minutes and seconds. Each limit keeps at most two windows per caller in the cache store of Laravel's rate limiter, the one running and the one before, so a database or a file store, which deletes an expired entry only when its key is read again, does not fill with the windows that have passed. A request with a key counts by the counters the caller's tier holds for the route's limit class, per key and per tenant as the gateway sent them, a key without a tenant on a class that counts only tenants by the tenant's numbers, and on a route that writes or deletes also by writes_per_minute, where 0 refuses every write. On a route with operations, a request that names one of them counts by the counters of the operation's class instead, and every other request by the route's. On an MCP route, a request without a version whose headers name its operation, or with a name in Base64 that does not decode, counts against nothing and gets the gateway's 400 malformed_request instead. A route the gateway lets through without a key counts by its guard limit, per client address or per value of the named parameter, every request without a value for it together, as the gateway counts them. The tier source is read on every request, so a changed tier counts at once. Each number grows by a margin, 10 percent by default and rounded up, against the two clocks not agreeing to the second, and a limit per second counts twice its number, because a second of your application can hold the end of one second of the gateway and the start of the next. So the second line stops a caller only where the gateway did not count; SecondLine::register($tierOf, 0) counts without a margin. With Laravel's ThrottleRequests a refused request costs nothing, as at the gateway. ThrottleRequestsWithRedis, which throttleWithRedis() puts behind throttle:, counts each limit as it checks it, so the second line checks the limits per second first: a request one of them refuses costs nothing that outlasts its second, while one a limit per minute refuses still counts against the limits before it. A refusal names in Retry-After the wait until its window ends, the calendar minute or second, as the gateway's does; where a limit per second and one per minute refuse the same request, it names the second's. How many requests run at once stays the gateway's to limit. A caller whose tier your resolver does not name is counted by nothing.