mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-10-29 22:55:24 +00:00
fix: slow file deletes from the trash lookups on every delete (#1390)
This commit is contained in:
1 parent
b03000e396
commit
a1b37c6829
3 files changed
+140
-6
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import type { Express, Request, Response } from "express";
|
||||
import type { PluginContext } from "@termix/plugin-sdk/backend";
|
||||
import type { SFTPWrapper } from "ssh2";
|
||||
import {
|
||||
execChannel,
|
||||
execWithSudo,
|
||||
@@ -24,6 +25,8 @@ type FileOperationRoutesDeps = {
|
||||
|
||||
const TRASH_RETENTION_DAYS_KEY = "trashRetentionDays";
|
||||
|
||||
const TRASH_PURGE_INTERVAL_MS = 60 * 60 * 1000;
|
||||
|
||||
export function registerFileOperationRoutes(
|
||||
app: Express,
|
||||
{ ctx, sshSessions, verifySessionOwnership }: FileOperationRoutesDeps,
|
||||
@@ -39,6 +42,22 @@ export function registerFileOperationRoutes(
|
||||
}
|
||||
};
|
||||
|
||||
// Deleting used to purge expired trash first, reading every trash entry
|
||||
// before answering. Do it after the response, at most hourly per session.
|
||||
const lastPurge = new WeakMap<SSHSession, number>();
|
||||
const purgeExpiredTrash = (session: SSHSession, sftp: SFTPWrapper) => {
|
||||
const now = Date.now();
|
||||
if (now - (lastPurge.get(session) ?? 0) < TRASH_PURGE_INTERVAL_MS) return;
|
||||
lastPurge.set(session, now);
|
||||
void getTrashRetentionDays()
|
||||
.then((days) => listTrash(sftp, days))
|
||||
.catch((error) =>
|
||||
ctx.log.warn(
|
||||
`Could not purge expired trash: ${(error as Error).message}`,
|
||||
),
|
||||
);
|
||||
};
|
||||
|
||||
async function ownedSession(
|
||||
req: Request,
|
||||
res: Response,
|
||||
@@ -494,9 +513,8 @@ export function registerFileOperationRoutes(
|
||||
if (!permanent) {
|
||||
try {
|
||||
const sftp = await getSessionSftp(sshConn);
|
||||
const retentionDays = await getTrashRetentionDays();
|
||||
await listTrash(sftp, retentionDays);
|
||||
const item = await moveToTrash(sftp, itemPath);
|
||||
purgeExpiredTrash(sshConn, sftp);
|
||||
ctx.log.info(
|
||||
`Item moved to trash: ${sessionId} (${itemPath}, trashId=${item.id})`,
|
||||
);
|
||||
|
||||
@@ -70,6 +70,8 @@ async function exists(sftp: SFTPWrapper, target: string) {
|
||||
}
|
||||
|
||||
async function ensureDirectory(sftp: SFTPWrapper, target: string) {
|
||||
// Usually there already: one round trip instead of one per segment.
|
||||
if (await exists(sftp, target)) return;
|
||||
const normalized = target.replace(/\\/g, "/");
|
||||
const root = normalized.startsWith("/") ? "/" : "";
|
||||
const parts = normalized.split("/").filter(Boolean);
|
||||
@@ -95,13 +97,32 @@ async function removeTree(sftp: SFTPWrapper, target: string): Promise<void> {
|
||||
await rmdir(sftp, target);
|
||||
}
|
||||
|
||||
async function trashPaths(sftp: SFTPWrapper) {
|
||||
type TrashDirs = { root: string; files: string; info: string };
|
||||
|
||||
// The trash folders of a channel do not move while it is open, and finding
|
||||
// them costs several round trips, which made every delete slow on a remote
|
||||
// host (#1390).
|
||||
const trashDirsByChannel = new WeakMap<SFTPWrapper, Promise<TrashDirs>>();
|
||||
|
||||
function trashPaths(sftp: SFTPWrapper): Promise<TrashDirs> {
|
||||
let dirs = trashDirsByChannel.get(sftp);
|
||||
if (!dirs) {
|
||||
dirs = findTrashPaths(sftp);
|
||||
trashDirsByChannel.set(sftp, dirs);
|
||||
dirs.catch(() => trashDirsByChannel.delete(sftp));
|
||||
}
|
||||
return dirs;
|
||||
}
|
||||
|
||||
async function findTrashPaths(sftp: SFTPWrapper): Promise<TrashDirs> {
|
||||
const home = await call<string>((done) => sftp.realpath(".", done));
|
||||
const root = path.posix.join(home.replace(/\\/g, "/"), TRASH_DIR);
|
||||
const files = path.posix.join(root, "files");
|
||||
const info = path.posix.join(root, "info");
|
||||
await ensureDirectory(sftp, files);
|
||||
await ensureDirectory(sftp, info);
|
||||
await Promise.all([
|
||||
ensureDirectory(sftp, files),
|
||||
ensureDirectory(sftp, info),
|
||||
]);
|
||||
return { root, files, info };
|
||||
}
|
||||
|
||||
@@ -124,7 +145,7 @@ function publicItem(item: StoredTrashItem): TrashItem {
|
||||
|
||||
async function readStoredItem(
|
||||
sftp: SFTPWrapper,
|
||||
dirs: { root: string; files: string; info: string },
|
||||
dirs: TrashDirs,
|
||||
id: string,
|
||||
): Promise<StoredTrashItem> {
|
||||
if (!ID_PATTERN.test(id)) throw new Error("Invalid trash item id");
|
||||
|
||||
Reference in new issue
Block a user