From d169dce364958a2f82e55f557a5206c300db021c Mon Sep 17 00:00:00 2001 From: Alexander Onnikov Date: Tue, 3 Sep 2024 14:57:02 +0700 Subject: [PATCH 1/2] fix: retry requests to collaborator in case of failure Signed-off-by: Alexander Onnikov --- packages/collaborator-client/src/client.ts | 32 ++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/collaborator-client/src/client.ts b/packages/collaborator-client/src/client.ts index bca4a8c0b1..b358d411f9 100644 --- a/packages/collaborator-client/src/client.ts +++ b/packages/collaborator-client/src/client.ts @@ -69,6 +69,10 @@ class CollaboratorClientImpl implements CollaboratorClient { body: JSON.stringify({ method, documentId, payload }) }) + if (!res.ok) { + throw new Error('HTTP error ' + res.status) + } + const result = await res.json() if (result.error != null) { @@ -79,12 +83,16 @@ class CollaboratorClientImpl implements CollaboratorClient { } async getContent (document: CollaborativeDoc): Promise> { - const res = (await this.rpc(document, 'getContent', {})) as GetContentResponse + const res = await retry(3, async () => { + return (await this.rpc(document, 'getContent', {})) as GetContentResponse + }, 50) return res.content ?? {} } async updateContent (document: CollaborativeDoc, content: Record): Promise { - await this.rpc(document, 'updateContent', { content }) + await retry(3, async () => { + await this.rpc(document, 'updateContent', { content }) + }, 50) } async copyContent (source: CollaborativeDoc, target: CollaborativeDoc): Promise { @@ -92,3 +100,23 @@ class CollaboratorClientImpl implements CollaboratorClient { await this.updateContent(target, content) } } + +async function retry ( + retries: number, + op: () => Promise, + delay: number = 100 +): Promise { + let error: any + while (retries > 0) { + retries-- + try { + return await op() + } catch (err: any) { + error = err + if (retries !== 0) { + await new Promise((resolve) => setTimeout(resolve, delay)) + } + } + } + throw error +} From f459caa5783f7bc30fa853f98bff90141aa1b2be Mon Sep 17 00:00:00 2001 From: Alexander Onnikov Date: Tue, 3 Sep 2024 15:43:07 +0700 Subject: [PATCH 2/2] rush fast-format Signed-off-by: Alexander Onnikov --- packages/collaborator-client/src/client.ts | 26 +++++++++++++--------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/collaborator-client/src/client.ts b/packages/collaborator-client/src/client.ts index b358d411f9..c7ffa716a4 100644 --- a/packages/collaborator-client/src/client.ts +++ b/packages/collaborator-client/src/client.ts @@ -83,16 +83,24 @@ class CollaboratorClientImpl implements CollaboratorClient { } async getContent (document: CollaborativeDoc): Promise> { - const res = await retry(3, async () => { - return (await this.rpc(document, 'getContent', {})) as GetContentResponse - }, 50) + const res = await retry( + 3, + async () => { + return (await this.rpc(document, 'getContent', {})) as GetContentResponse + }, + 50 + ) return res.content ?? {} } async updateContent (document: CollaborativeDoc, content: Record): Promise { - await retry(3, async () => { - await this.rpc(document, 'updateContent', { content }) - }, 50) + await retry( + 3, + async () => { + await this.rpc(document, 'updateContent', { content }) + }, + 50 + ) } async copyContent (source: CollaborativeDoc, target: CollaborativeDoc): Promise { @@ -101,11 +109,7 @@ class CollaboratorClientImpl implements CollaboratorClient { } } -async function retry ( - retries: number, - op: () => Promise, - delay: number = 100 -): Promise { +async function retry (retries: number, op: () => Promise, delay: number = 100): Promise { let error: any while (retries > 0) { retries--