Measure transfer_min_speed in KiB/s and raise it to 64

The guard measures a proxy stream on completed writes to the receiver.
The OS buffers a couple of MiB per connection and only takes more once
a large part has been sent, so a slow receiver can go a long time with
no write completing. At the old default of 30 KiB per 30 s, downloads
well above that could be closed, and the slow receiver test failed
every run on Linux.

controller.transfer_min_progress (KiB per 30 s) is replaced by
controller.transfer_min_speed (KiB/s), defaulting to 64. At that speed
the measured 1.6 MB bursts come within the 30 second check. Configs
on the old default move to the new one, other values are converted.
The slow receiver test checks every 3 s instead of 500 ms, as with a
500 ms check it fails at any minimum.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-11-20 20:59:37 +00:00
1 parent e9a11013a4
commit 19569aefa4
6 files changed
+85 -54

No files matched your search

+20 -19
View File
@@ -419,7 +419,7 @@ export interface ProxyStream {
timeout: ReturnType<typeof setTimeout>;
closed: boolean;
/** Bytes the receiver has taken, set once the stream is flowing */
progress?: () => number;
bytesTaken?: () => number;
}
export async function createProxyStream(app: Application): Promise<ProxyStream> {
@@ -473,8 +473,8 @@ const answeredUploadDrainMs = 1000;
* so the connection is closed if the body hasn't ended shortly after the
* response. Closing it right away could make the client lose the response.
*
* The connection is also closed if the request moved less than
* controller.transfer_min_progress during a check interval, so that a
* The connection is also closed if the request moved slower than
* controller.transfer_min_speed during a check interval, so that a
* request which stalled or is trickled in does not stay open forever while
* a slow one can take the time it needs. For a request body this is what
* was received of it. For both ends of a proxy stream it is what the
@@ -501,33 +501,34 @@ function guardRequest(checkIntervalMs: number) {
});
});
const received = () => socket.bytesRead;
let measure: (() => number) | undefined = received;
let last = received();
const bytesReceived = () => socket.bytesRead;
let measuredBytes: (() => number) | undefined = bytesReceived;
let lastBytes = bytesReceived();
const check = setInterval(() => {
const stream: ProxyStream | undefined = res.locals.proxyStream;
let current: (() => number) | undefined;
let currentBytes: (() => number) | undefined;
if (stream && !stream.closed) {
current = stream.progress;
currentBytes = stream.bytesTaken;
} else if (!req.complete) {
current = received;
currentBytes = bytesReceived;
}
const minProgress = req.app.locals.controller.config.get("controller.transfer_min_progress");
if (current !== measure || !current || !minProgress) {
const minSpeedKiBps = req.app.locals.controller.config.get("controller.transfer_min_speed");
if (currentBytes !== measuredBytes || !currentBytes || !minSpeedKiBps) {
// What to measure changed, start over from here.
measure = current;
last = current ? current() : 0;
measuredBytes = currentBytes;
lastBytes = currentBytes ? currentBytes() : 0;
return;
}
const progress = current();
if (progress - last < minProgress * 1024) {
const bytes = currentBytes();
const intervalS = checkIntervalMs / 1000;
if (bytes - lastBytes < minSpeedKiBps * 1024 * intervalS) {
logger.warn(
`Closing ${req.method} ${req.path}, it moved less than ${minProgress} KiB ` +
`in ${checkIntervalMs / 1000} seconds`
`Closing ${req.method} ${req.path}, it moved slower than ${minSpeedKiBps} KiB/s ` +
`over ${intervalS} seconds`
);
socket.destroy();
}
last = progress;
lastBytes = bytes;
}, checkIntervalMs);
res.once("close", () => clearInterval(check));
next();
@@ -567,7 +568,7 @@ function startStream(res: Response, stream: ProxyStream) {
source.pipe(res);
stream.flowing = true;
// Counts completed writes, so data waiting in a buffer is not progress.
stream.progress = () => socket.bytesWritten - socket.writableLength;
stream.bytesTaken = () => socket.bytesWritten - socket.writableLength;
// Pipe does not pass on a source that was cut short, the receiver would wait forever.
const onSourceClose = () => {
if (!res.writableEnded) {
+13 -5
View File
@@ -31,7 +31,7 @@ export interface ControllerConfigFields {
"controller.max_subscription_filters": number;
"controller.system_metrics_interval": number;
"controller.proxy_stream_timeout": number;
"controller.transfer_min_progress": number;
"controller.transfer_min_speed": number;
"controller.factorio_username": string | null;
"controller.factorio_token": string | null;
"controller.share_factorio_credentials_with_hosts": boolean;
@@ -63,6 +63,14 @@ export class ControllerConfig extends classes.Config<ControllerConfigFields> {
config["controller.public_url"] = config["controller.external_address"];
delete config["controller.external_address"];
}
if (config.hasOwnProperty("controller.transfer_min_progress")) {
// Was KiB per 30 s, the old default is left out so the new one applies
const kib = config["controller.transfer_min_progress"];
if (kib !== 30) {
config["controller.transfer_min_speed"] = typeof kib === "number" ? kib / 30 : kib;
}
delete config["controller.transfer_min_progress"];
}
return config;
}
@@ -224,11 +232,11 @@ export class ControllerConfig extends classes.Config<ControllerConfigFields> {
initialValue: 15,
validator: validators.greaterThanZero,
},
"controller.transfer_min_progress": {
title: "Transfer Minimum Progress",
description: "KiB an upload or download has to move every 30 seconds to not be closed, 0 for no limit.",
"controller.transfer_min_speed": {
title: "Transfer Minimum Speed",
description: "KiB/s an upload or download has to move at over 30 seconds to not be closed, 0 for no limit.",
type: "number",
initialValue: 30,
initialValue: 64,
validator: validators.greaterThanEqualZero,
},
"controller.session_timeout": {