mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-03 05:45:04 +03:00
perf: lazy provider init, P2C quota cache, structuredClone elimination, getSettings→getCachedSettings (batch 2) (#7893)
* perf: startup parallelization, stream TextEncoder lift, auth middleware bottlenecks
Startup (~100-300ms faster cold start):
- Parallelize 4 early imports via Promise.all() in registerNodejs()
- Parallelize 10 independent background services via Promise.allSettled()
- Each service has independent try/catch — no failure domino effect
Streaming pipeline (8 fewer TextEncoder GC allocations per SSE event):
- Lift new TextEncoder() from per-chunk inside buildClaudeStreamingResponse
to function scope alongside existing decoder singleton
Auth middleware bottlenecks (from PerfBottleneckAnalysis):
- Backoff decay loop: replace updateProviderConnection (full CRUD:
SELECT+encrypt+cache-invalidate+backup) with resetConnectionBackoff
(targeted UPDATE of backoff/error columns only)
- Dual .filter() for quota: replace two passes calling
isQuotaExhaustedForRequest per connection with a single for loop
partitioning into withQuota/exhaustedQuota
- Debug-log filter recomputation: capture connectionFilterStatus Map
during the filter pass; debug loop reads 6 string comparisons instead
of 6 function calls per connection
Supporting:
- Add resetConnectionBackoff to src/lib/db/providers.ts (patterned after
clearConnectionErrorIfUnchanged, no CAS check)
- Re-export resetConnectionBackoff from src/lib/localDb.ts
- Update integration-wiring.test.ts regex for parallelized dynamic import
* perf: P2C quota cache, lazy provider init, structuredClone elimination, getSettings→getCachedSettings
- **auth.ts: P2C quota re-evaluation cache** — quotaResults Map threaded
through selectPoolSubset → compareP2CConnections → getP2CConnectionScore.
Populated during filter + partition passes, eliminating redundant
evaluateQuotaLimitPolicy / isQuotaExhaustedForRequest calls when the
P2C comparator re-evaluates previously-scored connections.
- **constants.ts: lazy PROVIDERS via Proxy** — replaces eager
generateLegacyProviders() + loadProviderCredentials() at module load
with Proxy delegating to deferred init on first property access.
- **providerModels.ts: lazy PROVIDER_MODELS + PROVIDER_ID_TO_ALIAS** —
same Proxy pattern for both exports; generateModels()/generateAliasMap()
deferred until first read.
- **stream.ts: structuredClone → minimal object spread** — replaces
O(n) deep clone of SSE response chunks with targeted reconstruction
of only mutated fields (usage, delta.content, finish_reason).
- **progressTracker.ts: TextDecoder lift** — module-level decoder
instead of per-chunk new TextDecoder().
- **Route files: getSettings() → getCachedSettings()** — 13 API route
files converted from uncached per-request DB reads to TTL-cached
wrapper (5s default), eliminating redundant queries on every request.
- **settings.ts: re-export getCachedSettings** from readCache for
non-localDb consumers.
- **Remove settingsCache.ts** — dead file, no imports reference it.
TS compile: 0 errors. Auth tests: 225/225 pass. Services: 269/269 pass.
* perf: Phase 1 tangible wins — egressCache eviction, mmap_size PRAGMA, composite indexes, proxyFallback lazy import
- egressCache: lazy TTL eviction on getCachedEgressIp access (bounds memory
leak to distinct proxy URLs, typically <100)
- mmap_size: apply stored PRAGMA from key_value table (256MiB default) after
applyStoredDatabaseOptimizationSettings — setting was stored but never applied
- schemaColumns: add idx_uh_provider_model_timestamp (covers getModelLatencyStats)
and idx_pc_provider_auth_type (covers 6+ provider_connections queries)
- proxyFallback: convert static import to dynamic import() inside error handler
(defers 210ms module load from startup to first proxy-retry scenario)
* perf: add dedup expression index, unref() sweep timers
- Add COALESCE expression index idx_uh_dedup on usage_history
matching the exact dedup query pattern. Eliminates FULL TABLE
SCAN on every saveRequestUsage insert.
- Add composite idx_uh_provider_model_timestamp on usage_history.
- Add composite idx_pc_provider_auth_type on provider_connections.
- Add .unref() to setInterval in batchProcessor.ts (polling loop).
- Add .unref() to setInterval in runtimeHeartbeat.ts (heartbeat).
* perf: bump SQLite cache_size default from 16MB to 64MB
New installs now start with 64MB page cache (was 16MB). Existing
users' stored settings are unchanged. Reduces disk reads for the
typical ~250MB database by keeping ~25% of pages in memory.
Also resolved pre-existing merge conflict in webhooks.ts.
* docs: add Redis production config guide and proxy port clash investigation report
- docs/redis-production-config.md: comprehensive Redis tuning guide
covering client options, server config, Docker settings, scaling,
and monitoring for all three Redis workloads (rate limiting,
auth cache, quota store)
- docs/proxy-port-clash-report.md: investigation confirming proxy
subsystem has no port binding issues; real EADDRINUSE history
traced to process supervisor crash-loop restart race (#4425) and
live-dashboard port clash (#6324), both already fixed
* fix: address PR #7893 review — add Proxy traps, extract migrations to reduce providers.ts size
- Add set trap to PROVIDER_ID_TO_ALIAS Proxy (providerModels.ts)
- Add deleteProperty traps to all three lazy Proxies (PROVIDERS,
PROVIDER_MODELS, PROVIDER_ID_TO_ALIAS)
- Extract autoMigrateLegacyEncryptedConnections and getGheCopilotHosts
from providers.ts (1129→1036 lines, -93) into providers/migrations.ts
- Both functions re-exported via providers.ts for backward compat
File-size ratchet resolved: src/lib/db/providers.ts now 1036 lines.
* fix: resolve merge conflict markers in 3 route/test files
- model-combo-mappings/route.ts: kept upstream version (Zod pagination
via validateBody + isValidationFailure), restored missing return
statement for GET handler
- playground/presets/route.ts: kept stashed version details (satisfies
type-narrowing + inlined Response) — functionally identical
- error-sanitization.test.ts: matches upstream exactly (no diff)
Test verification: same 7 pre-existing failures confirmed on upstream
baseline (c1bdd91e7). Zero regressions from conflict resolution.
Closes remaining uncommitted work from PR #7046 rebase.
* test(db): add resetConnectionBackoff coverage + fix file-size ratchet regression
- Add tests/unit/reset-connection-backoff.test.ts: covers the new
resetConnectionBackoff lightweight-UPDATE helper (clears backoff/error
columns and re-activates a connection, unconditional-write behavior on
terminal statuses, no-op for empty/unknown ids). Zero prior coverage
per pre-merge review of PR #7893 (Hard Rule #8).
- open-sse/services/batchProcessor.ts: fold the new unref() call into the
existing setInterval(...).unref() chain instead of a separate statement,
keeping the file at the frozen 915-line ratchet (Timeout.unref() already
returns `this`, so no type cast is needed).
- src/lib/localDb.ts: drop one blank separator line so the new
resetConnectionBackoff re-export stays within the frozen 808-line ratchet.
Pre-merge fix for PR #7893 (perf/startup-stream-auth-optimizations) per
/green-prs plan-file _tasks/pipeline/prs/1-analyzed/7893-*.plan.md.
Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouzapw@users.noreply.github.com>
---------
Co-authored-by: oyi77 <oyi77@users.noreply.github.com>
Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com>
Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouzapw@users.noreply.github.com>
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getTaskManager } from "@/lib/a2a/taskManager";
|
||||
import { getSettings } from "@/lib/db/settings";
|
||||
import { getCachedSettings } from "@/lib/db/settings";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const [settings, stats] = await Promise.all([
|
||||
getSettings(),
|
||||
getCachedSettings(),
|
||||
Promise.resolve(getTaskManager().getStats()),
|
||||
]);
|
||||
const enabled = settings.a2aEnabled === true;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getAuditRequestContext, logAuditEvent } from "@/lib/compliance/index";
|
||||
import { getSettings } from "@/lib/localDb";
|
||||
import { getCachedSettings } from "@/lib/localDb";
|
||||
import { SignJWT } from "jose";
|
||||
import { cookies } from "next/headers";
|
||||
import {
|
||||
@@ -72,7 +72,7 @@ export async function POST(request) {
|
||||
if (!password) {
|
||||
return NextResponse.json({ error: "Invalid password payload" }, { status: 400 });
|
||||
}
|
||||
const settings = await getSettings();
|
||||
const settings = await getCachedSettings();
|
||||
const bruteForceEnabled = settings.bruteForceProtection !== false;
|
||||
const clientIp = auditContext.ipAddress || null;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getSettings, updateSettings } from "@/lib/localDb";
|
||||
import { getCachedSettings, updateSettings } from "@/lib/localDb";
|
||||
import { SignJWT, jwtVerify, createRemoteJWKSet } from "jose";
|
||||
import { cookies } from "next/headers";
|
||||
// Test seam (static) — allows tests to inject a cookie store and capture the minted auth_token.
|
||||
@@ -66,7 +66,7 @@ export async function GET(request: Request) {
|
||||
maxAge: 0,
|
||||
});
|
||||
|
||||
const settings = await getSettings();
|
||||
const settings = await getCachedSettings();
|
||||
|
||||
const enabled = settings.oidcEnabled === true;
|
||||
const issuer =
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getSettings } from "@/lib/localDb";
|
||||
import { getCachedSettings } from "@/lib/localDb";
|
||||
|
||||
/**
|
||||
* GET /api/auth/oidc/login
|
||||
@@ -8,7 +8,7 @@ import { getSettings } from "@/lib/localDb";
|
||||
* Password login remains available as fallback.
|
||||
*/
|
||||
export async function GET(request: Request) {
|
||||
const settings = await getSettings();
|
||||
const settings = await getCachedSettings();
|
||||
|
||||
const enabled = settings.oidcEnabled === true;
|
||||
const issuer =
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { getAuditRequestContext, logAuditEvent } from "@/lib/compliance/index";
|
||||
import { getSettings } from "@/lib/localDb";
|
||||
import { getCachedSettings } from "@/lib/localDb";
|
||||
import {
|
||||
ensurePersistentManagementPasswordHash,
|
||||
getStoredManagementPassword,
|
||||
@@ -49,7 +49,7 @@ export async function POST(request: Request) {
|
||||
}
|
||||
const { password, name, scope, expiresInDays } = validation.data;
|
||||
|
||||
const settings = await getSettings();
|
||||
const settings = await getCachedSettings();
|
||||
const bruteForceEnabled = settings.bruteForceProtection !== false;
|
||||
const clientIp = auditContext.ipAddress || null;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getSettings } from "@/lib/db/settings";
|
||||
import { getCachedSettings } from "@/lib/db/settings";
|
||||
import {
|
||||
DEFAULT_HEADROOM_URL,
|
||||
isLoopbackHeadroomUrl,
|
||||
@@ -12,7 +12,7 @@ export const dynamic = "force-dynamic";
|
||||
|
||||
export async function POST(): Promise<Response> {
|
||||
try {
|
||||
const settings = await getSettings();
|
||||
const settings = await getCachedSettings();
|
||||
const url =
|
||||
typeof settings.headroomUrl === "string" && settings.headroomUrl
|
||||
? settings.headroomUrl
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getSettings } from "@/lib/db/settings";
|
||||
import { getCachedSettings } from "@/lib/db/settings";
|
||||
import { DEFAULT_HEADROOM_URL, getHeadroomStatus } from "@/lib/headroom/detect";
|
||||
import { getManagedPid } from "@/lib/headroom/process";
|
||||
import { createErrorResponse } from "@/lib/api/errorResponse";
|
||||
@@ -8,7 +8,7 @@ export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(): Promise<Response> {
|
||||
try {
|
||||
const settings = await getSettings();
|
||||
const settings = await getCachedSettings();
|
||||
const url =
|
||||
typeof settings.headroomUrl === "string" && settings.headroomUrl
|
||||
? settings.headroomUrl
|
||||
|
||||
@@ -7,12 +7,12 @@
|
||||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getSettings } from "@/lib/db/settings";
|
||||
import { getCachedSettings } from "@/lib/db/settings";
|
||||
import { handleMcpSSE } from "../../../../../open-sse/mcp-server/httpTransport";
|
||||
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
|
||||
|
||||
async function guardEnabled(): Promise<NextResponse | null> {
|
||||
const settings = await getSettings();
|
||||
const settings = await getCachedSettings();
|
||||
if (!settings.mcpEnabled) {
|
||||
return NextResponse.json(
|
||||
{ error: "MCP server is disabled. Enable it from the Endpoints page." },
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
getMcpHttpStatus,
|
||||
isMcpHttpTransportReady,
|
||||
} from "../../../../../open-sse/mcp-server/httpTransport";
|
||||
import { getSettings } from "@/lib/db/settings";
|
||||
import { getCachedSettings } from "@/lib/db/settings";
|
||||
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
@@ -21,7 +21,7 @@ export async function GET(request: Request) {
|
||||
readMcpHeartbeat(),
|
||||
getAuditStats(),
|
||||
queryAuditEntries({ limit: 1, offset: 0 }),
|
||||
getSettings(),
|
||||
getCachedSettings(),
|
||||
]);
|
||||
|
||||
const mcpEnabled = !!settings.mcpEnabled;
|
||||
|
||||
@@ -8,12 +8,12 @@
|
||||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getSettings } from "@/lib/db/settings";
|
||||
import { getCachedSettings } from "@/lib/db/settings";
|
||||
import { handleMcpStreamableHTTP } from "../../../../../open-sse/mcp-server/httpTransport";
|
||||
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
|
||||
|
||||
async function guardEnabled(): Promise<NextResponse | null> {
|
||||
const settings = await getSettings();
|
||||
const settings = await getCachedSettings();
|
||||
if (!settings.mcpEnabled) {
|
||||
return NextResponse.json(
|
||||
{ error: "MCP server is disabled. Enable it from the Endpoints page." },
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getProviderConnections, getSettings } from "@/lib/localDb";
|
||||
import { getProviderConnections, getCachedSettings } from "@/lib/localDb";
|
||||
import { buildHealthPayload } from "@/lib/monitoring/observability";
|
||||
import { APP_CONFIG } from "@/shared/constants/config";
|
||||
import { AI_PROVIDERS } from "@/shared/constants/providers";
|
||||
@@ -67,7 +67,7 @@ export async function GET() {
|
||||
import("@omniroute/open-sse/services/sessionManager.ts"),
|
||||
import("@/lib/credentialHealth/cache"),
|
||||
import("@/lib/localHealthCheck"),
|
||||
getSettings(),
|
||||
getCachedSettings(),
|
||||
getProviderConnections(),
|
||||
]);
|
||||
|
||||
|
||||
@@ -69,10 +69,13 @@ export async function GET(request: Request): Promise<Response> {
|
||||
const raw = {
|
||||
offset: searchParams.get("offset") || undefined,
|
||||
limit: searchParams.get("limit") || undefined,
|
||||
};
|
||||
} satisfies { offset?: string; limit?: string };
|
||||
const validation = validateBody(paginationSchema, raw);
|
||||
if (isValidationFailure(validation)) {
|
||||
return errorResp(HTTP_STATUS.BAD_REQUEST, validation.error);
|
||||
return new Response(JSON.stringify(validation.error), {
|
||||
status: 400,
|
||||
headers: { "Content-Type": "application/json", ...CORS_HEADERS },
|
||||
});
|
||||
}
|
||||
const { limit, offset } = validation.data;
|
||||
const result = listPlaygroundPresets(limit !== undefined ? { limit, offset } : undefined);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getSettings, updateSettings } from "@/lib/localDb";
|
||||
import { getCachedSettings, getSettings, updateSettings } from "@/lib/localDb";
|
||||
import {
|
||||
buildLegacyResilienceCompat,
|
||||
mergeResilienceSettings,
|
||||
@@ -121,7 +121,7 @@ async function syncRuntimeSettings(resilienceSettings: ResilienceSettings) {
|
||||
*/
|
||||
export async function GET() {
|
||||
try {
|
||||
const settings = await getSettings();
|
||||
const settings = await getCachedSettings();
|
||||
const resilience = resolveResilienceSettings(settings);
|
||||
|
||||
return NextResponse.json({
|
||||
|
||||
Reference in New Issue
Block a user