Skip to content

Commit 17c7ec0

Browse files
committed
perf(server): usage-limit sweeps read on the reader thread, off the event loop
1 parent 05ccf1c commit 17c7ec0

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

‎apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type OrchestrationV2Command,
66
} from "@t3tools/contracts";
77
import { modelSelectionsEqual } from "@t3tools/shared/model";
8+
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
89
import * as DateTime from "effect/DateTime";
910
import * as Effect from "effect/Effect";
1011
import * as Layer from "effect/Layer";
@@ -207,12 +208,16 @@ const makeSweep = Effect.gen(function* () {
207208
const now = yield* DateTime.now;
208209
// A thread scheduled to resume continues early once claude-swap moves its login.
209210
const loginMayMove = preferences.autoResumeLimitedThreads && preferences.claudeSwapUsageEnabled;
210-
const candidates = yield* projections.getLimitRecoveryCandidates({
211-
now,
212-
autoResume: preferences.autoResumeLimitedThreads,
213-
snooze: preferences.snoozeLimitedThreads,
214-
beforeReset: fallbackEnabled || loginMayMove,
215-
});
211+
// Both scans run every scheduler tick and read every thread. On the writer they
212+
// run on the event loop, and a cold page cache made one take seconds.
213+
const candidates = yield* projections
214+
.getLimitRecoveryCandidates({
215+
now,
216+
autoResume: preferences.autoResumeLimitedThreads,
217+
snooze: preferences.snoozeLimitedThreads,
218+
beforeReset: fallbackEnabled || loginMayMove,
219+
})
220+
.pipe(NodeSqliteClient.readOnly);
216221
const nowMs = DateTime.toEpochMillis(now);
217222
for (const thread of candidates) {
218223
// The fallback is the last resort, so it wins over waiting for the reset.
@@ -235,7 +240,9 @@ const makeSweep = Effect.gen(function* () {
235240
yield* dispatch(thread, command);
236241
}
237242
// Runs even with the fallback turned off, so threads it already moved still come back.
238-
const returns = yield* projections.getLimitFallbackReturnCandidates();
243+
const returns = yield* projections
244+
.getLimitFallbackReturnCandidates()
245+
.pipe(NodeSqliteClient.readOnly);
239246
if (returns.length === 0 || !(yield* usageLimitSources.claudeSwapHasRoom)) return;
240247
for (const thread of returns) {
241248
const command = limitFallbackReturnCommand(thread);

0 commit comments

Comments
 (0)