velocious 1.0.756 → 1.0.758
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/background-jobs/main.js +35 -3
- package/build/database/advisory-lock-lock-server-backend.js +52 -19
- package/build/src/background-jobs/main.d.ts +14 -0
- package/build/src/background-jobs/main.d.ts.map +1 -1
- package/build/src/background-jobs/main.js +35 -4
- package/build/src/database/advisory-lock-lock-server-backend.d.ts +16 -9
- package/build/src/database/advisory-lock-lock-server-backend.d.ts.map +1 -1
- package/build/src/database/advisory-lock-lock-server-backend.js +55 -20
- package/package.json +1 -1
- package/src/background-jobs/main.js +35 -3
- package/src/database/advisory-lock-lock-server-backend.js +52 -19
|
@@ -11,7 +11,7 @@ import {AdvisoryLockServiceUnavailableError} from "./advisory-lock-errors.js"
|
|
|
11
11
|
* @property {() => Promise<void>} connect - Opens the transport.
|
|
12
12
|
* @property {(type: string, args: {onMessage: (body: Record<string, unknown>) => void, params: Record<string, unknown>, timeoutMs: number}) => LockServerConnectionLike} openConnection - Opens the 1:1 connection.
|
|
13
13
|
* @property {() => Promise<void> | void} [close] - Closes the transport.
|
|
14
|
-
* @typedef {{epoch: number, name: string, timer: ReturnType<typeof
|
|
14
|
+
* @typedef {{epoch: number, name: string, timer: ReturnType<typeof setTimeout> | null, leaseExpiresAt: number, renewing: boolean}} LockServerHold
|
|
15
15
|
* @typedef {{status: "acquired", epoch: number, leaseExpiresAt: number} | {status: "already-held", epoch: number, leaseExpiresAt: number} | {status: "busy"} | {status: "timeout"}} LockServerAcquireResult
|
|
16
16
|
*/
|
|
17
17
|
|
|
@@ -74,7 +74,7 @@ export default class AdvisoryLockLockServerBackend {
|
|
|
74
74
|
this.connectPromise = null
|
|
75
75
|
/** @type {{reject: (error: Error) => void, resolve: () => void, timer: ReturnType<typeof setTimeout> | null} | null} */
|
|
76
76
|
this.helloWaiter = null
|
|
77
|
-
/** @type {Map<string,
|
|
77
|
+
/** @type {Map<string, LockServerHold>} */
|
|
78
78
|
this.holds = new Map()
|
|
79
79
|
/** @type {Map<string, {currentEpoch: number | null, epoch: number | null}>} */
|
|
80
80
|
this.fencedHolds = new Map()
|
|
@@ -231,7 +231,7 @@ export default class AdvisoryLockLockServerBackend {
|
|
|
231
231
|
if (!hold) return
|
|
232
232
|
|
|
233
233
|
this.holds.delete(name)
|
|
234
|
-
|
|
234
|
+
if (hold.timer) clearTimeout(hold.timer)
|
|
235
235
|
|
|
236
236
|
if (!this.connection) return
|
|
237
237
|
|
|
@@ -381,7 +381,7 @@ export default class AdvisoryLockLockServerBackend {
|
|
|
381
381
|
if (!hold) return
|
|
382
382
|
|
|
383
383
|
this.holds.delete(name)
|
|
384
|
-
|
|
384
|
+
if (hold.timer) clearTimeout(hold.timer)
|
|
385
385
|
this.fencedHolds.set(name, {currentEpoch: Number(body.epoch ?? 0), epoch: hold.epoch})
|
|
386
386
|
|
|
387
387
|
return
|
|
@@ -458,15 +458,34 @@ export default class AdvisoryLockLockServerBackend {
|
|
|
458
458
|
startRenewal(name, epoch) {
|
|
459
459
|
const existing = this.holds.get(name)
|
|
460
460
|
|
|
461
|
-
if (existing)
|
|
461
|
+
if (existing?.timer) clearTimeout(existing.timer)
|
|
462
462
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
463
|
+
this.holds.set(name, {epoch, leaseExpiresAt: Date.now() + this.leaseMs, name, renewing: false, timer: null})
|
|
464
|
+
this.scheduleRenewal(name, epoch)
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Schedules the next lease renewal. A one-shot timer prevents overlapping
|
|
469
|
+
* renewal requests: under transport pressure, the old interval could start
|
|
470
|
+
* another request before the previous one settled, and one transient request
|
|
471
|
+
* timeout then incorrectly converted a still-valid lease into a fenced hold.
|
|
472
|
+
* @param {string} name - Lock name.
|
|
473
|
+
* @param {number} epoch - Granted epoch.
|
|
474
|
+
* @param {number} [delayMs] - Delay before the next renewal.
|
|
475
|
+
* @returns {void}
|
|
476
|
+
*/
|
|
477
|
+
scheduleRenewal(name, epoch, delayMs = Math.max(250, Math.floor(this.leaseMs / 3))) {
|
|
478
|
+
const hold = this.holds.get(name)
|
|
467
479
|
|
|
468
|
-
|
|
469
|
-
|
|
480
|
+
if (!hold || hold.epoch !== epoch) return
|
|
481
|
+
|
|
482
|
+
if (hold.timer) clearTimeout(hold.timer)
|
|
483
|
+
|
|
484
|
+
hold.timer = setTimeout(() => {
|
|
485
|
+
hold.timer = null
|
|
486
|
+
void this.renew(name, epoch)
|
|
487
|
+
}, delayMs)
|
|
488
|
+
hold.timer.unref?.()
|
|
470
489
|
}
|
|
471
490
|
|
|
472
491
|
/**
|
|
@@ -478,34 +497,48 @@ export default class AdvisoryLockLockServerBackend {
|
|
|
478
497
|
async renew(name, epoch) {
|
|
479
498
|
const hold = this.holds.get(name)
|
|
480
499
|
|
|
481
|
-
if (!hold || !this.connection) return
|
|
500
|
+
if (!hold || hold.epoch !== epoch || hold.renewing || !this.connection) return
|
|
501
|
+
|
|
502
|
+
hold.renewing = true
|
|
482
503
|
|
|
483
504
|
try {
|
|
484
|
-
const
|
|
505
|
+
const remainingMs = Math.max(1, hold.leaseExpiresAt - Date.now())
|
|
506
|
+
const response = await this.request("renew", {epoch, name}, Math.max(1, Math.min(this.connectTimeoutMs * 2, remainingMs)))
|
|
485
507
|
const status = String(response.status)
|
|
486
508
|
|
|
487
509
|
if (status === "renewed") {
|
|
488
510
|
hold.leaseExpiresAt = Number(response.leaseExpiresAt)
|
|
511
|
+
this.scheduleRenewal(name, epoch)
|
|
489
512
|
|
|
490
513
|
return
|
|
491
514
|
}
|
|
492
515
|
|
|
493
516
|
if (status === "lost" || status === "not-found") {
|
|
494
517
|
this.holds.delete(name)
|
|
495
|
-
|
|
518
|
+
if (hold.timer) clearTimeout(hold.timer)
|
|
496
519
|
this.fencedHolds.set(name, {currentEpoch: Number(response.currentEpoch ?? 0), epoch})
|
|
497
520
|
}
|
|
498
521
|
} catch (error) {
|
|
499
522
|
if (!(error instanceof AdvisoryLockServiceUnavailableError)) throw error
|
|
500
523
|
|
|
501
|
-
// A
|
|
502
|
-
//
|
|
503
|
-
//
|
|
524
|
+
// A request timeout is not itself proof that the lease was fenced. Keep
|
|
525
|
+
// the hold until its server-granted expiry and retry the renewal while
|
|
526
|
+
// that lease remains valid. If the server did fence it, the next request
|
|
527
|
+
// returns `lost` (or the server's lost push records it); if the lease
|
|
528
|
+
// really expires, the next retry records the loss after that event.
|
|
529
|
+
if (Date.now() < hold.leaseExpiresAt && this.holds.get(name) === hold) {
|
|
530
|
+
this.scheduleRenewal(name, epoch, Math.max(250, Math.min(Math.floor(this.leaseMs / 3), hold.leaseExpiresAt - Date.now() - 100)))
|
|
531
|
+
|
|
532
|
+
return
|
|
533
|
+
}
|
|
534
|
+
|
|
504
535
|
this.markUnavailable(error)
|
|
505
536
|
this.holds.delete(name)
|
|
506
|
-
|
|
537
|
+
if (hold.timer) clearTimeout(hold.timer)
|
|
507
538
|
this.fencedHolds.set(name, {currentEpoch: null, epoch})
|
|
508
539
|
this.reportError(error)
|
|
540
|
+
} finally {
|
|
541
|
+
hold.renewing = false
|
|
509
542
|
}
|
|
510
543
|
}
|
|
511
544
|
|
|
@@ -526,7 +559,7 @@ export default class AdvisoryLockLockServerBackend {
|
|
|
526
559
|
*/
|
|
527
560
|
teardown() {
|
|
528
561
|
for (const [name, hold] of this.holds) {
|
|
529
|
-
|
|
562
|
+
if (hold.timer) clearTimeout(hold.timer)
|
|
530
563
|
this.fencedHolds.set(name, {currentEpoch: null, epoch: hold.epoch})
|
|
531
564
|
}
|
|
532
565
|
|