small optimizations

This commit is contained in:
KingKDot
2026-07-12 23:14:29 -05:00
parent e54634d7ee
commit 1bc6364055
11 changed files with 273 additions and 37 deletions
+11 -2
View File
@@ -1,7 +1,16 @@
import { upsertClientRows, type ClientDbRow } from "./db";
import { metrics } from "./metrics";
export const CLIENT_DB_SYNC_INTERVAL_MS = Number(process.env.OVERLORD_CLIENT_DB_SYNC_MS || 5000);
function positiveNumberEnv(name: string, fallback: number): number {
const value = Number(process.env[name] || fallback);
return Number.isFinite(value) && value > 0 ? value : fallback;
}
export const CLIENT_DB_SYNC_INTERVAL_MS = positiveNumberEnv("OVERLORD_CLIENT_DB_SYNC_MS", 5000);
export const CLIENT_PRESENCE_SYNC_INTERVAL_MS = Math.max(
CLIENT_DB_SYNC_INTERVAL_MS,
positiveNumberEnv("OVERLORD_CLIENT_PRESENCE_SYNC_MS", 60_000),
);
export const CLIENT_DB_SYNC_BATCH_SIZE = Math.max(
1,
Number(process.env.OVERLORD_CLIENT_DB_SYNC_BATCH_SIZE || 500),
@@ -65,7 +74,7 @@ setInterval(flushQueuedClientDbUpdates, CLIENT_DB_SYNC_INTERVAL_MS);
export function shouldSyncClientToDb(clientId: string, now: number): boolean {
const last = lastClientDbSync.get(clientId) || 0;
if (now - last < CLIENT_DB_SYNC_INTERVAL_MS) return false;
if (now - last < CLIENT_PRESENCE_SYNC_INTERVAL_MS) return false;
lastClientDbSync.set(clientId, now);
return true;
}
+17 -1
View File
@@ -221,8 +221,24 @@ try {
DELETE FROM client_search_fts WHERE rowid = old.rowid;
END;
`);
db.run(`DROP TRIGGER IF EXISTS clients_search_au`);
db.run(`
CREATE TRIGGER IF NOT EXISTS clients_search_au AFTER UPDATE ON clients BEGIN
CREATE TRIGGER clients_search_au AFTER UPDATE ON clients
WHEN old.host IS NOT new.host
OR old.user IS NOT new.user
OR old.nickname IS NOT new.nickname
OR old.custom_tag IS NOT new.custom_tag
OR old.custom_tag_note IS NOT new.custom_tag_note
OR old.os IS NOT new.os
OR old.ip IS NOT new.ip
OR old.hwid IS NOT new.hwid
OR old.country IS NOT new.country
OR old.version IS NOT new.version
OR old.build_tag IS NOT new.build_tag
OR old.cpu IS NOT new.cpu
OR old.gpu IS NOT new.gpu
OR old.ram IS NOT new.ram
BEGIN
DELETE FROM client_search_fts WHERE rowid = old.rowid;
INSERT INTO client_search_fts(
rowid, id, host, user, nickname, custom_tag, custom_tag_note,
@@ -127,7 +127,10 @@ function getCaptureDllBytes(): Uint8Array | null {
return null;
}
const VIEWER_BACKPRESSURE_BYTES = 2 * 1024 * 1024; // 2 MB
const VIEWER_BACKPRESSURE_BYTES = Math.max(
64 * 1024,
Number(process.env.OVERLORD_MEDIA_VIEWER_BACKPRESSURE_BYTES || 512 * 1024),
);
type FrameBroadcastResult = {
sent: boolean;
@@ -41,6 +41,14 @@ const desktopAudioSessions = new Map<string, DesktopAudioViewer>();
const desktopAudioSessionsByClient = new Map<string, Set<string>>();
const dashboardSessions = new Map<string, DashboardViewer>();
const chatSessions = new Map<string, ChatViewer>();
const VIEWER_BACKPRESSURE_BYTES = Math.max(
64 * 1024,
Number(process.env.OVERLORD_VIEWER_BACKPRESSURE_BYTES || 2 * 1024 * 1024),
);
function viewerHasBackpressure(ws: ServerWebSocket<SocketData>): boolean {
return (ws.getBufferedAmount?.() ?? 0) > VIEWER_BACKPRESSURE_BYTES;
}
function addSessionToClientIndex(
index: Map<string, Set<string>>,
@@ -346,6 +354,7 @@ export function safeSendViewer(
payload: any,
): boolean {
try {
if (viewerHasBackpressure(ws)) return false;
ws.send(JSON.stringify(payload));
return true;
} catch (err) {
@@ -359,6 +368,7 @@ export function safeSendViewerFrame(
header?: any,
): number {
try {
if (viewerHasBackpressure(ws)) return 0;
const meta = JSON.stringify(header || {});
const metaBytes = new TextEncoder().encode(meta);
const metaLength = new Uint8Array(4);
@@ -532,6 +542,7 @@ export function notifyDashboardClientEvent(
});
for (const [id, session] of dashboardSessions) {
try {
if (viewerHasBackpressure(session.viewer)) continue;
session.viewer.send(msg);
} catch {
dashboardSessions.delete(id);
@@ -553,6 +564,7 @@ export function notifyDashboardViewers(): void {
const msg = JSON.stringify({ type: "clients_changed" });
for (const [id, session] of dashboardSessions) {
try {
if (viewerHasBackpressure(session.viewer)) continue;
session.viewer.send(msg);
} catch {
dashboardSessions.delete(id);
@@ -580,6 +592,7 @@ export function getChatSessionCount(): number {
export function broadcastChatMessage(msg: string): void {
for (const [id, session] of chatSessions) {
try {
if (viewerHasBackpressure(session.viewer)) continue;
session.viewer.send(msg);
} catch {
chatSessions.delete(id);
+10 -9
View File
@@ -16,7 +16,7 @@ import {
requestThumbnailRegen,
} from "./thumbnails";
import { metrics } from "./metrics";
import { flushQueuedClientDbUpdates, markClientDbSynced, queueClientDbUpdate, shouldSyncClientToDb } from "./client-db-sync";
import { flushQueuedClientDbUpdates, queueClientDbUpdate, shouldSyncClientToDb } from "./client-db-sync";
import { setClientDisconnectInfo } from "./db";
export { clearClientSyncState } from "./client-db-sync";
@@ -225,14 +225,15 @@ export function handlePong(info: ClientInfo, payload: WireMessage) {
if (rtt >= 0 && rtt < maxRttMs) {
info.pingMs = rtt;
queueClientDbUpdate({
id: info.id,
pingMs: info.pingMs,
lastSeen: info.lastSeen,
online: 1,
isAdmin: info.isAdmin,
});
markClientDbSynced(info.id, nowTs);
if (shouldSyncClientToDb(info.id, nowTs)) {
queueClientDbUpdate({
id: info.id,
pingMs: info.pingMs,
lastSeen: info.lastSeen,
online: 1,
isAdmin: info.isAdmin,
});
}
metrics.recordPing(rtt);
} else {