mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 00:13:12 +09:00
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:
1 parent
e9a11013a4
commit
19569aefa4
6 files changed
+85
-54
No files matched your search
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user