Downloads: speed limit in Settings

hf has no bandwidth option, so the worker pauses hf (SIGSTOP/SIGCONT) whenever it is ahead of a
token bucket over bytes on disk. The limit is shared by all running downloads and re-read every
5 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pascal LinxweilerandClaude Opus 5.5 committed 2026-10-13 10:04:48 +02:00
1 parent b623b16a6d
commit 85af566b93
9 files changed
+285 -8

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
// Download speed limit (Settings → Downloads). Shared by the settings API and the worker.
export const SPEED_LIMIT_KEY = "downloads.maxBytesPerSecond";
/** Smallest limit accepted (0.1 MB/s); below that hf's own timeouts start to bite. */
export const MIN_SPEED_LIMIT = 100_000;
/** The stored value as bytes per second, or null for no limit. */
export function parseSpeedLimit(v: unknown): number | null {
return typeof v === "number" && Number.isFinite(v) && v >= MIN_SPEED_LIMIT ? v : null;
}
+15
View File
@@ -4,6 +4,7 @@ import { db } from "@/lib/db/client";
import { settings } from "@/lib/db/schema";
import { isLabelColor } from "@/lib/colors";
import { FILE_ROLE_ORDER, FILE_ROLES, type FileRole, type FileRoleStyles } from "@/lib/file-roles";
import { MIN_SPEED_LIMIT, parseSpeedLimit, SPEED_LIMIT_KEY } from "@/lib/download-limit";
import { DEFAULT_RECOMMENDED_ROLES, RECOMMENDABLE_ROLES } from "@/lib/recommend";
const FILE_ROLES_KEY = "labels.fileRoles";
@@ -84,3 +85,17 @@ export async function saveRecommendedRoles(roles: string[]): Promise<FileRole[]>
await write(RECOMMENDED_KEY, RECOMMENDABLE_ROLES.filter((r) => roles.includes(r)));
return getRecommendedRoles();
}
/** Download speed limit in bytes per second (Settings → Downloads), or null for no limit. */
export async function getDownloadSpeedLimit(): Promise<number | null> {
return parseSpeedLimit(await read<unknown>(SPEED_LIMIT_KEY));
}
export async function saveDownloadSpeedLimit(bytesPerSecond: number | null): Promise<number | null> {
if (bytesPerSecond != null && (!Number.isFinite(bytesPerSecond) || bytesPerSecond < MIN_SPEED_LIMIT)) {
throw new SettingsError("The speed limit must be at least 0.1 MB/s, or empty for no limit.");
}
if (bytesPerSecond == null) await db().delete(settings).where(eq(settings.key, SPEED_LIMIT_KEY));
else await write(SPEED_LIMIT_KEY, Math.round(bytesPerSecond));
return getDownloadSpeedLimit();
}