fix(connection): release stuck connection attempts
This commit is contained in:
1 parent
21cfd1b0f7
commit
413261a476
3 files changed
+76
-2
No files matched your search
@@ -0,0 +1,25 @@
|
||||
import type { ConnectionConfig } from "@/types/database";
|
||||
|
||||
export const CONNECTION_ATTEMPT_TIMEOUT_BUFFER_MS = 2_000;
|
||||
const DEFAULT_CONNECT_TIMEOUT_SECS = 5;
|
||||
|
||||
function positiveSeconds(value: unknown, fallback: number): number {
|
||||
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : fallback;
|
||||
}
|
||||
|
||||
export function connectionAttemptTimeoutMs(
|
||||
config: Pick<ConnectionConfig, "connect_timeout_secs" | "ssh_enabled" | "ssh_connect_timeout_secs" | "ssh_tunnels">,
|
||||
): number {
|
||||
const timeouts = [positiveSeconds(config.connect_timeout_secs, DEFAULT_CONNECT_TIMEOUT_SECS)];
|
||||
if (config.ssh_enabled) {
|
||||
timeouts.push(positiveSeconds(config.ssh_connect_timeout_secs, DEFAULT_CONNECT_TIMEOUT_SECS));
|
||||
for (const tunnel of config.ssh_tunnels ?? []) {
|
||||
timeouts.push(positiveSeconds(tunnel.connect_timeout_secs, DEFAULT_CONNECT_TIMEOUT_SECS));
|
||||
}
|
||||
}
|
||||
return Math.ceil(Math.max(...timeouts) * 1000 + CONNECTION_ATTEMPT_TIMEOUT_BUFFER_MS);
|
||||
}
|
||||
|
||||
export function connectionAttemptTimeoutMessage(timeoutMs: number): string {
|
||||
return `Connection attempt timed out after ${Math.ceil(timeoutMs / 1000)}s. Please check the network or VPN and try again.`;
|
||||
}
|
||||
Reference in new issue
Block a user