Make offline checking intervals configurable

Fixes #3855
This commit is contained in:
Owen
2026-10-13 09:35:09 -04:00
parent 6bf4595969
commit 2f65a8c124
6 changed files with 104 additions and 23 deletions
+27
View File
@@ -0,0 +1,27 @@
/*
* This file is part of a proprietary work.
*
* Copyright (c) 2025-2026 Fossorial, Inc.
* All rights reserved.
*
* This file is licensed under the Fossorial Commercial License.
* You may not use this file except in compliance with the License.
* Unauthorized use, copying, modification, or distribution is strictly prohibited.
*
* This file is not licensed under the AGPLv3.
*/
import type { OfflineThresholds } from "@server/lib/offlineThresholds";
import privateConfig from "#private/lib/config";
export function getOfflineThresholds(): OfflineThresholds {
const offlineChecker = privateConfig.getRawPrivateConfig().offline_checker;
return {
checkIntervalMs: offlineChecker.check_interval_ms,
newtMs: offlineChecker.newt_threshold_ms,
newtWireguardMs: offlineChecker.newt_wireguard_threshold_ms,
olmMs: offlineChecker.olm_threshold_ms,
remoteExitNodeMs: offlineChecker.remote_exit_node_threshold_ms
};
}
+31
View File
@@ -18,6 +18,7 @@ import { z } from "zod";
import { colorsSchema } from "@server/lib/colorsSchema";
import { build } from "@server/build";
import { getEnvOrYaml } from "@server/lib/getEnvOrYaml";
import { DEFAULT_OFFLINE_THRESHOLDS } from "@server/lib/offlineThresholds";
const portSchema = z.number().positive().gt(0).lte(65535);
@@ -176,6 +177,36 @@ export const privateConfigSchema = z
.default("http://gerbil:3004")
})
.prefault({}),
// How long a newt site / olm client / remote exit node has to go
// without a ping before the offline checker marks it offline, and how
// often the checkers run.
offline_checker: z
.object({
check_interval_ms: z
.number()
.int()
.positive()
.default(DEFAULT_OFFLINE_THRESHOLDS.checkIntervalMs),
newt_threshold_ms: z
.number()
.int()
.default(DEFAULT_OFFLINE_THRESHOLDS.newtMs),
// wireguard sites don't ping, so they are judged by their
// last bandwidth update instead
newt_wireguard_threshold_ms: z
.number()
.int()
.default(DEFAULT_OFFLINE_THRESHOLDS.newtWireguardMs),
olm_threshold_ms: z
.number()
.int()
.default(DEFAULT_OFFLINE_THRESHOLDS.olmMs),
remote_exit_node_threshold_ms: z
.number()
.int()
.default(DEFAULT_OFFLINE_THRESHOLDS.remoteExitNodeMs)
})
.prefault({}),
flags: z
.object({
enable_redis: z.boolean().optional().default(false),
@@ -14,11 +14,10 @@
import { db, exitNodes } from "@server/db";
import { eq, lt, isNull, and, or } from "drizzle-orm";
import logger from "@server/logger";
import { getOfflineThresholds } from "#private/lib/offlineThresholds";
// Track if the offline checker interval is running
let offlineCheckerInterval: NodeJS.Timeout | null = null;
const OFFLINE_CHECK_INTERVAL = 30 * 1000; // Check every 30 seconds
const OFFLINE_THRESHOLD_MS = 2 * 60 * 1000; // 2 minutes
/**
* Starts the background interval that checks for clients that haven't pinged recently
@@ -31,11 +30,11 @@ export const startRemoteExitNodeOfflineChecker = (): void => {
offlineCheckerInterval = setInterval(async () => {
try {
const twoMinutesAgo = Math.floor(
(Date.now() - OFFLINE_THRESHOLD_MS) / 1000
const offlineBefore = Math.floor(
(Date.now() - getOfflineThresholds().remoteExitNodeMs) / 1000
);
// Find clients that haven't pinged in the last 2 minutes and mark them as offline
// Find clients that haven't pinged within the offline threshold and mark them as offline
const offlineNodes = await db
.update(exitNodes)
.set({ online: false })
@@ -44,7 +43,7 @@ export const startRemoteExitNodeOfflineChecker = (): void => {
eq(exitNodes.online, true),
eq(exitNodes.type, "remoteExitNode"),
or(
lt(exitNodes.lastPing, twoMinutesAgo),
lt(exitNodes.lastPing, offlineBefore),
isNull(exitNodes.lastPing)
)
)
@@ -65,7 +64,7 @@ export const startRemoteExitNodeOfflineChecker = (): void => {
} catch (error) {
logger.error("Error in offline checker interval", { error });
}
}, OFFLINE_CHECK_INTERVAL);
}, getOfflineThresholds().checkIntervalMs);
logger.debug("Started offline checker interval");
};