Skip to content

Commit

Permalink
Proper failover #15
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-milovidov committed Apr 21, 2024
1 parent 22217dd commit 02804b1
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,37 @@
}
}

class CustomError extends Error {
constructor(message, payload) {
super(message);
this.name = this.constructor.name;
this.payload = payload;
}
}

/// Returns the first successful (HTTP 2xx) response or an error if all of them failed.
/// Cancels other requests on first success.
function fetchAny(urls, params) {
const abort_controllers = {};
urls.forEach(url => abort_controllers[url] = new AbortController);

const requests = urls.map(url => {
const abort_controller = abort_controllers[url];
params.signal = abort_controller.signal;
return fetch(url, params).then(response => {
if (response.ok) {
urls.filter(other => other != url).map(other => abort_controllers[other].abort());
return response;
}
throw new CustomError(response.statusText, response);
}).catch(error => {
return Promise.reject(error);
})
});

return Promise.any(requests);
}

document.querySelector('form').addEventListener('change', e => {
cached_tiles = {};
main_layer_sample100.redraw();
Expand Down Expand Up @@ -449,20 +480,22 @@
`&param_z=${coords.z - 2}&param_x=${coords.x}&param_y=${coords.y}`;

progress_update_period = 1;
const response = await Promise.any(hosts.map(host => fetch(url(host), { method: 'POST', body: sql })));

if (!response.ok) {
try {
const response = await fetchAny(hosts.map(host => url(host)), { method: 'POST', body: sql });
buf = await response.arrayBuffer();
cached_tiles[key][priority] = buf;
} catch (error) {
const response = error.errors[0].payload;
const text = await response.text();
if (!text.includes('QUERY_WAS_CANCELLED') && !text.includes('QUERY_WITH_SAME_ID_IS_ALREADY_RUNNING')) {
if (!text.includes('QUERY_WAS_CANCELLED') &&
!text.includes('QUERY_WITH_SAME_ID_IS_ALREADY_RUNNING')) {
let err = document.getElementById('error');
err.textContent = text;
err.style.display = 'block';
}
return;
}

buf = await response.arrayBuffer();
cached_tiles[key][priority] = buf;
}

let ctx = tile.getContext('2d');
Expand Down Expand Up @@ -690,7 +723,7 @@
`&param_bottom=${Math.max(selected_box[0].y, selected_box[1].y)}`;

progress_update_period = 1;
const response = await Promise.any(hosts.map(host => fetch(url(host), { method: 'POST', body: sql })));
const response = await fetchAny(hosts.map(host => url(host)), { method: 'POST', body: sql });

if (!response.ok) {
const text = await response.text();
Expand Down Expand Up @@ -1057,7 +1090,7 @@
const sql = `SELECT text FROM saved_queries WHERE hash = unhex({hash:String}) LIMIT 1`;
const hosts = getHosts(null);
const url = host => `${host}/?user=website_saved_queries&default_format=JSON&param_hash=${hash}`;
const response = await Promise.any(hosts.map(host => fetch(url(host), { method: 'POST', body: sql })));
const response = await fetchAny(hosts.map(host => url(host)), { method: 'POST', body: sql });
const data = await response.json();
return data.data[0].text;
}
Expand Down

0 comments on commit 02804b1

Please sign in to comment.