Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: retry requests to collaborator in case of failure #6468

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions packages/collaborator-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -79,16 +83,40 @@ class CollaboratorClientImpl implements CollaboratorClient {
}

async getContent (document: CollaborativeDoc): Promise<Record<string, Markup>> {
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<string, Markup>): Promise<void> {
await this.rpc(document, 'updateContent', { content })
await retry(3, async () => {
await this.rpc(document, 'updateContent', { content })
}, 50)
}

async copyContent (source: CollaborativeDoc, target: CollaborativeDoc): Promise<void> {
const content = await this.getContent(source)
await this.updateContent(target, content)
}
}

async function retry<T> (
retries: number,
op: () => Promise<T>,
delay: number = 100
): Promise<T> {
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
}
Loading