2022-07-14 10:08:50 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const assert = require('assert');
|
|
|
|
const urlJoin = require('url-join');
|
|
|
|
const asyncHandler = require('../lib/express-async-handler');
|
2023-05-01 23:39:01 -06:00
|
|
|
const RouteTimeoutAbortError = require('../lib/errors/route-timeout-abort-error');
|
|
|
|
const UserClosedConnectionAbortError = require('../lib/errors/user-closed-connection-abort-error');
|
2022-07-14 10:08:50 -06:00
|
|
|
const { getSerializableSpans, getActiveTraceId } = require('../tracing/tracing-middleware');
|
2023-05-01 23:39:01 -06:00
|
|
|
const { SemanticAttributes } = require('@opentelemetry/semantic-conventions');
|
2022-07-14 10:08:50 -06:00
|
|
|
const sanitizeHtml = require('../lib/sanitize-html');
|
2023-05-01 14:13:16 -06:00
|
|
|
const renderPageHtml = require('../hydrogen-render/render-page-html');
|
2022-07-14 10:08:50 -06:00
|
|
|
|
|
|
|
const config = require('../lib/config');
|
|
|
|
const basePath = config.get('basePath');
|
|
|
|
assert(basePath);
|
|
|
|
const requestTimeoutMs = config.get('requestTimeoutMs');
|
|
|
|
assert(requestTimeoutMs);
|
2023-05-11 15:34:16 -06:00
|
|
|
const workaroundCloudflare504TimeoutErrors = config.get('workaroundCloudflare504TimeoutErrors');
|
2022-07-14 10:08:50 -06:00
|
|
|
|
|
|
|
// Based off of the `connect-timeout` middleware,
|
|
|
|
// https://github.com/expressjs/timeout/blob/f2f520f335f2f2ae255d4778e908e8d38e3a4e68/index.js
|
|
|
|
async function timeoutMiddleware(req, res, next) {
|
2023-05-01 23:39:01 -06:00
|
|
|
req.abortController = new AbortController();
|
|
|
|
req.abortSignal = req.abortController.signal;
|
|
|
|
|
2022-07-14 10:08:50 -06:00
|
|
|
const timeoutId = setTimeout(() => {
|
2023-05-01 23:39:01 -06:00
|
|
|
// Signal to downstream middlewares/routes that they should stop processing/fetching
|
|
|
|
// things since we timed out (downstream consumers need to respect `req.abortSignal`)
|
|
|
|
req.abortController.abort(
|
|
|
|
new RouteTimeoutAbortError(
|
|
|
|
`Timed out after ${requestTimeoutMs}ms while trying to respond to route ${req.originalUrl}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2022-07-14 10:08:50 -06:00
|
|
|
const traceId = getActiveTraceId();
|
|
|
|
const serializableSpans = getSerializableSpans();
|
|
|
|
|
|
|
|
let humanReadableSpans;
|
|
|
|
if (serializableSpans.length > 0) {
|
|
|
|
humanReadableSpans = serializableSpans.map((serializableSpan) => {
|
2023-05-01 23:39:01 -06:00
|
|
|
const method = serializableSpan.attributes[SemanticAttributes.HTTP_METHOD];
|
|
|
|
const url =
|
|
|
|
serializableSpan.attributes[SemanticAttributes.HTTP_TARGET] ||
|
|
|
|
serializableSpan.attributes[SemanticAttributes.HTTP_URL];
|
|
|
|
const statusCode = serializableSpan.attributes[SemanticAttributes.HTTP_STATUS_CODE];
|
2022-07-14 10:08:50 -06:00
|
|
|
|
|
|
|
let durationString = `request is still running (${
|
|
|
|
Date.now() - serializableSpan.startTimeInMs
|
|
|
|
}ms so far)`;
|
|
|
|
if (serializableSpan.durationInMs) {
|
|
|
|
durationString = `took ${serializableSpan.durationInMs}ms`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `<li class="tracing-span-list-item">
|
|
|
|
<div class="tracing-span-item-http-details">${statusCode ?? '🏃'}: ${method} ${url}</div>
|
|
|
|
<div class="tracing-span-item-sub-details">${durationString}</div>
|
|
|
|
</li>`;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const noTracingDataAvailableItem = `<li class="tracing-span-list-item">
|
|
|
|
<div class="tracing-span-item-http-details">No tracing data available</div>
|
|
|
|
</li>`;
|
|
|
|
|
|
|
|
humanReadableSpans = [noTracingDataAvailableItem];
|
|
|
|
}
|
|
|
|
|
2023-05-01 14:13:16 -06:00
|
|
|
const bodyHtml = `
|
2022-07-14 10:08:50 -06:00
|
|
|
${/* We add the .hydrogen class here just to get normal body styles */ ''}
|
2023-05-01 14:13:16 -06:00
|
|
|
<div class="hydrogen">
|
2022-07-14 10:08:50 -06:00
|
|
|
<h1>504: Server timeout</h1>
|
|
|
|
<p>Server was unable to respond in time (${requestTimeoutMs / 1000}s)</p>
|
|
|
|
<h3>These are the external API requests that made it slow:</h3>
|
|
|
|
${sanitizeHtml(`<ul class="tracing-span-list">
|
|
|
|
${humanReadableSpans.join('\n')}
|
|
|
|
</ul>`)}
|
|
|
|
|
|
|
|
${sanitizeHtml(
|
|
|
|
`<h2>Trace ID: <span class="heading-sub-detail">${
|
|
|
|
traceId ?? `none (tracing is probably not enabled)`
|
|
|
|
}</span></h2>`
|
|
|
|
)}
|
2023-05-01 14:13:16 -06:00
|
|
|
</div>
|
2022-07-14 10:08:50 -06:00
|
|
|
`;
|
|
|
|
|
2023-05-01 14:13:16 -06:00
|
|
|
const pageOptions = {
|
2023-07-14 14:52:35 -06:00
|
|
|
title: `Server timeout - Matrix Viewer`,
|
2023-05-04 21:46:09 -06:00
|
|
|
description: `Unable to respond in time (${requestTimeoutMs / 1000}s)`,
|
2023-05-01 14:13:16 -06:00
|
|
|
entryPoint: 'client/js/entry-client-timeout.js',
|
2023-06-22 00:50:55 -06:00
|
|
|
locationUrl: urlJoin(basePath, req.originalUrl),
|
2023-05-01 14:13:16 -06:00
|
|
|
// We don't have a Matrix room so we don't know whether or not to index. Just choose
|
|
|
|
// a safe-default of false.
|
|
|
|
shouldIndex: false,
|
|
|
|
cspNonce: res.locals.cspNonce,
|
|
|
|
};
|
|
|
|
|
|
|
|
const pageHtml = renderPageHtml({
|
|
|
|
pageOptions,
|
|
|
|
bodyHtml,
|
|
|
|
vmRenderContext: {
|
|
|
|
config: {
|
|
|
|
basePath,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-05-11 15:24:58 -06:00
|
|
|
// The most semantic HTTP status code to return here is a 504 Gateway timeout but if
|
2023-07-14 14:52:35 -06:00
|
|
|
// you use Cloudflare in front of the viewer, it will serve its own
|
2023-05-11 15:24:58 -06:00
|
|
|
// Cloudflare-branded 504 page if your own origin server responds with a 504. And
|
|
|
|
// the only way to disable this functionality is to have an Enterprise Cloudflare
|
|
|
|
// plan. So to workaround this, we return a 500 instead. Relevant Cloudflare docs:
|
|
|
|
// https://developers.cloudflare.com/support/troubleshooting/cloudflare-errors/troubleshooting-cloudflare-5xx-errors/#502504-from-your-origin-web-server
|
|
|
|
//
|
|
|
|
// We want to show our own timeout page because it has more information about what
|
|
|
|
// went wrong (e.g. which external Matrix API requests were slow).
|
2023-05-11 15:34:16 -06:00
|
|
|
res.status(workaroundCloudflare504TimeoutErrors ? 500 : 504);
|
2023-05-11 15:24:58 -06:00
|
|
|
|
2022-07-14 10:08:50 -06:00
|
|
|
res.set('Content-Type', 'text/html');
|
|
|
|
|
|
|
|
res.send(pageHtml);
|
|
|
|
}, requestTimeoutMs);
|
|
|
|
|
|
|
|
res.on('finish', function () {
|
2023-05-01 23:39:01 -06:00
|
|
|
// Clear the timeout if the response finishes naturally
|
2022-07-14 10:08:50 -06:00
|
|
|
clearTimeout(timeoutId);
|
|
|
|
});
|
|
|
|
|
2023-05-01 23:39:01 -06:00
|
|
|
req.on('close', function () {
|
|
|
|
// Signal to downstream middlewares/routes that they should stop processing/fetching
|
|
|
|
// things since the user closed the connection before we sent a response (downstream
|
|
|
|
// consumers need to respect `req.abortSignal`)
|
|
|
|
//
|
|
|
|
// This is a bit adjacent to "timeouts" but fits easily enough here (this could be a
|
|
|
|
// separate middleware).
|
|
|
|
req.abortController.abort(
|
|
|
|
new UserClosedConnectionAbortError(`User closed connection before we could respond`)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-07-14 10:08:50 -06:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = asyncHandler(timeoutMiddleware);
|