Repository navigation
feat(connections): balance new threads across connected machines #9895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
maria-rcks
merged 12 commits into
pingdotgg:main
from
maria-rcks:t3code/connection-load-balancing
Sep 6, 2026
+865
−22
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3529983
feat(connections): balance new threads across connected machines
maria-rcks 1fdd6da
fix(connections): preserve settings and clock compatibility
maria-rcks 6123ffa
fix(connections): recheck balancing when drafts change project
maria-rcks 6a60282
fix(connections): preserve attachments and allow available providers
maria-rcks d3eb4c3
fix(connections): respect saved preferences and reclaimable memory
maria-rcks f3c6825
fix(connections): recover resource sampling after cancellation
maria-rcks 93b7303
fix(connections): give auto balance its own name and icon
maria-rcks 1e4f0a4
chore: resolve main conflicts for connection balancing
maria-rcks 9745014
fix(connections): replace load sliders with named preferences
maria-rcks 775c89f
fix(connections): wait for saved preferences before editing
maria-rcks 4cfe8bf
chore: preserve both client runtime exports from main
maria-rcks 61423c5
fix(connections): make auto balance opt-in
maria-rcks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import * as NodeOS from "node:os"; | ||
| import * as NodeTimersPromises from "node:timers/promises"; | ||
| import type { HostResourcesSnapshot } from "@t3tools/contracts"; | ||
| import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; | ||
| import * as DateTime from "effect/DateTime"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as FileSystem from "effect/FileSystem"; | ||
|
|
||
| const CACHE_MS = 5_000; | ||
|
|
||
| // All sockets share the sample. Idle servers do no polling or process scans. | ||
| let latest: HostResourcesSnapshot | undefined; | ||
| let pending: Promise<HostResourcesSnapshot> | undefined; | ||
|
|
||
| function readCpu() { | ||
| const cpus = NodeOS.cpus(); | ||
| const cpu = cpus.reduce( | ||
| (sum, { times }) => ({ | ||
| idle: sum.idle + times.idle, | ||
| total: sum.total + times.user + times.nice + times.sys + times.idle + times.irq, | ||
| }), | ||
| { idle: 0, total: 0 }, | ||
| ); | ||
| return { ...cpu, count: cpus.length }; | ||
| } | ||
|
|
||
| async function sample( | ||
| sampledAt: number, | ||
| platform: NodeJS.Platform, | ||
| readMeminfo: () => Promise<string>, | ||
| ): Promise<HostResourcesSnapshot> { | ||
| const previousCpu = readCpu(); | ||
| // CPU counters need two readings; a bounded demand-only sample also works on Windows. | ||
| await NodeTimersPromises.setTimeout(200); | ||
| const cpu = readCpu(); | ||
| const totalDelta = cpu.total - previousCpu.total; | ||
| const idleDelta = cpu.idle - previousCpu.idle; | ||
| const cpuUtilization = | ||
| previousCpu.count === cpu.count && totalDelta > 0 && idleDelta >= 0 | ||
| ? Math.min(1, Math.max(0, 1 - idleDelta / totalDelta)) | ||
| : null; | ||
| const totalMemoryBytes = NodeOS.totalmem(); | ||
| let availableMemoryBytes = NodeOS.freemem(); | ||
| if (platform === "linux") { | ||
| const meminfo = await readMeminfo(); | ||
| const available = /^MemAvailable:\s+(\d+)\s+kB$/m.exec(meminfo)?.[1]; | ||
| if (available) availableMemoryBytes = Number(available) * 1024; | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
| latest = { | ||
| sampledAt, | ||
| cpuUtilization, | ||
| cpuCount: cpu.count, | ||
| availableMemoryBytes: Math.min(totalMemoryBytes, Math.max(0, availableMemoryBytes)), | ||
| totalMemoryBytes, | ||
| }; | ||
| return latest; | ||
| } | ||
|
|
||
| export const readHostResources = Effect.fn("HostResources.read")(function* () { | ||
| const sampledAt = DateTime.toEpochMillis(yield* DateTime.now); | ||
| if (latest && sampledAt >= latest.sampledAt && sampledAt - latest.sampledAt < CACHE_MS) { | ||
| return latest; | ||
| } | ||
| const fs = yield* FileSystem.FileSystem; | ||
| const platform = yield* HostProcessPlatform; | ||
| return yield* Effect.promise(() => { | ||
| pending ??= sample(sampledAt, platform, () => | ||
| Effect.runPromise( | ||
| fs.readFileString("/proc/meminfo").pipe(Effect.catch(() => Effect.succeed(""))), | ||
| ), | ||
| ).finally(() => { | ||
| pending = undefined; | ||
| }); | ||
| return pending; | ||
| }); | ||
| }); | ||
|
maria-rcks marked this conversation as resolved.
Outdated
|
||
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.