Loading exercise…

Request quota service

Build the per-client rolling request limit behind an API gateway, with exact time boundaries and useful retry guidance.

Task

Build a request-quota service that decides which requests fit within each client’s rolling time window.

Requirements

  1. Input requests have a unique id, a client_id, and a nonnegative integer at (milliseconds), in nondecreasing time order. limit and window_ms must be positive integers, not booleans. Raise ValueError containing limit, window, or time for invalid values or order.
  2. Return one {id, allowed, remaining, retry_after_ms} per request. Quotas are separate for each client_id.
  3. Only accepted requests in the interval (at - window_ms, at] count. An accepted request consumes one slot; a denied request does not consume a slot.
  4. For a denied request, remaining is 0 and retry_after_ms is the earliest accepted timestamp + window_ms - at. For an accepted request, retry_after_ms is 0. Do not mutate input. This is a single-process model, not a distributed gateway.

Exercises

AI-assisted Coding9 exercises