mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 21:27:20 +00:00
fix(server): distinguish local mutex correctly (#9444)
This commit is contained in:
@@ -11,11 +11,12 @@ import { Lock } from './lock';
|
||||
const lockScript = `local key = KEYS[1]
|
||||
local owner = ARGV[1]
|
||||
|
||||
-- if lock is not exists or lock is owned by the owner
|
||||
-- then set lock to the owner and return 1, otherwise return 0
|
||||
-- if lock is not exists then set lock to the owner and return 1, otherwise return 0
|
||||
-- if the lock is not released correctly due to unexpected reasons
|
||||
-- lock will be released after 60 seconds
|
||||
if redis.call("get", key) == owner or redis.call("set", key, owner, "NX", "EX", 60) then
|
||||
if redis.call("get", key) == owner then
|
||||
return 0
|
||||
elseif redis.call("set", key, owner, "NX", "EX", 60) then
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
|
||||
@@ -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';
|
||||
@@ -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) {}
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user