mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-15 14:49:29 +00:00
refactor(plugin-sdk): dedupe file lock release
This commit is contained in:
@@ -107,6 +107,20 @@ export type FileLockHandle = {
|
||||
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(
|
||||
filePath: string,
|
||||
options: FileLockOptions,
|
||||
@@ -118,19 +132,7 @@ export async function acquireFileLock(
|
||||
held.count += 1;
|
||||
return {
|
||||
lockPath,
|
||||
release: async () => {
|
||||
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);
|
||||
},
|
||||
release: () => releaseHeldLock(normalizedFile),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -145,19 +147,7 @@ export async function acquireFileLock(
|
||||
HELD_LOCKS.set(normalizedFile, { count: 1, handle, lockPath });
|
||||
return {
|
||||
lockPath,
|
||||
release: async () => {
|
||||
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);
|
||||
},
|
||||
release: () => releaseHeldLock(normalizedFile),
|
||||
};
|
||||
} catch (err) {
|
||||
const code = (err as { code?: string }).code;
|
||||
|
||||
Reference in New Issue
Block a user