Skip to content

Commit

Permalink
limit the maximum number of requests made per second
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
lovasoa committed Nov 15, 2023
1 parent 4f7ea9b commit c1a5628
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dezoomers/arts-culture.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down
13 changes: 10 additions & 3 deletions zoommanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "%";
Expand Down Expand Up @@ -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);
};

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -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 " +
Expand Down

0 comments on commit c1a5628

Please sign in to comment.