Repository navigation
Per-customer quotas in a middleware in front of DRF: three surprises from running before DRF does #10098
supertj
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
DRF's throttles are a good fit for "at most N requests per minute per user". We needed something slightly different for a paid API: each customer's limits come from their plan, there's a quota per billing period on top of the rate limit, and every request (or every token, for model endpoints) has to be counted toward that customer's usage. We do that in a plain Django middleware, which means it runs before DRF does. That ordering is where most of the surprises came from.
The setup is the middleware plus a
track()call in the view. For this word-count example, the app is set to count units (Settings > What counts as usage in the console); in an app that counts requests,track()does nothing.The three things:
1. The middleware runs before DRF authenticates. With
SessionAuthenticationthat's fine, because Django'sAuthenticationMiddlewarehas already setrequest.user. WithTokenAuthentication,request.useris still anonymous when the middleware asks who the caller is, so every request looks anonymous: no rate limit, no quota, no usage counted for anyone. The fix is to call the authentication class yourself in the function that identifies the customer:The cost is a second token lookup per request, once here and once when DRF authenticates the view.
2. Refused requests never reach DRF. A 429 for a customer over their limit is answered by the middleware, so DRF's exception handler and renderers don't see it. If your API has a custom error format, set it in the middleware's
on_rejectand copyrejection.headersonto the response soRetry-Aftersurvives.3. Router URLs are regular expressions. Usage is grouped by the URL pattern that matched. Routers build their patterns as regexes, so with
itemsregistered on aDefaultRouterunderpath("v1/", include(router.urls)), the detail route is recorded as/v1/items/(?P<pk>[^/.]+)/$, and format suffixes such as/v1/items/7.jsonget a pattern of their own. If you give a route a weight (say, an expensive endpoint counts as 10), the weight has to match that exact string.Also worth knowing if you use UsageRail's own keys (
"auth": "api-key") instead: clients send them asAuthorization: Bearer <key>orx-api-key. TheAuthorization: Token <key>form that DRF'sTokenAuthenticationuses isn't read, and those requests get a 401.Disclosure: I work on UsageRail. The Django page has a DRF section with the rest of the details: https://usagerail.com/docs/django#django-rest-framework
All reactions