Skip to content

Commit

Permalink
Retry on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Nov 14, 2023
1 parent 121ff48 commit e10a3f3
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/backend/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,31 @@ class HandlerConnection implements Connection {

// Target

async mutate(mutations: Array<Mutation>): Promise<{commitHash: string}> {
async mutate(
mutations: Array<Mutation>,
retry = 0
): Promise<{commitHash: string}> {
const {target, media, db} = this.handler.options
if (!target) throw new Error('Target not available')
if (!media) throw new Error('Media not available')
const changeSet = this.handler.changes.create(mutations)
const {commitHash: fromCommitHash} = await this.handler.syncPending()
const {commitHash: toCommitHash} = await target.mutate(
{commitHash: fromCommitHash, mutations: changeSet},
this.ctx
)
let toCommitHash: string
try {
const result = await target.mutate(
{commitHash: fromCommitHash, mutations: changeSet},
this.ctx
)
toCommitHash = result.commitHash
} catch (error: any) {
if ('expectedCommitHash' in error) {
// Attempt again after syncing
// Todo: this needs to be handled differently
if (retry >= 3) throw error
return this.mutate(mutations, retry + 1)
}
throw error
}
await db.applyMutations(mutations, toCommitHash)
const tasks = []
for (const mutation of mutations) {
Expand Down

0 comments on commit e10a3f3

Please sign in to comment.