Skip to content

Commit

Permalink
fix: distinguish local mutex correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Dec 31, 2024
1 parent 79e0064 commit a54f092
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/backend/server/src/base/mutex/mutex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { randomUUID } from 'node:crypto';
import { Inject, Injectable, Logger, Scope } from '@nestjs/common';
import { ModuleRef, REQUEST } from '@nestjs/core';
import type { Request } from 'express';
import { nanoid } from 'nanoid';

import { GraphqlContext } from '../graphql';
import { retryable } from '../utils/promise';
Expand All @@ -14,7 +15,7 @@ export const MUTEX_WAIT = 100;
@Injectable()
export class Mutex {
protected logger = new Logger(Mutex.name);
private readonly clusterIdentifier = `cluster:${randomUUID()}`;
private readonly clusterIdentifier = `cluster:${nanoid()}`;

constructor(protected readonly locker: Locker) {}

Expand All @@ -39,7 +40,10 @@ export class Mutex {
* @param key resource key
* @returns LockGuard
*/
async acquire(key: string, owner: string = this.clusterIdentifier) {
async acquire(
key: string,
owner: string = `${this.clusterIdentifier}:${nanoid()}`
) {
try {
return await retryable(
() => this.locker.lock(owner, key),
Expand Down
65 changes: 65 additions & 0 deletions packages/backend/server/tests/mutex.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { TestingModule } from '@nestjs/testing';
import ava, { TestFn } from 'ava';
import Sinon from 'sinon';

import { Locker, Mutex } from '../src/base/mutex';
import { SessionRedis } from '../src/base/redis';
import { createTestingModule, sleep } from './utils';

const test = ava as TestFn<{
module: TestingModule;
mutex: Mutex;
locker: Locker;
session: SessionRedis;
}>;

test.beforeEach(async t => {
const module = await createTestingModule();

t.context.module = module;
t.context.mutex = module.get(Mutex);
t.context.locker = module.get(Locker);
t.context.session = module.get(SessionRedis);
});

test.afterEach(async t => {
await t.context.module.close();
});

test('should be able to acquire lock', async t => {
const { mutex } = t.context;

{
t.truthy(await mutex.acquire('test'), 'should be able to acquire lock');
t.falsy(
await mutex.acquire('test'),
'should not be able to acquire lock again'
);
}

{
const lock1 = await mutex.acquire('test');
t.truthy(lock1);
await lock1?.release();
const lock2 = await mutex.acquire('test');
t.truthy(lock2);
}
});

test('should be able to acquire lock parallel', async t => {
const { mutex, locker } = t.context;
const spyedLocker = Sinon.spy(locker, 'lock');
const requestLock = async (key: string) => {
const lock = mutex.acquire(key);
await using _lock = await lock;
await t.throwsAsync(spyedLocker.lastCall.returnValue, {
message: `Failed to acquire lock for resource [${key}]`,
});
await sleep(100);
};

await t.notThrowsAsync(
Promise.all(Array.from({ length: 10 }, _ => requestLock('test'))),
'should be able to acquire lock parallel'
);
});

0 comments on commit a54f092

Please sign in to comment.