StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 10 · Middleware, Filters and Error Handling

Api Rate Limiter

medium—No data yet

Problem statement

Complete the implementation of LookupRateLimiting, and apply its policy to the public airport lookup, so that a client calling that lookup too often inside one window is turned away instead of served.

Sample Data

The airport directory is pre-seeded with five airports, each carrying a code, a name, a city and a country:

{ "code": "AMS", "name": "Schiphol", "city": "Amsterdam", "country": "Netherlands" }

The other four codes are BLQ, CPH, DUB and EDI.

Configuration carries the allowance:

"RateLimiting": {
  "PermitLimit": 4,
  "WindowMilliseconds": 60000
}

Service

LookupRateLimiting

AddLookupRateLimiting registers the rate limit policy and UseLookupRateLimiting puts it into the request pipeline. Both are already called during startup, so only their bodies are unwritten.

Register one fixed window policy, named lookup:

  • Its allowance is RateLimiting:PermitLimit requests and its window is RateLimiting:WindowMilliseconds milliseconds long. Both are read from configuration rather than fixed in code.
  • Requests are counted per client. A client is the value of the X-Client-Id request header, and a request carrying no such header, or a blank one, is counted as anonymous. Every client has its own allowance and its own window.
  • The first PermitLimit requests a client makes inside a window reach the endpoint.
  • Every further request from that client inside that window is refused with 429. It is refused straight away rather than queued, and it never reaches the endpoint.
  • Once the window has elapsed, that client's full allowance is back.

A refused request is answered as application/json with this body:

{ "message": "Rate limit exceeded.", "client": "atlas-maps", "limit": 4 }

client is the client the request was counted under, and limit is the configured PermitLimit.

AirportLookupController

Apply the lookup policy to GET /lookup/airports/{code}, and to nothing else.

  • Every public lookup counts against one allowance whichever code it asks for, and a lookup that finds no airport spends a request just as a lookup that finds one does.
  • The allowance belongs to that action rather than to the path it sits under. GET /lookup/airports carries no code, so it reaches no action, answers 404, and is counted against nothing. POST /lookup/airports/AMS is not a call that action answers, so it reaches no action either, answers 405, and is counted against nothing.
  • GET /ops/airports/{code} reads the same directory and is never refused, however often it is called, and calls to it never spend a client's public allowance.
Sample Requests & Responses

With PermitLimit set to 2, GET /lookup/airports/AMS carrying X-Client-Id: atlas-maps:

Response code: 200

{
  "code": "AMS",
  "name": "Schiphol",
  "city": "Amsterdam",
  "country": "Netherlands"
}

The same call a second time answers the same way. The third one, still inside the window:

Response code: 429

{
  "message": "Rate limit exceeded.",
  "client": "atlas-maps",
  "limit": 2
}

GET /lookup/airports/ZZZ answers 404 with a message of Airport ZZZ was not found., and still spends one of the two requests.

More in Middleware, Filters and Error Handling

See the module
Endpoint Validation FilterThis problem covers checking an incoming booking once, in one reusable place attached to a whole set of related routes, so that a bad request is turned away with a clear list of what was wrong before any of the code behind those routes runs.—No data yetmediumGlobal Exception HandlerThis problem covers turning every failure the application raises into one clear answer that says what went wrong and nothing about how the application is built.—No data yetmediumHardened Public API PipelineThis problem covers putting the guards of a public API together in one working order, so that bad input is turned away before any work starts, callers who ask too often are held back, failures come back as a tidy document that gives nothing away, and every answer can be traced back to the request that caused it.—No data yetmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.