Skip to content

Commit 6daa707

Browse files
move time amounts into constants
1 parent 405eb8e commit 6daa707

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/libraries/FileLock.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import fsPromises from 'fs/promises';
22
import { InternalServerOptions } from "../../types";
33

4+
const mtimeUpdateIntervalTime = 2_000
5+
const mtimeLimit = 10_000
6+
47
export async function waitForLock(path: string, options: InternalServerOptions): Promise<void> {
58
const lockPath = `${path}.lock`
69
let retries = 0;
7-
10+
811
do {
912
retries++;
1013
try {
1114
const stat = await fsPromises.stat(lockPath)
12-
if (performance.now() - stat.mtime.getTime() > 10_000) {
15+
if (performance.now() - stat.mtime.getTime() > mtimeLimit) {
1316
return
1417
} else {
1518
await new Promise(resolve => setTimeout(resolve, options.lockRetryWait))
@@ -32,7 +35,7 @@ function setupMTimeEditor(lockPath: string): () => Promise<void> {
3235
const time = performance.now();
3336
await fsPromises.utimes(lockPath, time, time)
3437
} catch {}
35-
}, 2_000)
38+
}, mtimeUpdateIntervalTime)
3639

3740
return async () => {
3841
clearInterval(interval)
@@ -48,7 +51,7 @@ export async function lockFile(path: string): Promise<() => Promise<void>> {
4851
} catch (e) {
4952
if (e.code === 'EEXIST') {
5053
const stat = await fsPromises.stat(lockPath)
51-
if (performance.now() - stat.mtime.getTime() > 10_000) {
54+
if (performance.now() - stat.mtime.getTime() > mtimeLimit) {
5255
return setupMTimeEditor(lockPath)
5356
} else {
5457
throw 'LOCKED'

0 commit comments

Comments
 (0)