Title: improvement(db): cut hot-path connector count and rate-limit lock costs by waleedlatif1 · Pull Request #8944 · simstudioai/sim · GitHub
Open Graph Title: improvement(db): cut hot-path connector count and rate-limit lock costs by waleedlatif1 · Pull Request #8944 · simstudioai/sim
X Title: improvement(db): cut hot-path connector count and rate-limit lock costs by waleedlatif1 · Pull Request #8944 · simstudioai/sim
Description: Summary DbTokenBucket.consumeTokensAtomically now locks, refills, decides and spends every dimension in one CTE statement. Before, the bucket rows stayed locked across a separate UPDATE round trip per dimension plus the commit, and each waiter on a hot provider key queued behind that whole hold (waits ran 1.1–1.7 s with about 280 ms per holder). Now the lock is held for one statement plus the commit. The token arithmetic is the same float8 math consumeTokens does in JS, with refills on whole intervals. Refill and cooldowns are judged once every lock is held, as before. The statement advances the caller's clock by the lock wait it measures (clock_timestamp() sampled before the first row can reach the lock and again after a count over every locked row), so an uncontended admission decides at exactly the caller clock, so a contended waiter still refills the time it waited, and every stored instant stays on the application clock. The JS reader cross-checks the statement's own decision and spent count. On a mismatch, a vanished bucket, an abort, or a deadline passed after the statement, it throws inside the transaction so the spend rolls back. Once a connector has finished a pass (lastSyncAt set, which unfinished slices never advance), a content-pass slice that leaves the next pass unfinished no longer runs the full live document count. It keeps the count from that finished pass, which is the pass lastSyncAt still names. Until the first pass finishes, every slice is still counted, so a first sync shows progress. Finished passes count exactly as before, so the mass-deletion guard floor and the incremental listedCount are unchanged. Type of Change Performance improvement Testing provider-admission.integration.ts against real Postgres and Redis (bun run test:integration provider-admission): 20/20 pass. That includes the 40-way concurrent reversed-order admission, cooldown, cancel/expiry and partial-bucket cases, plus new cases: fractional refill on the atomic path; a waiter held behind another connection's row lock across a refill interval, which sees the refill and a wait measured from lock acquisition; and a spend rolled back when the deadline passes or the caller aborts while the statement runs. 23/23 pass. Each new case goes red with its guard removed (lock-wait clock, post-statement deadline check, post-statement abort check). Breaking the whole-interval refill anchoring turns the fractional case and 3 existing atomic cases red. lib/core/rate-limiter/** and sync-engine.test.ts: 221 pass. The new unfinished-slice count cases go red when the skip is reverted, and the first-pass later-slice case goes red without the lastSyncAt gate. Biome on the changed files. Type-check and check:audits are left to CI. Checklist Code follows project style guidelines Self-reviewed my changes Tests added/updated and passing (new tests pass the test-audit authoring gate) No new warnings introduced I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)
Open Graph Description: Summary DbTokenBucket.consumeTokensAtomically now locks, refills, decides and spends every dimension in one CTE statement. Before, the bucket rows stayed locked across a separate UPDATE round trip...
X Description: Summary DbTokenBucket.consumeTokensAtomically now locks, refills, decides and spends every dimension in one CTE statement. Before, the bucket rows stayed locked across a separate UPDATE round trip...
Opengraph URL: https://github.com/simstudioai/sim/pull/8944
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:ec91588b-41b5-984d-6625-e37e0f7afe42 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | EBF8:2B8D95:7DA9C5:B2ADCD:6AFB4D9E |
| html-safe-nonce | 7bf270eb55632336a546e31fdf4b5490ab1b7a5b5473dcf4c2959161c6a58f02 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQkY4OjJCOEQ5NTo3REE5QzU6QjJBRENEOjZBRkI0RDlFIiwidmlzaXRvcl9pZCI6IjQ4NDIwNTYyNjc3MDQ1MjAwOTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b2761677c3a66f496a280964becbd2f2fdd9a1fee0c580e09c5ad4d007bbcb6c |
| hovercard-subject-tag | pull_request:4835154756 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/simstudioai/sim/pull/8944/files |
| twitter:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| og:image:alt | Summary DbTokenBucket.consumeTokensAtomically now locks, refills, decides and spends every dimension in one CTE statement. Before, the bucket rows stayed locked across a separate UPDATE round trip... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | acf7616d06cc051644705b379878c9bb13dc5e70ecf4ffd9b34140f8ac01d7e1 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/simstudioai/sim git https://github.com/simstudioai/sim.git |
| octolytics-dimension-user_id | 199344406 |
| octolytics-dimension-user_login | simstudioai |
| octolytics-dimension-repository_id | 912559512 |
| octolytics-dimension-repository_nwo | simstudioai/sim |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 912559512 |
| octolytics-dimension-repository_network_root_nwo | simstudioai/sim |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 26504db649b58525278f77a52b80ba5f10d5f4de |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width