Repository navigation
fix(core): measure connection rates from recent samples and share the global limit per task - #572
Merged
Merged
Conversation
… global limit per task GET /api/connections reported a connection's average since it opened as its speed: each one-shot read started a new sampler, whose first sample measured from the connection's opening. Each connection now keeps its last two samples, so a collection that starts soon after another measures from them, and one with nothing recent to measure from notes the counters and sends its first sample one interval later, so an idle connection reads 0. The global speed limit was shared by calls rather than by tasks: every waiter polled and took what had accumulated, so a task waiting with ten coroutines (a v1 torrent's peers) took ten times the share of one waiting with one (a v2 session). With one 800 kB/s limit a one-caller task got 9% beside a ten-caller task. TokenBucket now queues waiters by owner and serves them in 16 KiB turns by start-time fair queuing; the global limiter's owner is the task, so tasks share it equally by bytes. Buckets read a monotonic TimeSource, so the tests run on virtual time.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
The small-requests case simulated about 8,800 turns, which took close to Mocha's 2 s limit locally and passed it on CI. A 200 kB/s rate and shorter runs cut the turns about eightfold; the shares measured stay within 2% of equal.
Contributor
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.
Two rate problems in
library/core, found by the field tests of #570.Connection speeds were lifetime averages
GET /api/connectionsreads one snapshot with.first(). The registry's samples are sharedWhileSubscribed(replayExpirationMillis = 0), so each such read started a fresh sampler, whose first sample measured every connection from its opening. An idle seeding connection showed its average since it opened, such as 105 KB/s.The global speed limit was shared by calls, not by tasks
TokenBucket.acquirelet each waiter poll at least every 50 ms and take whatever had built up. A task's share was therefore roughly its number of concurrent waiters. A v1 torrent charges once per peer worker; a v2 session charges through one coroutine.A unit test reproduced it first. With one 800 kB/s bucket, 10 coroutines of one owner and 1 of another, the single one got 9.1% (1/11).
TokenBucketnow queues waiters by owner (SpeedLimiter.acquire(bytes, owner); the plainacquireis one owner). It serves them in turns of at most 16 KiB by start-time fair queuing: each turn goes to the waiting owner granted the fewest bytes. Owners therefore share equally by bytes, however many coroutines each waits with and however small its requests.DownloadExecutioncharges the global limiter with the task ID as owner. Per-task limiters keep a single owner.TimeSourceinstead ofClock.System, and keep fractional tokens. The new tests run on virtual time.Measured in
TokenBucketFairnessTest, on virtual time:Tests
ConnectionRegistryTest: an idle connection read by a fresh collection reports 0. A fresh collection measures over its warm-up second. A collection soon after another answers at once, measuring from the earlier points. A new connection is measured from its opening at once. The first three fail on main.TokenBucketFairnessTest: the table above, plus requests larger than the burst being served in turns,updateRate(0)releasing every waiter, and a cancelled first waiter letting the next through../gradlew :library:core:jvmTest :library:server:testpass (593 and 221 tests). The new tests also pass onjsNodeTest. The torrent limit tests pass (MixedDownloadLimitsTest,TorrentActiveLimitTest,TorrentPublicV2WorkflowTest). Core tests compile for iOS and WasmWasi.:library:core:wasmWasiNodeTestcrashes inawaitFlag_singleFile_downloadsWithoutWaitingwith "dereferencing a null pointer". It crashes the same way on the base commit, so it is not caused by this change.AGENTS.md and
docs/architecture.mddescribe both behaviors.