refactor(compute-core): centralize runtime cpu config exports

This commit is contained in:
Richard R 2026-05-21 20:54:22 -06:00
parent 51fbfa117d
commit 754fbee227
7 changed files with 20 additions and 31 deletions

View file

@ -15,6 +15,7 @@
".": "./src/index.ts",
"./contracts": "./src/contracts.ts",
"./local-runtime": "./src/local-runtime.ts",
"./runtime": "./src/runtime/index.ts",
"./runtime/timeout-config": "./src/runtime/timeout-config.ts",
"./pdf-layout/*": "./src/pdf-layout/*.ts"
}

View file

@ -1,6 +1,6 @@
import path from 'path';
export const DOCSTORE_DIR = process.env.COMPUTE_DOCSTORE_DIR?.trim() || path.join(process.cwd(), 'docstore');
export const DOCSTORE_DIR = path.join(process.cwd(), 'docstore');
export function getDocstoreDir(): string {
return DOCSTORE_DIR;

View file

@ -0,0 +1,15 @@
export {
getComputeJobConcurrency,
getAvailableCpuCores,
getOnnxThreadsPerJob,
} from './cpu-budget';
export {
getComputeTimeoutConfig,
getWorkerClientWaitTimeoutMs,
withTimeout,
withIdleTimeoutAndHardCap,
type ComputeTimeoutConfig,
type ComputeOperationKind,
type IdleTimeoutAndHardCapInput,
} from './timeout-config';

View file

@ -1,4 +1,4 @@
import { getComputeJobConcurrency } from '@/lib/server/compute/cpu-budget';
import { getComputeJobConcurrency } from '@openreader/compute-core/runtime';
export class ConcurrencyLimiter {
private readonly maxInFlight: number;

View file

@ -1,28 +0,0 @@
import os from 'node:os';
function readPositiveInt(name: string, fallback: number): number {
const raw = process.env[name]?.trim();
if (!raw) return fallback;
const parsed = Number(raw);
if (!Number.isFinite(parsed) || parsed <= 0) return fallback;
return Math.floor(parsed);
}
export function getComputeJobConcurrency(): number {
return readPositiveInt('COMPUTE_JOB_CONCURRENCY', 1);
}
export function getAvailableCpuCores(): number {
if (typeof os.availableParallelism === 'function') {
const value = os.availableParallelism();
if (Number.isFinite(value) && value >= 1) return Math.floor(value);
}
const fallback = os.cpus().length;
return Number.isFinite(fallback) && fallback >= 1 ? Math.floor(fallback) : 1;
}
export function getOnnxThreadsPerJob(): number {
const concurrency = getComputeJobConcurrency();
const usableCores = Math.max(1, getAvailableCpuCores() - 1);
return Math.max(1, Math.floor(usableCores / concurrency));
}

View file

@ -8,7 +8,7 @@ import { Tokenizer } from '@huggingface/tokenizers';
import JSZip from 'jszip';
import type { TTSAudioBuffer, TTSAudioBytes, TTSSentenceAlignment } from '@/types/tts';
import { getFFmpegPath } from '@/lib/server/audiobooks/ffmpeg-bin';
import { getOnnxThreadsPerJob } from '@/lib/server/compute/cpu-budget';
import { getOnnxThreadsPerJob } from '@openreader/compute-core/runtime';
import {
mapWordsToSentenceOffsets,
type WhisperWord,

View file

@ -23,6 +23,7 @@
"@openreader/compute-core": ["./compute/core/src/index.ts"],
"@openreader/compute-core/contracts": ["./compute/core/src/contracts.ts"],
"@openreader/compute-core/local-runtime": ["./compute/core/src/local-runtime.ts"],
"@openreader/compute-core/runtime": ["./compute/core/src/runtime/index.ts"],
"@openreader/compute-core/runtime/timeout-config": ["./compute/core/src/runtime/timeout-config.ts"],
"@openreader/compute-core/pdf-layout/*": ["./compute/core/src/pdf-layout/*"]
}