From 40b91af8007dd0a5d180eba37a8168ca12e013e2 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Wed, 9 Apr 2025 01:10:28 -0400 Subject: When booting a session, retry every five seconds if unable to send the request. This is intended to transparently resume the session (using `boot` to start over) after more serious connection interruptions. It interacts with the heartbeat timeout: we let the browser try to reconnect through `EventSource` on its own for up to 30 seconds, before intervening, closing the event source, and starting attempts to call `boot`. This covers both initial boot, which will now hang if the server is unavailable (sorry), and reconnection after an event timeout. No other operations are retried (particularly, sending a message is _not_ retried). --- ui/lib/retry.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 ui/lib/retry.js (limited to 'ui/lib/retry.js') diff --git a/ui/lib/retry.js b/ui/lib/retry.js new file mode 100644 index 0000000..777f1be --- /dev/null +++ b/ui/lib/retry.js @@ -0,0 +1,21 @@ +export async function retry(callback, retryCond, delay) { + while (true) { + try { + return await callback(); + } catch (err) { + if (retryCond(err)) { + await delay(); + } else { + throw err; + } + } + } +} + +export function delay(millis) { + return async () => await sleep(millis); +} + +function sleep(millis) { + return new Promise((resolve) => setTimeout(resolve, millis)); +} -- cgit v1.2.3 From 4d6cc62f0c9bb5d50720c83fb1ecbd0889d2acd3 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Wed, 23 Apr 2025 22:02:42 -0400 Subject: Naming improvements c/o Kit --- ui/lib/apiServer.js | 3 ++- ui/lib/retry.js | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'ui/lib/retry.js') diff --git a/ui/lib/apiServer.js b/ui/lib/apiServer.js index 7edddc3..cad8997 100644 --- a/ui/lib/apiServer.js +++ b/ui/lib/apiServer.js @@ -1,6 +1,7 @@ import axios from 'axios'; import * as r from './retry.js'; +import { timedDelay } from './retry.js'; export const apiServer = axios.create({ baseURL: '/api/' @@ -70,7 +71,7 @@ export class LoggedOut extends Error {} export class SetupRequired extends Error {} export async function retry(op) { - return await r.retry(op, isRetryable, r.delay(5000)); + return await r.retry(op, isRetryable, r.timedDelay(5000)); } function responseError(err) { diff --git a/ui/lib/retry.js b/ui/lib/retry.js index 777f1be..a2cff65 100644 --- a/ui/lib/retry.js +++ b/ui/lib/retry.js @@ -1,9 +1,9 @@ -export async function retry(callback, retryCond, delay) { +export async function retry(callback, shouldRetry, delay) { while (true) { try { return await callback(); } catch (err) { - if (retryCond(err)) { + if (shouldRetry(err)) { await delay(); } else { throw err; @@ -12,7 +12,7 @@ export async function retry(callback, retryCond, delay) { } } -export function delay(millis) { +export function timedDelay(millis) { return async () => await sleep(millis); } -- cgit v1.2.3