Skip to content

Commit

Permalink
fix: remove sync lock and add simultaneous async locks
Browse files Browse the repository at this point in the history
  • Loading branch information
NeuralFlux committed Nov 5, 2024
1 parent 5ee52e6 commit 55c6640
Showing 1 changed file with 9 additions and 33 deletions.
42 changes: 9 additions & 33 deletions src/misc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import lockfile from "proper-lockfile";
import { setTimeout as sleep } from "timers/promises";

export function toArray<Type>(input: Type | Type[]): Type[] {
if (Array.isArray(input)) {
Expand Down Expand Up @@ -91,50 +90,27 @@ export const LOCKFILE_RETRY_CONFIG = {
stale: LOCKFILE_STALENESS["stale"],
};

export async function lockWithActionAsync<T>(filePath: string, action: () => Promise<T>, debug?: (message: string) => void): Promise<T> {
export async function lockWithActionAsync<T>(filePaths: string[], action: () => Promise<T>, debug?: (message: string) => void, lockfileRetryConfig?: any): Promise<T> {
if (process.env.NODE_ENV !== "production") {
debug(`Development mode: Skipping lockfile ${process.env.NODE_ENV}`);
const result = await action();
return result;

Check warning on line 97 in src/misc.ts

View check run for this annotation

Codecov / codecov/patch

src/misc.ts#L95-L97

Added lines #L95 - L97 were not covered by tests
}

let release;
const releases: (() => void)[] = [];

Check warning on line 100 in src/misc.ts

View check run for this annotation

Codecov / codecov/patch

src/misc.ts#L100

Added line #L100 was not covered by tests
const retryConfig = lockfileRetryConfig || LOCKFILE_RETRY_CONFIG;
try {
release = await lockfile.lock(filePath, LOCKFILE_RETRY_CONFIG);
for (const filePath of filePaths) {
let release = await lockfile.lock(filePath, retryConfig);
releases.push(release);

Check warning on line 105 in src/misc.ts

View check run for this annotation

Codecov / codecov/patch

src/misc.ts#L102-L105

Added lines #L102 - L105 were not covered by tests
}
const result = await action();
return result;

Check warning on line 108 in src/misc.ts

View check run for this annotation

Codecov / codecov/patch

src/misc.ts#L107-L108

Added lines #L107 - L108 were not covered by tests
} catch (error) {
debug(`Lockfile error: ${error}`);

Check warning on line 110 in src/misc.ts

View check run for this annotation

Codecov / codecov/patch

src/misc.ts#L110

Added line #L110 was not covered by tests
// throw error;
} finally {
if (release) release();
}
}

export function lockWithActionSync<T>(filePath: string, action: () => T, debug?: (message: string) => void): T {
if (process.env.NODE_ENV !== "production") {
debug(`Development mode: Skipping lockfile ${process.env.NODE_ENV}`);
return action();
}

let release;
try {
const startTime = Date.now();

while (Date.now() - startTime < LOCKFILE_STALENESS["stale"]) {
if (!lockfile.checkSync(filePath)) {
release = lockfile.lockSync(filePath, LOCKFILE_STALENESS);
const result = action();
return result;
} else {
sleep(LOCKFILE_RETRY_CONFIG["retries"]["minTimeout"]);
}
}
debug("Lockfile timeout: did not read file");
} catch (error) {
debug(`Lockfile error: ${error}`);
// throw error;
} finally {
if (release) release();
for (const release of releases)

Check warning on line 113 in src/misc.ts

View check run for this annotation

Codecov / codecov/patch

src/misc.ts#L113

Added line #L113 was not covered by tests
if (release) release();
}
}

0 comments on commit 55c6640

Please sign in to comment.