Log inside locks
This commit is contained in:
parent
ab5336f567
commit
bcf48c428d
2 changed files with 11 additions and 2 deletions
|
|
@ -1,12 +1,14 @@
|
||||||
|
import { Logger } from "../tracing/logger";
|
||||||
import type { RelativePath } from "../persistence/database";
|
import type { RelativePath } from "../persistence/database";
|
||||||
import { DocumentLocks } from "./document-locks";
|
import { DocumentLocks } from "./document-locks";
|
||||||
|
|
||||||
describe("Document lock", () => {
|
describe("Document lock", () => {
|
||||||
const testPath: RelativePath = "test/document/path";
|
const testPath: RelativePath = "test/document/path";
|
||||||
let locks = new DocumentLocks();
|
const logger = new Logger();
|
||||||
|
let locks = new DocumentLocks(logger);
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
locks = new DocumentLocks();
|
locks = new DocumentLocks(logger);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("should lock a document successfully", () => {
|
test("should lock a document successfully", () => {
|
||||||
|
|
@ -1,15 +1,19 @@
|
||||||
|
import type { Logger } from "../tracing/logger";
|
||||||
import type { RelativePath } from "../persistence/database";
|
import type { RelativePath } from "../persistence/database";
|
||||||
|
|
||||||
export class DocumentLocks {
|
export class DocumentLocks {
|
||||||
private readonly locked = new Set<RelativePath>();
|
private readonly locked = new Set<RelativePath>();
|
||||||
private readonly waiters = new Map<RelativePath, (() => void)[]>();
|
private readonly waiters = new Map<RelativePath, (() => void)[]>();
|
||||||
|
|
||||||
|
public constructor(private readonly logger: Logger) {}
|
||||||
|
|
||||||
public tryLockDocument(relativePath: RelativePath): boolean {
|
public tryLockDocument(relativePath: RelativePath): boolean {
|
||||||
if (this.locked.has(relativePath)) {
|
if (this.locked.has(relativePath)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.locked.add(relativePath);
|
this.locked.add(relativePath);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -20,6 +24,8 @@ export class DocumentLocks {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.debug(`Waiting for lock on ${relativePath}`);
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
let waiting = this.waiters.get(relativePath);
|
let waiting = this.waiters.get(relativePath);
|
||||||
if (!waiting) {
|
if (!waiting) {
|
||||||
|
|
@ -42,6 +48,7 @@ export class DocumentLocks {
|
||||||
const nextWaiting = this.waiters.get(relativePath)?.shift();
|
const nextWaiting = this.waiters.get(relativePath)?.shift();
|
||||||
|
|
||||||
if (nextWaiting) {
|
if (nextWaiting) {
|
||||||
|
this.logger.debug(`Granted lock on ${relativePath}`);
|
||||||
nextWaiting();
|
nextWaiting();
|
||||||
} else {
|
} else {
|
||||||
this.locked.delete(relativePath);
|
this.locked.delete(relativePath);
|
||||||
Loading…
Add table
Add a link
Reference in a new issue