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>
87 lines
3.3 KiB
TypeScript
87 lines
3.3 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { toast } from "sonner";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
const MB = 1_000_000;
|
|
|
|
/** Download speed limit in MB/s; empty means no limit. Applies to running downloads within seconds. */
|
|
export function SpeedLimit({ initial }: { initial: number | null }) {
|
|
const [saved, setSaved] = useState(initial);
|
|
const [text, setText] = useState(initial ? String(initial / MB) : "");
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
const value = text.trim() === "" ? null : Number(text.replace(",", "."));
|
|
const invalid = value != null && !(value >= 0.1);
|
|
const dirty = value !== (saved == null ? null : saved / MB);
|
|
|
|
async function save(next: number | null) {
|
|
setBusy(true);
|
|
try {
|
|
const res = await fetch("/api/settings/downloads", {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ maxBytesPerSecond: next == null ? null : Math.round(next * MB) }),
|
|
});
|
|
const body = await res.json().catch(() => ({}));
|
|
if (!res.ok) return toast.error(body.error?.message ?? `Could not save (${res.status})`);
|
|
setSaved(body.maxBytesPerSecond);
|
|
setText(body.maxBytesPerSecond ? String(body.maxBytesPerSecond / MB) : "");
|
|
toast.success(body.maxBytesPerSecond ? `Downloads limited to ${body.maxBytesPerSecond / MB} MB/s` : "Download speed is no longer limited");
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Speed limit</CardTitle>
|
|
<CardDescription>
|
|
Caps the total download speed from Hugging Face, e.g. to leave bandwidth for others. It also applies to downloads that are already running. Leave empty for
|
|
no limit.
|
|
</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="flex flex-col gap-2">
|
|
<form
|
|
className="flex items-end gap-2"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
if (!invalid) void save(value);
|
|
}}
|
|
>
|
|
<div className="flex flex-col gap-1.5">
|
|
<Label htmlFor="speed-limit">Maximum speed (MB/s)</Label>
|
|
<Input
|
|
id="speed-limit"
|
|
value={text}
|
|
onChange={(e) => setText(e.target.value.replace(/[^\d.,]/g, ""))}
|
|
inputMode="decimal"
|
|
placeholder="No limit"
|
|
className="w-40"
|
|
aria-invalid={invalid}
|
|
/>
|
|
</div>
|
|
<Button type="submit" disabled={busy || invalid || !dirty}>
|
|
Save
|
|
</Button>
|
|
{saved != null && (
|
|
<Button type="button" variant="ghost" disabled={busy} onClick={() => void save(null)}>
|
|
Remove limit
|
|
</Button>
|
|
)}
|
|
</form>
|
|
<p className="text-xs text-muted-foreground">
|
|
{invalid
|
|
? "At least 0.1 MB/s."
|
|
: `1 MB/s ≈ 8 Mbit/s. hf has no speed option of its own, so Kaname pauses it whenever it gets ahead; short bursts above the limit are normal, the average holds.`}
|
|
</p>
|
|
</CardContent>
|
|
</Card>
|
|
);
|
|
}
|