syncguard
Version:
Functional TypeScript library for distributed locking across microservices. Prevents race conditions with Redis, Firestore, and custom backends. Features automatic lock management, timeout handling, and extensible architecture.
40 lines (39 loc) • 1.54 kB
JavaScript
/* SPDX-FileCopyrightText: 2025-present Kriasoft */
/* SPDX-License-Identifier: MIT */
import { withRetries } from "../retry.js";
/**
* Creates an isLocked operation for Firestore backend
*/
export function createIsLockedOperation(db, locksCollection, config) {
return async (key) => {
return withRetries(async () => {
const docRef = locksCollection.doc(key);
const doc = await docRef.get();
if (!doc.exists) {
return false;
}
const data = doc.data();
const currentTime = Date.now();
if (data.expiresAt <= currentTime) {
// Use atomic transaction for cleanup to prevent race conditions
try {
await db.runTransaction(async (trx) => {
const transactionDoc = await trx.get(docRef);
if (transactionDoc.exists) {
const transactionData = transactionDoc.data();
// Double-check expiration within transaction
if (transactionData.expiresAt <= currentTime) {
trx.delete(docRef);
}
}
});
}
catch {
// Ignore cleanup errors - lock will eventually be cleaned up by other operations
}
return false;
}
return true;
}, config);
};
}