From 920764277926209da27543dc130e1a6449682831 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 24 Jun 2022 17:41:17 +0100 Subject: [PATCH] Fixing an issue where periodically the cache state of the couch revision gets out of sync and need to update it for cache to be able to write again. --- packages/backend-core/src/cache/writethrough.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/backend-core/src/cache/writethrough.ts b/packages/backend-core/src/cache/writethrough.ts index b9817d7d93..e11ca0acaa 100644 --- a/packages/backend-core/src/cache/writethrough.ts +++ b/packages/backend-core/src/cache/writethrough.ts @@ -36,18 +36,25 @@ export async function put( const updateDb = !cacheItem || cacheItem.lastWrite < Date.now() - writeRateMs let output = doc if (updateDb) { - try { + const writeDb = async (toWrite: any) => { // doc should contain the _id and _rev - const response = await db.put(doc) + const response = await db.put(toWrite) output = { ...doc, _id: response.id, _rev: response.rev, } + } + try { + await writeDb(doc) } catch (err: any) { - // ignore 409s, some other high speed write has hit it first, just move straight to caching if (err.status !== 409) { throw err + } else { + // get the rev, update over it - this is risky, may change in future + const readDoc = await db.get(doc._id) + doc._rev = readDoc._rev + await writeDb(doc) } } }