Files
Netcatty/application/state/sftp/transferConcurrency.ts
T
bincxz 3d140c2f8e fix(sftp): harden folder pause/resume, delete, and root reconnect
Make folder pause latch-first with barriers and a child pause watchdog so
workers stop claiming files after soft-drain; soft-resume live walks instead
of full rewalks. Surface path conflicts with toasts, keep pause progress
visible, and show a resume spinner. Restore verified shell rm -rf with SFTP
confirmation for fast directory delete, and prefer reconnect over wiping the
pane to the host picker when listing fails on some hosts.
2026-07-26 00:10:22 +08:00

49 lines
1.6 KiB
TypeScript

export const DEFAULT_SFTP_FILE_TRANSFER_CONCURRENCY = 2;
export const MIN_SFTP_FILE_TRANSFER_CONCURRENCY = 1;
export const MAX_SFTP_FILE_TRANSFER_CONCURRENCY = 16;
export function resolveSftpTransferConcurrency(readStoredValue: () => number | null | undefined): number {
const stored = readStoredValue();
return stored != null &&
stored >= MIN_SFTP_FILE_TRANSFER_CONCURRENCY &&
stored <= MAX_SFTP_FILE_TRANSFER_CONCURRENCY
? stored
: DEFAULT_SFTP_FILE_TRANSFER_CONCURRENCY;
}
export async function runSftpTransferWorkers<T>(
items: T[],
readStoredConcurrency: () => number | null | undefined,
worker: (item: T, index: number) => Promise<void>,
options?: {
/**
* Called before claiming the next queue index. Folder pause must wait here
* so a worker that just finished soft-drain cannot claim the next file
* while the parent is still latched (claim-before-wait started new work).
*/
beforeClaim?: () => Promise<void>;
},
): Promise<void> {
const concurrency = resolveSftpTransferConcurrency(readStoredConcurrency);
let nextIndex = 0;
const runNext = async () => {
while (nextIndex < items.length) {
// Wait BEFORE claiming so pause does not leave a claimed-but-not-started
// index that arms as soon as soft-drain finishes the previous file.
if (options?.beforeClaim) {
await options.beforeClaim();
}
if (nextIndex >= items.length) return;
const index = nextIndex++;
await worker(items[index], index);
}
};
const workers = Array.from(
{ length: Math.min(concurrency, items.length) },
() => runNext(),
);
await Promise.all(workers);
}