-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Merged
Changes from 5 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,88 @@ | ||
| import * as NodeOS from "node:os"; | ||
| import type { HostResourcesSnapshot } from "@t3tools/contracts"; | ||
| import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; | ||
| import * as Context from "effect/Context"; | ||
| import * as DateTime from "effect/DateTime"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as FileSystem from "effect/FileSystem"; | ||
| import * as Layer from "effect/Layer"; | ||
| import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; | ||
|
|
||
| export class HostResources extends Context.Service< | ||
| HostResources, | ||
| { readonly read: Effect.Effect<HostResourcesSnapshot> } | ||
| >()("t3/resourceTelemetry/HostResources") {} | ||
|
|
||
| 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 }; | ||
| } | ||
|
|
||
| function darwinAvailableMemory(output: string): number | null { | ||
| const pageSize = /page size of (\d+) bytes/.exec(output)?.[1]; | ||
| const free = /^Pages free:\s+(\d+)\./m.exec(output)?.[1]; | ||
| const inactive = /^Pages inactive:\s+(\d+)\./m.exec(output)?.[1]; | ||
| const speculative = /^Pages speculative:\s+(\d+)\./m.exec(output)?.[1]; | ||
| if (!pageSize || !free || !inactive || !speculative) return null; | ||
| // vm_stat subtracts speculative pages from its printed "Pages free" count. | ||
| // Adding them here counts each reclaimable page once; purgeable pages overlap. | ||
| const available = (Number(free) + Number(inactive) + Number(speculative)) * Number(pageSize); | ||
| return Number.isSafeInteger(available) && Number(pageSize) > 0 ? available : null; | ||
| } | ||
|
|
||
| export const make = Effect.fn("makeHostResources")(function* () { | ||
| const fs = yield* FileSystem.FileSystem; | ||
| const platform = yield* HostProcessPlatform; | ||
| const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; | ||
|
|
||
| const sample = Effect.fn("HostResources.sample")(function* () { | ||
| const previousCpu = readCpu(); | ||
| // CPU counters need two readings; idle servers do no polling or process scans. | ||
| yield* Effect.sleep("200 millis"); | ||
| 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(); | ||
| // On Windows libuv returns GlobalMemoryStatusEx.ullAvailPhys, including standby memory. | ||
| let availableMemoryBytes = NodeOS.freemem(); | ||
| if (platform === "linux") { | ||
| const meminfo = yield* fs | ||
| .readFileString("/proc/meminfo") | ||
| .pipe(Effect.catch(() => Effect.succeed(""))); | ||
| const available = /^MemAvailable:\s+(\d+)\s+kB$/m.exec(meminfo)?.[1]; | ||
| if (available) availableMemoryBytes = Number(available) * 1024; | ||
| } else if (platform === "darwin") { | ||
| const output = yield* spawner | ||
| .string(ChildProcess.make("/usr/bin/vm_stat", [], { stdin: "ignore", stderr: "ignore" })) | ||
| .pipe( | ||
| Effect.timeout("1 second"), | ||
| Effect.catch(() => Effect.succeed("")), | ||
| ); | ||
| availableMemoryBytes = darwinAvailableMemory(output) ?? availableMemoryBytes; | ||
| } | ||
| return { | ||
| sampledAt: DateTime.toEpochMillis(yield* DateTime.now), | ||
| cpuUtilization, | ||
| cpuCount: cpu.count, | ||
| availableMemoryBytes: Math.min(totalMemoryBytes, Math.max(0, availableMemoryBytes)), | ||
| totalMemoryBytes, | ||
| }; | ||
| }); | ||
|
|
||
| // One server-lifetime cache deduplicates simultaneous requests from all sockets. | ||
| const read = yield* Effect.cachedWithTTL(sample(), "5 seconds"); | ||
| return HostResources.of({ read }); | ||
| }); | ||
|
|
||
| export const layer = Layer.effect(HostResources, make()); | ||
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.