Do not use lock

This commit is contained in:
Adria Navarro 2024-03-05 17:15:50 +01:00
parent e584d82e6f
commit f5e2dc7a27
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`)
}
})
})
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,10 +47,8 @@ describe("docWritethrough", () => {
beforeEach(async () => {
resetTime()
documentId = structures.uuid()
await config.doInTenant(async () => {
docWritethrough = new DocWritethrough(db, documentId, WRITE_RATE_MS)
})
})
it("patching will not persist if timeout does not hit", async () => {
await config.doInTenant(async () => {