Repository navigation
perf(api): the sync freshness poll rides along the request's D1 batch - #150
Merged
Merged
Conversation
Every 15 s per isolate the API worker polls _sync_meta to invalidate L1. It ran at the top of the request as a separate D1 query racing the request's own: if the D1 session/binding keeps one query in flight, the request waited out a full extra round trip (~20 ms) on every poll. The poll is now claimed right before dispatch and attached to the request's first D1 call through the per-request session wrapper (core/d1stats.js riders): the same batch() carries it, so it costs no round trip and never competes with the request's query. Its rows go to the poll's apply step, its meta doesn't count toward the request's Server-Timing d1. A request that never reaches D1 (L1/L2 hit) runs the claimed poll after the response, on the bare session. The REST worker keeps ensureSyncFreshness unchanged. core/sync_state.js splits refresh() into the poll statement, applyPoll() and claimPoll()/runPoll(); the SQLite test shim counts batched reads like all()/first(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Why
Every 15 s per isolate, the API worker polls
_sync_metato invalidate L1. The poll ran at the top of the request as a separate D1 query, alongside the request's own. If the D1 session or binding keeps only one query in flight, the request waited out a full extra round trip (~20 ms) whenever the poll fired.Change
claimSyncPoll) and hands it to the per-request session wrapper (db.carry). The wrapper sends it in the samebatch()as the request's first D1 call, whether that'sall(),first()orbatch(). It costs no round trip and never competes with the request's query.Server-Timingd1.ctx.waitUntilon the bare session.core/sync_state.js:refresh()is split into the poll statement,applyPoll(), andclaimPoll()/runPoll().ensureSyncFreshnessunchanged.Tests
tests/unit/core/d1stats.test.js(riders):first()/all()send the rider in the same batch, its rows reachapplyand not the caller, and its metadata isn't counted;batch()appends it and strips its result;takeRider()hands back an unsent rider;applydoesn't fail the request.tests/unit/api/sync_poll_rider_sqlite.test.js(end to end, mocked clock):all()/first().🤖 Generated with Claude Code