Files
paseo/packages/server/src/utils/git-process-scheduler.ts
T
Mohamed Boudra 2e188787ff Limit daemon-wide Git process pressure (#2797)
* perf(server): bound Git process starts

Git refresh bursts could keep creating short-lived processes at the concurrency ceiling, causing sustained process churn on constrained hosts. Apply one daemon-global start-rate and concurrency policy to every Git command while preserving the legacy concurrency environment variable.

* build(nix): update npm dependency hash

* fix(server): hold Git slots until process exit

* fix(server): enforce Git limits across process lifetime

* fix(server): align Git metrics with process exit

* Fix Git limiter test deadlocks

* Tune default Git process rate
2026-08-02 19:59:43 +02:00

89 lines
2.4 KiB
TypeScript

import pLimit from "p-limit";
import pThrottle from "p-throttle";
export interface GitProcessPolicy {
maxProcessesPerSecond: number;
maxProcessConcurrency: number;
}
export const DEFAULT_GIT_PROCESS_POLICY: GitProcessPolicy = {
maxProcessesPerSecond: 64,
maxProcessConcurrency: 8,
};
export interface ScheduledGitProcess<T> {
result: Promise<T>;
exited: Promise<void>;
}
function parsePositiveInteger(value: string | undefined): number | undefined {
if (value === undefined) {
return undefined;
}
const parsed = Number(value);
return Number.isInteger(parsed) && parsed > 0 ? parsed : undefined;
}
export function resolveGitProcessPolicy(input: {
env: NodeJS.ProcessEnv;
persisted?: Partial<GitProcessPolicy>;
}): GitProcessPolicy {
return {
maxProcessesPerSecond:
parsePositiveInteger(input.env.PASEO_GIT_MAX_PROCESSES_PER_SECOND) ??
input.persisted?.maxProcessesPerSecond ??
DEFAULT_GIT_PROCESS_POLICY.maxProcessesPerSecond,
maxProcessConcurrency:
parsePositiveInteger(input.env.PASEO_GIT_MAX_PROCESS_CONCURRENCY) ??
// COMPAT(gitConcurrencyEnv): renamed in v0.2.6; remove after 2027-02-02.
parsePositiveInteger(input.env.PASEO_GIT_CONCURRENCY) ??
input.persisted?.maxProcessConcurrency ??
DEFAULT_GIT_PROCESS_POLICY.maxProcessConcurrency,
};
}
export class GitProcessScheduler {
private readonly limit;
private readonly throttle;
private running = 0;
private waiting = 0;
constructor(readonly policy: GitProcessPolicy) {
this.limit = pLimit(policy.maxProcessConcurrency);
this.throttle = pThrottle({
limit: policy.maxProcessesPerSecond,
interval: 1_000,
strict: true,
});
}
get activeCount(): number {
return this.running;
}
get pendingCount(): number {
return this.waiting;
}
run<T>(start: () => ScheduledGitProcess<T>): Promise<T> {
this.waiting += 1;
return new Promise<T>((resolve, reject) => {
void this.limit(
this.throttle(async () => {
this.waiting = Math.max(0, this.waiting - 1);
this.running += 1;
try {
const process = start();
void process.result.then(resolve, reject);
await process.exited;
} catch (error) {
reject(error);
} finally {
this.running = Math.max(0, this.running - 1);
}
}),
).catch(reject);
});
}
}