From bcf48c428d580e6573719433b0eef22323d95113 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sat, 1 Mar 2025 17:58:55 +0000 Subject: [PATCH] Log inside locks --- .../document-locks.test.ts | 6 ++++-- .../{sync-operations => file-operations}/document-locks.ts | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) rename frontend/sync-client/src/{sync-operations => file-operations}/document-locks.test.ts (93%) rename frontend/sync-client/src/{sync-operations => file-operations}/document-locks.ts (84%) diff --git a/frontend/sync-client/src/sync-operations/document-locks.test.ts b/frontend/sync-client/src/file-operations/document-locks.test.ts similarity index 93% rename from frontend/sync-client/src/sync-operations/document-locks.test.ts rename to frontend/sync-client/src/file-operations/document-locks.test.ts index ce661d02..1b8394ba 100644 --- a/frontend/sync-client/src/sync-operations/document-locks.test.ts +++ b/frontend/sync-client/src/file-operations/document-locks.test.ts @@ -1,12 +1,14 @@ +import { Logger } from "../tracing/logger"; import type { RelativePath } from "../persistence/database"; import { DocumentLocks } from "./document-locks"; describe("Document lock", () => { const testPath: RelativePath = "test/document/path"; - let locks = new DocumentLocks(); + const logger = new Logger(); + let locks = new DocumentLocks(logger); beforeEach(() => { - locks = new DocumentLocks(); + locks = new DocumentLocks(logger); }); test("should lock a document successfully", () => { diff --git a/frontend/sync-client/src/sync-operations/document-locks.ts b/frontend/sync-client/src/file-operations/document-locks.ts similarity index 84% rename from frontend/sync-client/src/sync-operations/document-locks.ts rename to frontend/sync-client/src/file-operations/document-locks.ts index e8e0eb13..3dc7ec29 100644 --- a/frontend/sync-client/src/sync-operations/document-locks.ts +++ b/frontend/sync-client/src/file-operations/document-locks.ts @@ -1,15 +1,19 @@ +import type { Logger } from "../tracing/logger"; import type { RelativePath } from "../persistence/database"; export class DocumentLocks { private readonly locked = new Set(); private readonly waiters = new Map void)[]>(); + public constructor(private readonly logger: Logger) {} + public tryLockDocument(relativePath: RelativePath): boolean { if (this.locked.has(relativePath)) { return false; } this.locked.add(relativePath); + return true; } @@ -20,6 +24,8 @@ export class DocumentLocks { return Promise.resolve(); } + this.logger.debug(`Waiting for lock on ${relativePath}`); + return new Promise((resolve) => { let waiting = this.waiters.get(relativePath); if (!waiting) { @@ -42,6 +48,7 @@ export class DocumentLocks { const nextWaiting = this.waiters.get(relativePath)?.shift(); if (nextWaiting) { + this.logger.debug(`Granted lock on ${relativePath}`); nextWaiting(); } else { this.locked.delete(relativePath);