Repository navigation
Conversation
At the moment it has infinite quota, since we haven't defined quotas for this. It records usage into clickhouse, to understand how much each token user consumes
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 2045-create-token-models #1336 +/- ##
============================================================
+ Coverage 94.03% 94.06% +0.03%
============================================================
Files 77 77
Lines 10038 10140 +102
Branches 599 616 +17
============================================================
+ Hits 9439 9538 +99
- Misses 471 474 +3
Partials 128 128
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| try: | ||
| insert_click( | ||
| db, | ||
| "INSERT INTO api_token_usage " |
There was a problem hiding this comment.
note: we'll need to make the oonimeasurements user readonly=0 (or add another clickhouse user) and add a grant for this table
aagbsn
left a comment
There was a problem hiding this comment.
looks good. just a few issues - failed authentication requests skip the per-IP limits, we need to remember to change the oonimeasurements db user settings to allow writes or add another user for this table grant, and using an async driver for postgres avoids sharing the threadpool with clickhouse (which isn't so straightforward to change)
| log.exception("Failed to look up API token") | ||
| API_TOKEN_LOOKUP_ERRORS.inc() | ||
| response = JSONResponse({"error": "could not validate token"}, 503) | ||
| return await response(scope, receive, send) |
There was a problem hiding this comment.
add consume_rate_limits on failed authentication
| if entry is None or not entry.is_valid(): | ||
| API_TOKEN_REJECTED.inc() | ||
| response = JSONResponse({"error": "invalid token"}, 401) | ||
| return await response(scope, receive, send) |
There was a problem hiding this comment.
consume_rate_limits like above at this exit point
| # would make the load balancer take down the API for everyone | ||
| warnings = [] | ||
| try: | ||
| await run_in_threadpool(check_postgresql, settings.postgresql_url) |
There was a problem hiding this comment.
consider using create_async_engine to avoid threadpool starvation
| # workers. | ||
| # pool_pre_ping replaces dead connections | ||
| self.engine = create_engine( | ||
| postgresql_url, pool_pre_ping=True, connect_args={"connect_timeout": 2} |
There was a problem hiding this comment.
add statement_timeout, pool_timeout, keepalives_idle
- a hung query can otherwise block the API
use create_async_engine - run_in_threadpool stays blocked until socket times out on a hung connection
| return entry | ||
|
|
||
| try: | ||
| row = await run_in_threadpool(self._fetch, token_hash) |
There was a problem hiding this comment.
see above re: create_async_engine
This PR implements token based rate limiting for the API, for users providing a token. For most users the API stays the same as always has been. We haven't defined a quota for token users, so for now they have free access, but this PR aims to leave everything in place for creating these quotas.
This builds upon #1335
/healthchecks Postgres health, but it only triggers a warning if it fails to avoid taking down the API for most usersDeployment
This PR requires some infrastructure changes before being deployed:
api_token_usagetable to Clickhouse, make sure this user is able to setasync_insertandwait_for_async_insertpostgres_urlfor oonimeasurements