Merge pull/4524: Add prefetchTimeout

This commit is contained in:
Reto Brunner 2022-04-27 18:22:49 +02:00
commit aa7db1e7f7
2 changed files with 24 additions and 1 deletions

View File

@ -160,6 +160,20 @@ module.exports = {
// This value is set to `50` kilobytes by default. // This value is set to `50` kilobytes by default.
prefetchMaxSearchSize: 50, prefetchMaxSearchSize: 50,
// ### `prefetchTimeout`
//
// When `prefetch` is enabled, this value sets the number of milliseconds
// before The Lounge gives up attempting to fetch a link. This can be useful
// if you've increased the `prefetchMaxImageSize`.
//
// Take caution, however, that an inordinately large value may lead to
// performance issues or even a denial of service, since The Lounge will not
// be able to clean up outgoing connections as quickly. Usually the default
// value is appropriate, so only change it if necessary.
//
// This value is set to `5000` milliseconds by default.
prefetchTimeout: 5000,
// ### `fileUpload` // ### `fileUpload`
// //
// Allow uploading files to the server hosting The Lounge. // Allow uploading files to the server hosting The Lounge.

View File

@ -10,6 +10,7 @@ const storage = require("../storage");
const currentFetchPromises = new Map(); const currentFetchPromises = new Map();
const imageTypeRegex = /^image\/.+/; const imageTypeRegex = /^image\/.+/;
const mediaTypeRegex = /^(audio|video)\/.+/; const mediaTypeRegex = /^(audio|video)\/.+/;
const log = require("../../log");
module.exports = function (client, chan, msg, cleanText) { module.exports = function (client, chan, msg, cleanText) {
if (!Helper.config.prefetch) { if (!Helper.config.prefetch) {
@ -381,6 +382,14 @@ function fetch(uri, headers) {
return promise; return promise;
} }
const prefetchTimeout = Helper.config.prefetchTimeout;
if (!prefetchTimeout) {
log.warn(
"prefetchTimeout is missing from your The Lounge configuration, defaulting to 5000 ms"
);
}
promise = new Promise((resolve, reject) => { promise = new Promise((resolve, reject) => {
let buffer = Buffer.from(""); let buffer = Buffer.from("");
let contentLength = 0; let contentLength = 0;
@ -390,7 +399,7 @@ function fetch(uri, headers) {
try { try {
const gotStream = got.stream(uri, { const gotStream = got.stream(uri, {
retry: 0, retry: 0,
timeout: 5000, timeout: prefetchTimeout || 5000, // milliseconds
headers: getRequestHeaders(headers), headers: getRequestHeaders(headers),
https: { https: {
rejectUnauthorized: false, rejectUnauthorized: false,