Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

2.4 KB
Raw Blame
// Latency-based proxy rotation strategy: picks the candidate with the lowest
// average latency observed in `proxy_logs` over a trailing window. Extracted
// from proxies.ts to keep that frozen god-file under its line-count cap
// (imported directly by src/lib/db/proxies.ts, anti-barrel, #6798).
import { getDbInstance } from "./core";

const PROXY_LATENCY_WINDOW_HOURS = parseInt(process.env.PROXY_LATENCY_WINDOW_HOURS ?? "3", 10);

type LatencyLogRow = {
  proxy_host: string;
  proxy_port: number;
  avg_latency: number | null;
};

// Builds a `"host:port" -> avg_latency_ms` map from proxy_logs rows recorded
// within the trailing PROXY_LATENCY_WINDOW_HOURS window. The latency strategy
// measures the latency of sends served: a fast refusal does not make an
// outlet fast, so abandoned sends are excluded (legacy rows predate the
// journal and stay included via the NULL guard).
function buildLatencyMap(db: ReturnType<typeof getDbInstance>): Map<string, number> {
  const sinceIso = new Date(Date.now() - PROXY_LATENCY_WINDOW_HOURS * 60 * 60 * 1000).toISOString();

  const latencyRows = db
    .prepare(
      `SELECT proxy_host, proxy_port, AVG(latency_ms) as avg_latency
       FROM proxy_logs
       WHERE timestamp >= ?
         AND (attempt_issue IS NULL OR attempt_issue <> 'abandoned')
       GROUP BY proxy_host, proxy_port`
    )
    .all(sinceIso) as LatencyLogRow[];

  const latencyMap = new Map<string, number>();
  for (const r of latencyRows) {
    if (r.avg_latency !== null && r.avg_latency !== undefined) {
      latencyMap.set(`${r.proxy_host}:${r.proxy_port}`, r.avg_latency);
    }
  }
  return latencyMap;
}

// Picks the candidate with the lowest recorded average latency; candidates
// with no logged latency are treated as -1 (best/first) so untested proxies
// still get a chance to be selected and gather data.
export function pickByLatency<T>(db: ReturnType<typeof getDbInstance>, candidates: T[]): T {
  const latencyMap = buildLatencyMap(db);

  const sorted = [...candidates].sort((a, b) => {
    const pA = a as { host: string; port: number };
    const pB = b as { host: string; port: number };
    const keyA = `${pA.host}:${pA.port}`;
    const keyB = `${pB.host}:${pB.port}`;
    const latA = latencyMap.has(keyA) ? latencyMap.get(keyA)! : -1;
    const latB = latencyMap.has(keyB) ? latencyMap.get(keyB)! : -1;
    return latA - latB;
  });

  return sorted[0];
}