Rolling windows and concurrency
Rolling windows
Section titled “Rolling windows”By default, a minute, hour or day limit resets at the UTC
boundary. Say each of your API’s customers may make 1,000 requests an
hour with their key. A key could send 1,000 requests at 12:59 and 1,000
more at 13:01, and both bursts would get a yes. Set rolling: true
and the limit counts the last 60 seconds, 60 minutes or 24 hours at the
moment of each check instead.
import { createClient } from "@limitry/sdk";
// Uses the LIMITRY_API_KEY environment variable.const limitry = createClient();
const limit = await limitry.limits.create({ name: "API per hour", subject: { kind: "apikey" }, action: "api-request", amount: 1000, window: "hour", rolling: true,});console.log(limit.id);from limitry import Limitry
# Uses the LIMITRY_API_KEY environment variable.limitry = Limitry()
limit = limitry.limits.create( name="API per hour", subject={"kind": "apikey"}, action="api-request", amount=1000, window="hour", rolling=True,)print(limit.id)limitry limits create --name "API per hour" --subject '{"kind":"apikey"}' \ --action api-request --amount 1000 --window hour --rollingcurl -X POST https://api.limitry.com/v1/limits \ -H "Authorization: Bearer $LIMITRY_API_KEY" \ -H "Content-Type: application/json" \ -d '{"name":"API per hour","subject":{"kind":"apikey"},"action":"api-request", "amount":1000,"window":"hour","rolling":true}'With every, a rolling limit counts the last every windows, such as
any 5 hours. A rolling minute limit spans at most 60 minutes. An
hour limit spans at most 24 hours, and a day limit at most 30 days.
A rolling limit takes no start, because it always counts back from
each check.
Limitry counts use in 60 slices of the window. With every at 1, a
slice is a second for a minute, a minute for an hour, and 24 minutes
for a day. The oldest
slice counts in full, so a rolling limit never allows more than its
amount. It may say no up to one slice early.
In a check’s response, resetsAt is when enough use has left the window
for the subject to try again. On a no, retryAfterSeconds gives the
same moment. Send it as Retry-After with your 429. The
Rate-limit your API’s customers guide
adds a second rolling limit of 60 in any minute, against bursts.
week, month, year, total and concurrent limits can’t be
rolling. In the app, choose In any 60 seconds, In any 60
minutes or In any 24 hours as the window.
Concurrency
Section titled “Concurrency”A limit with the window concurrent caps work in progress, and its
amount is how many may run at once. Say exports are heavy, and each
workspace in your product may run at most 3 at the same time. Each
reservation takes one slot, whatever its
cost. It gives the slot back when you commit or release it, or when
it expires. The cost still counts against the subject’s other limits.
This limit allows at most 3 exports at once per workspace.
import { createClient } from "@limitry/sdk";
// Uses the LIMITRY_API_KEY environment variable.const limitry = createClient();
const limit = await limitry.limits.create({ name: "Exports at once", subject: { kind: "workspace" }, action: "export", amount: 3, window: "concurrent",});console.log(limit.id);from limitry import Limitry
# Uses the LIMITRY_API_KEY environment variable.limitry = Limitry()
limit = limitry.limits.create( name="Exports at once", subject={"kind": "workspace"}, action="export", amount=3, window="concurrent",)print(limit.id)limitry limits create --name "Exports at once" --subject '{"kind":"workspace"}' \ --action export --amount 3 --window concurrentcurl -X POST https://api.limitry.com/v1/limits \ -H "Authorization: Bearer $LIMITRY_API_KEY" \ -H "Content-Type: application/json" \ -d '{"name":"Exports at once","subject":{"kind":"workspace"},"action":"export", "amount":3,"window":"concurrent"}'Then reserve a slot while each export runs. Limitry says no to a 4th
export until one of the 3 ends. reserve gives the slot back when the
work finishes or fails.
const file = await limitry.reserve( { subject: { kind: "workspace", id: "ws_42" }, action: "export" }, () => runExport(),);with limitry.reserve(subject={"kind": "workspace", "id": "ws_42"}, action="export"): run_export()- While a slot is free, a
consumeorpreviewcheck against a concurrent limit gets a yes, but takes no slot. - When work holds every slot,
retryAfterSecondsis when the earliest reservation expires. A release can free one sooner. - If you reserve, commit and release yourself (see
Reservations), release in a
finallyblock. A reservation you never commit or release keeps its slot until it expires. - When your code can’t reach Limitry and the limit’s fail mode is
open,reservestill runs at mostamountat once in your process for each subject and action. The SDK counts the reservations it has going, and remembers the amount from Limitry’s last response. Beyond that,reservethrowsUnavailableError, as it would for aclosedlimit. The count is per process, so several servers together may run more until Limitry responds again. A process that hasn’t yet had a response for that subject and action doesn’t hold anything back, just as a plainopenlimit wouldn’t.
Long-running work
Section titled “Long-running work”A reservation lasts ttlSeconds, which is 300 by default and 3,600 at
most. For work that runs longer, extend it as a heartbeat.
await limitry.reserve( { subject: { kind: "workspace", id: "ws_42" }, action: "export" }, async (reservation) => { for (const part of parts) { await exportPart(part); await reservation.extend(600); } },);with limitry.reserve(subject={"kind": "workspace", "id": "ws_42"}, action="export") as reservation: for part in parts: export_part(part) reservation.extend(600)limitry reservations extend rsv_… --ttl-seconds 600curl -X POST https://api.limitry.com/v1/reservations/rsv_…/extend \ -H "Authorization: Bearer $LIMITRY_API_KEY" \ -H "Content-Type: application/json" -d '{"ttlSeconds":600}'If your worker dies, it stops extending, and the slot frees itself.
A limit can’t change to or from concurrent, because one counts use
and the other counts open reservations. You can’t change a limit’s
window once it exists, so create a new limit instead. See
What can change.
In reports
Section titled “In reports”Rolling limits report like any other. On the Usage page, Right now shows each subject’s use in the last window.
For a concurrent limit, the chart shows how much work the limit let start each day. Right now shows the slots each subject has reserved, for subjects checked in the last hour.
For both kinds, Limitry sends limit.reached at most once an hour per
subject.