mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 05:07:27 +00:00
refactor(plugin-sdk): dedupe file lock release
This commit is contained in:
@@ -107,6 +107,20 @@ export type FileLockHandle = {
|
|||||||
release: () => Promise<void>;
|
release: () => Promise<void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function releaseHeldLock(normalizedFile: string): Promise<void> {
|
||||||
|
const current = HELD_LOCKS.get(normalizedFile);
|
||||||
|
if (!current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
current.count -= 1;
|
||||||
|
if (current.count > 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
HELD_LOCKS.delete(normalizedFile);
|
||||||
|
await current.handle.close().catch(() => undefined);
|
||||||
|
await fs.rm(current.lockPath, { force: true }).catch(() => undefined);
|
||||||
|
}
|
||||||
|
|
||||||
export async function acquireFileLock(
|
export async function acquireFileLock(
|
||||||
filePath: string,
|
filePath: string,
|
||||||
options: FileLockOptions,
|
options: FileLockOptions,
|
||||||
@@ -118,19 +132,7 @@ export async function acquireFileLock(
|
|||||||
held.count += 1;
|
held.count += 1;
|
||||||
return {
|
return {
|
||||||
lockPath,
|
lockPath,
|
||||||
release: async () => {
|
release: () => releaseHeldLock(normalizedFile),
|
||||||
const current = HELD_LOCKS.get(normalizedFile);
|
|
||||||
if (!current) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
current.count -= 1;
|
|
||||||
if (current.count > 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
HELD_LOCKS.delete(normalizedFile);
|
|
||||||
await current.handle.close().catch(() => undefined);
|
|
||||||
await fs.rm(current.lockPath, { force: true }).catch(() => undefined);
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,19 +147,7 @@ export async function acquireFileLock(
|
|||||||
HELD_LOCKS.set(normalizedFile, { count: 1, handle, lockPath });
|
HELD_LOCKS.set(normalizedFile, { count: 1, handle, lockPath });
|
||||||
return {
|
return {
|
||||||
lockPath,
|
lockPath,
|
||||||
release: async () => {
|
release: () => releaseHeldLock(normalizedFile),
|
||||||
const current = HELD_LOCKS.get(normalizedFile);
|
|
||||||
if (!current) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
current.count -= 1;
|
|
||||||
if (current.count > 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
HELD_LOCKS.delete(normalizedFile);
|
|
||||||
await current.handle.close().catch(() => undefined);
|
|
||||||
await fs.rm(current.lockPath, { force: true }).catch(() => undefined);
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const code = (err as { code?: string }).code;
|
const code = (err as { code?: string }).code;
|
||||||
|
|||||||
Reference in New Issue
Block a user