-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from Sebastian-Webster/own-filelock-implement…
…ation Use homemade lock file system
- Loading branch information
Showing
6 changed files
with
77 additions
and
67 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,73 @@ | ||
import { checkSync } from "proper-lockfile"; | ||
import fsPromises from 'fs/promises'; | ||
import { InternalServerOptions } from "../../types"; | ||
|
||
export function waitForLock(path: string, options: InternalServerOptions): Promise<void> { | ||
return new Promise(async (resolve, reject) => { | ||
let retries = 0; | ||
while (retries <= options.lockRetries) { | ||
retries++ | ||
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() > mtimeLimit) { | ||
return | ||
} else { | ||
await new Promise(resolve => setTimeout(resolve, options.lockRetryWait)) | ||
} | ||
} catch (e) { | ||
if (e.code === 'ENOENT') { | ||
return | ||
} else { | ||
throw e | ||
} | ||
} | ||
} while(retries <= options.lockRetries) | ||
|
||
throw `lockRetries has been exceeded. Lock had not been released after ${options.lockRetryWait} * ${options.lockRetries} (${options.lockRetryWait * options.lockRetries}) milliseconds.` | ||
} | ||
|
||
function setupMTimeEditor(lockPath: string): () => Promise<void> { | ||
const interval = setInterval(async () => { | ||
try { | ||
const time = new Date(); | ||
await fsPromises.utimes(lockPath, time, time) | ||
} catch {} | ||
}, mtimeUpdateIntervalTime) | ||
|
||
return async () => { | ||
clearInterval(interval) | ||
await fsPromises.rmdir(lockPath) | ||
} | ||
} | ||
|
||
export async function lockFile(path: string): Promise<() => Promise<void>> { | ||
const lockPath = `${path}.lock` | ||
try { | ||
await fsPromises.mkdir(lockPath) | ||
return setupMTimeEditor(lockPath) | ||
} catch (e) { | ||
if (e.code === 'EEXIST') { | ||
try { | ||
const locked = checkSync(path, {realpath: false}); | ||
if (!locked) { | ||
return resolve() | ||
const stat = await fsPromises.stat(lockPath) | ||
if (performance.now() - stat.mtime.getTime() > mtimeLimit) { | ||
return setupMTimeEditor(lockPath) | ||
} else { | ||
await new Promise(resolve => setTimeout(resolve, options.lockRetryWait)) | ||
throw 'LOCKED' | ||
} | ||
} catch (e) { | ||
return reject(e) | ||
if (e.code === 'ENOENT') { | ||
//This will run if the lock gets released after the EEXIST error is thrown but before the stat is checked. | ||
//If this is the case, the lock acquisition should be retried. | ||
return await lockFile(path) | ||
} else { | ||
throw e | ||
} | ||
} | ||
} else { | ||
throw e | ||
} | ||
reject(`lockRetries has been exceeded. Lock had not been released after ${options.lockRetryWait} * ${options.lockRetries} milliseconds.`) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters