Autoextend without ttl

This commit is contained in:
Adria Navarro 2023-11-30 13:54:42 +01:00
parent e81e37b613
commit a8ac4eed6d
2 changed files with 22 additions and 10 deletions

View File

@ -2,9 +2,9 @@ import Redlock from "redlock"
import { getLockClient } from "./init" import { getLockClient } from "./init"
import { LockOptions, LockType } from "@budibase/types" import { LockOptions, LockType } from "@budibase/types"
import * as context from "../context" import * as context from "../context"
import env from "../environment"
import { logWarn } from "../logging" import { logWarn } from "../logging"
import { utils } from "@budibase/shared-core" import { utils } from "@budibase/shared-core"
import { Duration } from "../utils"
async function getClient( async function getClient(
type: LockType, type: LockType,
@ -111,8 +111,13 @@ export async function doWithLock<T>(
try { try {
const name = getLockName(opts) const name = getLockName(opts)
const ttl =
opts.type === LockType.AUTO_EXTEND
? Duration.fromSeconds(10).toMs()
: opts.ttl
// create the lock // create the lock
lock = await redlock.lock(name, opts.ttl) lock = await redlock.lock(name, ttl)
if (opts.type === LockType.AUTO_EXTEND) { if (opts.type === LockType.AUTO_EXTEND) {
// We keep extending the lock while the task is running // We keep extending the lock while the task is running
@ -120,7 +125,7 @@ export async function doWithLock<T>(
timeout = setTimeout(async () => { timeout = setTimeout(async () => {
let isExpired = false let isExpired = false
try { try {
lock = await lock!.extend(opts.ttl) lock = await lock!.extend(ttl)
} catch (err: any) { } catch (err: any) {
isExpired = err.message.includes("Cannot extend lock on resource") isExpired = err.message.includes("Cannot extend lock on resource")
if (isExpired) { if (isExpired) {
@ -133,7 +138,7 @@ export async function doWithLock<T>(
if (!isExpired) { if (!isExpired) {
extendInIntervals() extendInIntervals()
} }
}, opts.ttl / 2) }, ttl / 2)
} }
extendInIntervals() extendInIntervals()

View File

@ -22,7 +22,7 @@ export enum LockName {
QUOTA_USAGE_EVENT = "quota_usage_event", QUOTA_USAGE_EVENT = "quota_usage_event",
} }
export interface LockOptions { export type LockOptions = {
/** /**
* The lock type determines which client to use * The lock type determines which client to use
*/ */
@ -36,10 +36,6 @@ export interface LockOptions {
* The name for the lock * The name for the lock
*/ */
name: LockName name: LockName
/**
* The ttl to auto-expire the lock if not unlocked manually
*/
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 * The individual resource to lock. This is useful for locking around very specific identifiers, e.g. a document that is prone to conflicts
*/ */
@ -48,4 +44,15 @@ export interface LockOptions {
* This is a system-wide lock - don't use tenancy in lock key * This is a system-wide lock - don't use tenancy in lock key
*/ */
systemLock?: boolean systemLock?: boolean
} } & (
| {
/**
* The ttl to auto-expire the lock if not unlocked manually
*/
ttl: number
type: Exclude<LockType, LockType.AUTO_EXTEND>
}
| {
type: LockType.AUTO_EXTEND
}
)