From f71e1ac03a784e89105efe75c93a14448b5caf55 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 29 Nov 2023 13:56:25 +0100 Subject: [PATCH] Allow locks without TTL --- packages/backend-core/src/redis/redlockImpl.ts | 17 +++++++++++++++-- packages/types/src/sdk/locks.ts | 4 ++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/backend-core/src/redis/redlockImpl.ts b/packages/backend-core/src/redis/redlockImpl.ts index 266f1fe989..cccb981399 100644 --- a/packages/backend-core/src/redis/redlockImpl.ts +++ b/packages/backend-core/src/redis/redlockImpl.ts @@ -4,6 +4,7 @@ import { LockOptions, LockType } from "@budibase/types" import * as context from "../context" import env from "../environment" import { logWarn } from "../logging" +import { Duration } from "../utils" async function getClient( type: LockType, @@ -105,12 +106,21 @@ export async function doWithLock( task: () => Promise ): Promise> { const redlock = await getClient(opts.type, opts.customOptions) - let lock + let lock: Redlock.Lock | undefined + let interval try { const name = getLockName(opts) + let ttl = opts.ttl || Duration.fromSeconds(15).toMs() + // create the lock - lock = await redlock.lock(name, opts.ttl) + lock = await redlock.lock(name, ttl) + + if (!opts.ttl) { + interval = setInterval(() => { + lock!.extend(ttl) + }, ttl / 2) + } // perform locked task // need to await to ensure completion before unlocking @@ -134,5 +144,8 @@ export async function doWithLock( if (lock) { await lock.unlock() } + if (interval) { + clearInterval(interval) + } } } diff --git a/packages/types/src/sdk/locks.ts b/packages/types/src/sdk/locks.ts index a35e7b379b..080574b735 100644 --- a/packages/types/src/sdk/locks.ts +++ b/packages/types/src/sdk/locks.ts @@ -36,9 +36,9 @@ export interface LockOptions { */ name: LockName /** - * The ttl to auto-expire the lock if not unlocked manually + * The ttl to auto-expire the lock if not unlocked manually. If undefined, the lock will be autoextending while the process is running. */ - ttl: number + ttl?: number /** * The individual resource to lock. This is useful for locking around very specific identifiers, e.g. a document that is prone to conflicts */