From c1a562853dc9cf8aca1d0c54a168bb974e78f75f Mon Sep 17 00:00:00 2001 From: lovasoa Date: Tue, 14 Nov 2023 19:59:29 +0100 Subject: [PATCH] limit the maximum number of requests made per second This should avoid crashing or getting banned from the server from which the tiles are downloaded (which can be the same as the server from which dezoomify is served, when using google arts for instance) --- dezoomers/arts-culture.js | 2 +- zoommanager.js | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/dezoomers/arts-culture.js b/dezoomers/arts-culture.js index c322f87..46ae05e 100644 --- a/dezoomers/arts-culture.js +++ b/dezoomers/arts-culture.js @@ -48,7 +48,7 @@ async function getTileURL( const tile_path = await compute_signed_path(path, token, x, y, z); const tile_url = ZoomManager.resolveRelative("/"+tile_path, origin); const buffer = await new Promise(accept => - ZoomManager.getFile(tile_url, { type: 'binary' }, accept) + ZoomManager.getFile(tile_url, { type: 'binary', is_tile: true }, accept) ); const tile = await decrypt_image({ buffer }); const blob = new Blob([tile], { type: "image/jpeg" }); diff --git a/zoommanager.js b/zoommanager.js index fe62aca..ec58814 100644 --- a/zoommanager.js +++ b/zoommanager.js @@ -132,6 +132,10 @@ Update the state of the progress bar. @param {String} description current state description */ UI.updateProgress = function (percent, text) { + if (!percent) { + document.getElementById("percent").innerHTML = text; + return; + } percent = parseInt(percent); document.getElementById("percent").innerHTML = text + ' (' + percent + "%)"; document.getElementById("progressbar").style.width = percent + "%"; @@ -311,13 +315,15 @@ ZoomManager.defaultRender = function (data) { nextTile(); }; +ZoomManager.MAX_REQUESTS_PER_SECOND = 5; + /** @function nextTick Call a function, but not immediatly @param {Function} f - the function to call */ ZoomManager.nextTick = function (f) { - return setTimeout(f, 3); + return setTimeout(f, 1000 / ZoomManager.MAX_REQUESTS_PER_SECOND); }; /** @@ -388,7 +394,7 @@ ZoomManager.open = function (url) { /** Call callback with the contents of the page at url @param {string} url -@param {{type:String, allow_failure?: boolean, error_callback: (err:string)=>any}} params +@param {{type:String, allow_failure?: boolean, error_callback: (err:string)=>any, is_tile?: boolean}} params @param {fileCallback} callback - callback to call when the file is loaded */ ZoomManager.getFile = function (url, params, callback) { @@ -415,7 +421,8 @@ ZoomManager.getFile = function (url, params, callback) { xhr.open("GET", requesturl, true); xhr.onloadstart = function () { - ZoomManager.updateProgress(1, "Sent a request in order to get information about the image..."); + if (!params.is_tile) + ZoomManager.updateProgress(0, "Sent a request in order to get information about the image..."); }; xhr.onerror = function (e) { onerror("Unable to connect to the proxy server " +