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.
This commit is contained in:
parent
a6537c3e0f
commit
0bf9072cd1
|
@ -36,18 +36,25 @@ export async function put(
|
||||||
const updateDb = !cacheItem || cacheItem.lastWrite < Date.now() - writeRateMs
|
const updateDb = !cacheItem || cacheItem.lastWrite < Date.now() - writeRateMs
|
||||||
let output = doc
|
let output = doc
|
||||||
if (updateDb) {
|
if (updateDb) {
|
||||||
try {
|
const writeDb = async (toWrite: any) => {
|
||||||
// doc should contain the _id and _rev
|
// doc should contain the _id and _rev
|
||||||
const response = await db.put(doc)
|
const response = await db.put(toWrite)
|
||||||
output = {
|
output = {
|
||||||
...doc,
|
...doc,
|
||||||
_id: response.id,
|
_id: response.id,
|
||||||
_rev: response.rev,
|
_rev: response.rev,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await writeDb(doc)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
// ignore 409s, some other high speed write has hit it first, just move straight to caching
|
|
||||||
if (err.status !== 409) {
|
if (err.status !== 409) {
|
||||||
throw err
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue