1
0
Fork 0
mirror of synced 2024-07-06 15:00:49 +12:00

Do not use lock

This commit is contained in:
Adria Navarro 2024-03-05 17:15:50 +01:00
parent 2b25f9f0cb
commit 4fe7e67dd5
2 changed files with 4 additions and 37 deletions

View file

@ -1,7 +1,6 @@
import BaseCache from "./base"
import { getDocWritethroughClient } from "../redis/init"
import { AnyDocument, Database, LockName, LockType } from "@budibase/types"
import * as locks from "../redis/redlockImpl"
import { AnyDocument, Database } from "@budibase/types"
import { JobQueue, createQueue } from "../queue"
import * as context from "../context"
@ -17,7 +16,6 @@ async function getCache() {
}
interface ProcessDocMessage {
tenantId: string
dbName: string
docId: string
cacheKeyPrefix: string
@ -28,34 +26,8 @@ export const docWritethroughProcessorQueue = createQueue<ProcessDocMessage>(
)
docWritethroughProcessorQueue.process(async message => {
const { tenantId, cacheKeyPrefix } = message.data
await context.doInTenant(tenantId, async () => {
const lockResponse = await locks.doWithLock(
{
type: LockType.TRY_ONCE,
name: LockName.PERSIST_WRITETHROUGH,
resource: cacheKeyPrefix,
ttl: 15000,
},
async () => {
await persistToDb(message.data)
console.log("DocWritethrough persisted", { data: message.data })
}
)
if (!lockResponse.executed) {
if (
lockResponse.reason !==
locks.UnsuccessfulRedlockExecutionReason.LockTakenWithTryOnce
) {
console.error("Error persisting docWritethrough", {
data: message.data,
})
throw "Error persisting docWritethrough"
}
console.log(`Ignoring redlock conflict in write-through cache`)
}
})
await persistToDb(message.data)
console.log("DocWritethrough persisted", { data: message.data })
})
export async function persistToDb({
@ -94,7 +66,6 @@ export class DocWritethrough {
private db: Database
private _docId: string
private writeRateMs: number
private tenantId: string
private cacheKeyPrefix: string
@ -103,7 +74,6 @@ export class DocWritethrough {
this._docId = docId
this.writeRateMs = writeRateMs
this.cacheKeyPrefix = `${this.db.name}:${this.docId}`
this.tenantId = context.getTenantId()
}
get docId() {
@ -117,7 +87,6 @@ export class DocWritethrough {
docWritethroughProcessorQueue.add(
{
tenantId: this.tenantId,
dbName: this.db.name,
docId: this.docId,
cacheKeyPrefix: this.cacheKeyPrefix,

View file

@ -47,9 +47,7 @@ describe("docWritethrough", () => {
beforeEach(async () => {
resetTime()
documentId = structures.uuid()
await config.doInTenant(async () => {
docWritethrough = new DocWritethrough(db, documentId, WRITE_RATE_MS)
})
docWritethrough = new DocWritethrough(db, documentId, WRITE_RATE_MS)
})
it("patching will not persist if timeout does not hit", async () => {