Skip to content

Commit 5dd9e28

Browse files
arath17Ashutosh Rath
andauthored
Fix Auto extending locks (#274)
* Fix extension failure * format --------- Co-authored-by: Ashutosh Rath <arath9@its.jnj.com>
1 parent dc7bcd9 commit 5dd9e28

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

src/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export type ExecutionStats = {
8888
*/
8989
export type ExecutionResult = {
9090
attempts: ReadonlyArray<Promise<ExecutionStats>>;
91+
start: number;
9192
};
9293

9394
/**
@@ -302,11 +303,10 @@ export default class Redlock extends EventEmitter {
302303
throw new Error("Duration must be an integer value in milliseconds.");
303304
}
304305

305-
const start = Date.now();
306306
const value = this._random();
307307

308308
try {
309-
const { attempts } = await this._execute(
309+
const { attempts, start } = await this._execute(
310310
this.scripts.acquireScript,
311311
resources,
312312
[value, duration],
@@ -436,13 +436,17 @@ export default class Redlock extends EventEmitter {
436436
const attempts: Promise<ExecutionStats>[] = [];
437437

438438
while (true) {
439-
const { vote, stats } = await this._attemptOperation(script, keys, args);
439+
const { vote, stats, start } = await this._attemptOperation(
440+
script,
441+
keys,
442+
args
443+
);
440444

441445
attempts.push(stats);
442446

443447
// The operation achieved a quorum in favor.
444448
if (vote === "for") {
445-
return { attempts };
449+
return { attempts, start };
446450
}
447451

448452
// Wait before reattempting.
@@ -472,9 +476,11 @@ export default class Redlock extends EventEmitter {
472476
keys: string[],
473477
args: (string | number)[]
474478
): Promise<
475-
| { vote: "for"; stats: Promise<ExecutionStats> }
476-
| { vote: "against"; stats: Promise<ExecutionStats> }
479+
| { vote: "for"; stats: Promise<ExecutionStats>; start: number }
480+
| { vote: "against"; stats: Promise<ExecutionStats>; start: number }
477481
> {
482+
const start = Date.now();
483+
478484
return await new Promise((resolve) => {
479485
const clientResults = [];
480486
for (const client of this.clients) {
@@ -511,6 +517,7 @@ export default class Redlock extends EventEmitter {
511517
resolve({
512518
vote: "for",
513519
stats: statsPromise,
520+
start,
514521
});
515522
}
516523

@@ -519,6 +526,7 @@ export default class Redlock extends EventEmitter {
519526
resolve({
520527
vote: "against",
521528
stats: statsPromise,
529+
start,
522530
});
523531
}
524532

0 commit comments

Comments
 (0)