Skip to content

Commit

Permalink
move time amounts into constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian-Webster committed Nov 14, 2024
1 parent 405eb8e commit 6daa707
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/libraries/FileLock.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import fsPromises from 'fs/promises';
import { InternalServerOptions } from "../../types";

const mtimeUpdateIntervalTime = 2_000
const mtimeLimit = 10_000

export async function waitForLock(path: string, options: InternalServerOptions): Promise<void> {
const lockPath = `${path}.lock`
let retries = 0;

do {
retries++;
try {
const stat = await fsPromises.stat(lockPath)
if (performance.now() - stat.mtime.getTime() > 10_000) {
if (performance.now() - stat.mtime.getTime() > mtimeLimit) {
return
} else {
await new Promise(resolve => setTimeout(resolve, options.lockRetryWait))
Expand All @@ -32,7 +35,7 @@ function setupMTimeEditor(lockPath: string): () => Promise<void> {
const time = performance.now();
await fsPromises.utimes(lockPath, time, time)
} catch {}
}, 2_000)
}, mtimeUpdateIntervalTime)

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

0 comments on commit 6daa707

Please sign in to comment.