Skip to content

fix(core): measure connection rates from recent samples and share the global limit per task - #572

Merged
linroid merged 2 commits into
mainfrom
fix/live-rates
Oct 17, 2026
Merged

linroid merged 2 commits into
mainfrom
fix/live-rates

Conversation

@linroid

@linroid linroid commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Two rate problems in library/core, found by the field tests of #570.

Connection speeds were lifetime averages

GET /api/connections reads one snapshot with .first(). The registry's samples are shared WhileSubscribed(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.

  • Each connection now keeps its last two sampled points on its registry entry, not in the sampler. A collection that starts within a few seconds of another (a GET right after another, or after the Connections page closed) measures from those points and answers at once.
  • A collection with nothing recent to measure some connection from notes every connection's counters and sends its first sample one interval (1 s) later. An idle connection then reads 0, and an active one its rate over that second. Connections opened in the last few seconds are still measured from their opening, as before.

The global speed limit was shared by calls, not by tasks

TokenBucket.acquire let 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).

  • TokenBucket now queues waiters by owner (SpeedLimiter.acquire(bytes, owner); the plain acquire is 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.
  • An owner that stops waiting gives up its turns rather than saving them: it comes back no further behind than the turn being served. One owner's callers are served in the order they came.
  • Only each owner's first waiter keeps a timer; the others sleep until they are first or granted.
  • DownloadExecution charges the global limiter with the task ID as owner. Per-task limiters keep a single owner.
  • Buckets read a monotonic TimeSource instead of Clock.System, and keep fractional tokens. The new tests run on virtual time.

Measured in TokenBucketFairnessTest, on virtual time:

Case Expected Measured
1 caller vs 10, 16 KiB requests 0.50 0.498
1 caller with 1 KiB requests vs 10 with 16 KiB 0.50 0.505
3 owners with 1, 5 and 9 callers 0.333 each 0.332–0.335
Owner joining after 10 s, its first second 0.50 0.51
Total over 20 s at 800 kB/s 16.0 MB 16.06 MB (burst included)

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:test pass (593 and 221 tests). The new tests also pass on jsNodeTest. The torrent limit tests pass (MixedDownloadLimitsTest, TorrentActiveLimitTest, TorrentPublicV2WorkflowTest). Core tests compile for iOS and WasmWasi.
  • :library:core:wasmWasiNodeTest crashes in awaitFlag_singleFile_downloadsWithoutWaiting with "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.md describe both behaviors.

… 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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-17T11:06:08.921698Z 1c46b5d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@claude

claude Bot commented Oct 17, 2026

Copy link
Copy Markdown

Code review

No 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.
@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 1 966 files  + 4   1 994 suites  +4   23m 2s ⏱️ + 1m 14s
13 242 tests +52  12 981 ✅ +52  261 💤 ±0  0 ❌ ±0 
15 945 runs  +52  15 684 ✅ +52  261 💤 ±0  0 ❌ ±0 

Results for commit 1096a6f. ± Comparison against base commit f168285.

@linroid
linroid merged commit 22cfd50 into main Oct 17, 2026
14 checks passed
@linroid
linroid deleted the fix/live-rates branch October 17, 2026 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant