7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.
TypeScript
refactor: add Scheduler abstraction for testable timer injection
Claude4cbb5710de11
diff --git a/server/src/index.ts b/server/src/index.tsindex 8d32fb96..436764fc 100644--- a/server/src/index.ts+++ b/server/src/index.ts@@ -5,6 +5,7 @@ import { startHealthChecker } from './services/health.js';import { applyProxyUrl, applyProxyEnabled, applyProxyBypass } from './lib/proxy.js';import { startCatalogSync } from './services/catalog-sync.js';import { installProcessSafetyNet } from './lib/process-safety-net.js';+import { NodeScheduler } from './lib/scheduler.js';const PORT = process.env.PORT ?? 3001;// Dual-stack ('::') by default so the dashboard is reachable over both IPv4@@ -17,6 +18,8 @@ async function main() {// listener) can't take the proxy down. Genuine bugs still exit 1.installProcessSafetyNet();+ const scheduler = new NodeScheduler();+initDb();// Load the persisted proxy settings from the DB (env var wins if set).@@ -31,8 +34,8 @@ async function main() {const display = host.includes(':') ? `[${host}]` : host;console.log(`Server running on http://${display}:${PORT}`);console.log(`Proxy endpoint: http://${display}:${PORT}/v1/chat/completions`);- startHealthChecker();- startCatalogSync();+ startHealthChecker(scheduler);+ startCatalogSync(scheduler);};const server = app.listen(Number(PORT), HOST, onReady(HOST));diff --git a/server/src/lib/scheduler.ts b/server/src/lib/scheduler.tsnew file mode 100644index 00000000..5815bc80--- /dev/null+++ b/server/src/lib/scheduler.ts@@ -0,0 +1,16 @@+export interface Scheduler {+ every(ms: number, fn: () => void | Promise<void>, opts?: { name?: string }): () => void;+ after(ms: number, fn: () => void | Promise<void>): () => void;+}++export class NodeScheduler implements Scheduler {+ every(ms: number, fn: () => void | Promise<void>, _opts?: { name?: string }): () => void {+ const id = setInterval(() => { void fn(); }, ms);+ return () => clearInterval(id);+ }++ after(ms: number, fn: () => void | Promise<void>): () => void {+ const id = setTimeout(() => { void fn(); }, ms);+ return () => clearTimeout(id);+ }+}diff --git a/server/src/services/catalog-sync.ts b/server/src/services/catalog-sync.tsindex e1ad036e..4f3eab97 100644--- a/server/src/services/catalog-sync.ts+++ b/server/src/services/catalog-sync.ts@@ -4,6 +4,7 @@ import { getDb, getSetting, setSetting } from '../db/index.js';import { hasProvider } from '../providers/index.js';import { MEDIA_PLATFORMS } from './media.js';import type { Platform } from '@freellmapi/shared/types.js';+import type { Scheduler } from '../lib/scheduler.js';// Generative-media modalities are routed into the separate media_models table// (see services/media.ts), never into the chat `models` table.@@ -475,11 +476,11 @@ export function reapplyCachedCatalog(): { reapplied: boolean; version?: string }}}-let intervalId: ReturnType<typeof setInterval> | null = null;-let bootTimer: ReturnType<typeof setTimeout> | null = null;+let cancelBootTimer: (() => void) | null = null;+let cancelInterval: (() => void) | null = null;-export function startCatalogSync(): void {- if (intervalId) return;+export function startCatalogSync(scheduler: Scheduler): void {+ if (cancelInterval) return;if (process.env.CATALOG_SYNC_DISABLED === '1') {console.log('[catalog-sync] disabled via CATALOG_SYNC_DISABLED=1');return;@@ -489,18 +490,18 @@ export function startCatalogSync(): void {void refreshLicenseStatus();void syncCatalog();};- bootTimer = setTimeout(run, BOOT_DELAY_MS);- intervalId = setInterval(run, SYNC_INTERVAL_MS);+ cancelBootTimer = scheduler.after(BOOT_DELAY_MS, run);+ cancelInterval = scheduler.every(SYNC_INTERVAL_MS, run);console.log(`[catalog-sync] polling ${catalogBaseUrl()} every ${SYNC_INTERVAL_MS / 3600000}h`);}export function stopCatalogSync(): void {- if (bootTimer) {- clearTimeout(bootTimer);- bootTimer = null;+ if (cancelBootTimer) {+ cancelBootTimer();+ cancelBootTimer = null;}- if (intervalId) {- clearInterval(intervalId);- intervalId = null;+ if (cancelInterval) {+ cancelInterval();+ cancelInterval = null;}}diff --git a/server/src/services/health.ts b/server/src/services/health.tsindex 58cd0181..967a310e 100644--- a/server/src/services/health.ts+++ b/server/src/services/health.ts@@ -3,6 +3,7 @@ import { resolveProvider } from '../providers/index.js';import { decrypt } from '../lib/crypto.js';import type { Platform, KeyStatus } from '@freellmapi/shared/types.js';import { inferQuotaPoolKey } from './provider-quota.js';+import type { Scheduler } from '../lib/scheduler.js';const CHECK_INTERVAL_MS = 5 * 60 * 1000; // 5 minutesconst CONSECUTIVE_FAILURES_TO_DISABLE = 3;@@ -70,19 +71,19 @@ export async function checkAllKeys(): Promise<void> {console.log(`[Health] Check complete.`);}-let intervalId: ReturnType<typeof setInterval> | null = null;+let cancelHealthCheck: (() => void) | null = null;-export function startHealthChecker(): void {- if (intervalId) return;+export function startHealthChecker(scheduler: Scheduler): void {+ if (cancelHealthCheck) return;console.log(`[Health] Starting health checker (every ${CHECK_INTERVAL_MS / 1000}s)`);- intervalId = setInterval(() => {- checkAllKeys().catch(err => console.error('[Health] Check failed:', err));- }, CHECK_INTERVAL_MS);+ cancelHealthCheck = scheduler.every(CHECK_INTERVAL_MS, () =>+ checkAllKeys().catch(err => console.error('[Health] Check failed:', err)),+ );}export function stopHealthChecker(): void {- if (intervalId) {- clearInterval(intervalId);- intervalId = null;+ if (cancelHealthCheck) {+ cancelHealthCheck();+ cancelHealthCheck = null;}}