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.
