Files
Netcatty/application/state/sftp/transferConnectionPool.ts
T
bincxz b31bc0bec8 feat(sftp): harden global transfer center with FileZilla-style pool
Add dedicated transfer connections (1–2 per host) so bulk uploads/downloads
no longer block browse sessions. Also cover session leases, resume after
restart, auto-reconnect, skipAdmission, and transfer-center UX fixes for #2396.
2026-07-23 15:47:26 +08:00

297 lines
9.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* FileZilla-style dedicated transfer connection pool.
*
* Browse/list uses the panel SFTP session. Bulk transfers use 1–2 separate
* sessions per host so interactive navigation is not blocked by multi-file
* uploads/downloads (same idea as FileZilla's transfer connections).
*/
export const DEFAULT_TRANSFER_CONNECTIONS_PER_HOST = 2;
export const MIN_TRANSFER_CONNECTIONS_PER_HOST = 1;
export const MAX_TRANSFER_CONNECTIONS_PER_HOST = 4;
export const DEFAULT_TRANSFER_CONNECTION_IDLE_TTL_MS = 30_000;
export type TransferPoolOpenFn = (poolKey: string) => Promise<string>;
export type TransferPoolCloseFn = (sftpId: string) => void | Promise<void>;
export interface TransferConnectionLease {
sftpId: string;
poolKey: string;
/** Drop the holder count; connection stays pooled for reuse. */
release: () => void;
/** Drop holder, remove from pool, and close — use when the session is dead. */
discard: () => void;
}
interface PoolSlot {
sftpId: string;
holders: Set<string>;
lastUsedAt: number;
opening?: Promise<string>;
}
export interface TransferConnectionPoolOptions {
maxPerHost?: number;
idleTtlMs?: number;
closeSession?: TransferPoolCloseFn;
now?: () => number;
}
export interface TransferConnectionPool {
acquire(poolKey: string, transferId: string, open: TransferPoolOpenFn): Promise<TransferConnectionLease>;
release(poolKey: string, sftpId: string, transferId: string): void;
/** Remove a dead session from the pool and close it (best-effort). */
discard(sftpId: string): void;
getStats(poolKey?: string): {
connections: number;
busy: number;
idle: number;
holders: number;
};
closeIdle(now?: number): Promise<number>;
closeAll(): Promise<void>;
setMaxPerHost(max: number): void;
}
function normalizeMaxPerHost(value: number | undefined): number {
if (!Number.isInteger(value)) return DEFAULT_TRANSFER_CONNECTIONS_PER_HOST;
return Math.min(
MAX_TRANSFER_CONNECTIONS_PER_HOST,
Math.max(MIN_TRANSFER_CONNECTIONS_PER_HOST, value as number),
);
}
export function createTransferConnectionPool(
options: TransferConnectionPoolOptions = {},
): TransferConnectionPool {
let maxPerHost = normalizeMaxPerHost(options.maxPerHost);
const idleTtlMs = options.idleTtlMs ?? DEFAULT_TRANSFER_CONNECTION_IDLE_TTL_MS;
const closeSession = options.closeSession;
const now = options.now ?? (() => Date.now());
/** poolKey -> open slots for that host endpoint */
const pools = new Map<string, PoolSlot[]>();
/** Serialize opens per host so we never exceed maxPerHost under concurrency */
const openLocks = new Map<string, Promise<void>>();
const getList = (poolKey: string): PoolSlot[] => {
let list = pools.get(poolKey);
if (!list) {
list = [];
pools.set(poolKey, list);
}
return list;
};
const withOpenLock = async <T>(poolKey: string, work: () => Promise<T>): Promise<T> => {
const previous = openLocks.get(poolKey) ?? Promise.resolve();
let releaseLock!: () => void;
const gate = new Promise<void>((resolve) => { releaseLock = resolve; });
// Chain waiters so concurrent acquires never exceed maxPerHost.
openLocks.set(poolKey, previous.catch(() => {}).then(() => gate));
await previous.catch(() => {});
try {
return await work();
} finally {
releaseLock();
}
};
const pickSlot = (list: PoolSlot[]): PoolSlot | null => {
if (list.length === 0) return null;
// Prefer idle connections, else least-loaded (FileZilla-style multiplexing).
const sorted = [...list].sort((a, b) => {
if (a.holders.size !== b.holders.size) return a.holders.size - b.holders.size;
return a.lastUsedAt - b.lastUsedAt;
});
return sorted[0] ?? null;
};
const release = (poolKey: string, sftpId: string, transferId: string) => {
const list = pools.get(poolKey);
if (!list) return;
const slot = list.find((candidate) => candidate.sftpId === sftpId);
if (!slot) return;
slot.holders.delete(transferId);
slot.lastUsedAt = now();
};
const discard = (sftpId: string) => {
if (!sftpId) return;
for (const [poolKey, list] of pools.entries()) {
const idx = list.findIndex((slot) => slot.sftpId === sftpId);
if (idx < 0) continue;
list.splice(idx, 1);
if (list.length === 0) pools.delete(poolKey);
else pools.set(poolKey, list);
void Promise.resolve(closeSession?.(sftpId)).catch(() => {});
return;
}
};
const makeLease = (poolKey: string, sftpId: string, transferId: string): TransferConnectionLease => ({
sftpId,
poolKey,
release: () => release(poolKey, sftpId, transferId),
discard: () => {
release(poolKey, sftpId, transferId);
discard(sftpId);
},
});
const acquire = async (
poolKey: string,
transferId: string,
open: TransferPoolOpenFn,
): Promise<TransferConnectionLease> => {
if (!poolKey) throw new Error("Transfer pool key is required");
if (!transferId) throw new Error("Transfer id is required");
return withOpenLock(poolKey, async () => {
const list = getList(poolKey);
const existing = pickSlot(list);
// Reuse when we already have max connections, or when an idle one exists.
// FileZilla-style: open a second connection only when the first is busy.
if (existing && (existing.holders.size === 0 || list.length >= maxPerHost)) {
existing.holders.add(transferId);
existing.lastUsedAt = now();
return makeLease(poolKey, existing.sftpId, transferId);
}
if (list.length < maxPerHost) {
const sftpId = await open(poolKey);
const slot: PoolSlot = {
sftpId,
holders: new Set([transferId]),
lastUsedAt: now(),
};
list.push(slot);
return makeLease(poolKey, sftpId, transferId);
}
// Should be unreachable when maxPerHost >= 1, but keep safe fallback.
const fallback = pickSlot(list);
if (!fallback) {
const sftpId = await open(poolKey);
const slot: PoolSlot = {
sftpId,
holders: new Set([transferId]),
lastUsedAt: now(),
};
list.push(slot);
return makeLease(poolKey, sftpId, transferId);
}
fallback.holders.add(transferId);
fallback.lastUsedAt = now();
return makeLease(poolKey, fallback.sftpId, transferId);
});
};
const closeIdle = async (at = now()): Promise<number> => {
let closed = 0;
for (const [poolKey, list] of pools.entries()) {
const kept: PoolSlot[] = [];
for (const slot of list) {
const idle = slot.holders.size === 0 && at - slot.lastUsedAt >= idleTtlMs;
if (!idle) {
kept.push(slot);
continue;
}
closed += 1;
try {
await closeSession?.(slot.sftpId);
} catch {
// best-effort
}
}
if (kept.length === 0) pools.delete(poolKey);
else pools.set(poolKey, kept);
}
return closed;
};
const closeAll = async () => {
for (const list of pools.values()) {
for (const slot of list) {
try {
await closeSession?.(slot.sftpId);
} catch {
// best-effort
}
}
}
pools.clear();
};
const getStats = (poolKey?: string) => {
const lists = poolKey ? [pools.get(poolKey) ?? []] : [...pools.values()];
let connections = 0;
let busy = 0;
let idle = 0;
let holders = 0;
for (const list of lists) {
for (const slot of list) {
connections += 1;
holders += slot.holders.size;
if (slot.holders.size > 0) busy += 1;
else idle += 1;
}
}
return { connections, busy, idle, holders };
};
return {
acquire,
release,
discard,
getStats,
closeIdle,
closeAll,
setMaxPerHost(max: number) {
maxPerHost = normalizeMaxPerHost(max);
},
};
}
/** Shared process-wide pool used by SFTP bulk transfers in the renderer. */
let sharedPool: TransferConnectionPool | null = null;
/**
* Process-wide transfer pool (FileZilla-style, max 2 sessions per host).
* `closeSession` is applied on first creation; later callers share the same pool.
*/
export function getSharedTransferConnectionPool(
options?: TransferConnectionPoolOptions,
): TransferConnectionPool {
if (!sharedPool) {
sharedPool = createTransferConnectionPool({
maxPerHost: DEFAULT_TRANSFER_CONNECTIONS_PER_HOST,
idleTtlMs: DEFAULT_TRANSFER_CONNECTION_IDLE_TTL_MS,
...options,
});
}
return sharedPool;
}
/** Test-only: drop the singleton so tests start clean. */
export function resetSharedTransferConnectionPoolForTests(): void {
sharedPool = null;
}
export function buildTransferPoolKey(input: {
hostId?: string;
hostname?: string;
port?: number;
username?: string;
protocol?: string;
sftpSudo?: boolean;
}): string {
if (input.hostId) return `host:${input.hostId}`;
const host = input.hostname || "unknown";
const port = input.port || 22;
const user = input.username || "root";
const protocol = input.protocol || "ssh";
const sudo = input.sftpSudo ? "sudo" : "nosudo";
return `ep:${host}:${port}:${user}:${protocol}:${sudo}`;
}