felanios / murlock

MurLock: A distributed locking solution for NestJS, providing a decorator for critical sections with Redis-based synchronization. Ideal for microservices and scalable applications.
https://www.npmjs.com/package/murlock
MIT License
62 stars 1 forks source link

Error during using the @MurLock decorator inside a service. #18

Closed OsoianMarcel closed 7 months ago

OsoianMarcel commented 9 months ago

murlock version: 2.0.0 package manager: yarn v1.22.19 environment: docker image node:20-alpine node version: v20.9.0 NestJS core & common version: ^10.0.0

Usage inside a service (not controller):

@MurLock(15e3, 'requestDto.itinerary_id')
  public async check(
    // requestDto contains a string property "itinerary_id"
    requestDto: RequestDto, 
  ): Promise<ResponseDto> {
  // ...
}

Error during calling the service method (with MurLock decorator):

[15:24:20.690] ERROR (63): Failed to acquire lock for key CheckService:check:5f92cbf10cf217478ba93561: Cannot set the key "clientId". No CLS context available, please make sure that a ClsMiddleware/Guard/Interceptor has set up the context, or wrap any calls that depend on CLS with "ClsService#run"
    req: {},
    context: "HttpExceptionFilter"
    err: {
      "type": "MurLockException",
      "message": "Failed to acquire lock for key CheckService:check:5f92cbf10cf217478ba93561: Cannot set the key \"clientId\". No CLS context available, please make sure that a ClsMiddleware/Guard/Interceptor has set up the context, or wrap any calls that depend on CLS with \"ClsService#run\"",
      "stack":
          MurLockException: Failed to acquire lock for key CheckService:check:5f92cbf10cf217478ba93561: Cannot set the key "clientId". No CLS context available, please make sure that a ClsMiddleware/Guard/Interceptor has set up the context, or wrap any calls that depend on CLS with "ClsService#run"
              at /home/node/app/node_modules/murlock/lib/decorators/murlock.decorator.ts:82:11
              at Generator.throw (<anonymous>)
              at rejected (/home/node/app/node_modules/murlock/dist/decorators/murlock.decorator.js:6:65)
              at processTicksAndRejections (node:internal/process/task_queues:95:5)
      "name": "MurLockException"
    }
felanios commented 9 months ago

@OsoianMarcel Thanks for your support, I found the issue, bugfix is ​​on the way. Since I use Nestjs-cls for tracing in my services, there are a few things I missed.

felanios commented 9 months ago

@OsoianMarcel https://www.npmjs.com/package/murlock/v/2.0.1 Fixed with this version, fyi.

OsoianMarcel commented 7 months ago

@felanios Can we remove the dependency "ClsModule"? I'm receiving the same problem again on another project. It seems like the "ClsModule" is only used for keeping the client ID. Can we keep the ID inside the service itself on a private property or another place inside the "murlock" module? This way, we'll remove an unnecessary dependency, and forget about this bug forever. Thank you.

felanios commented 7 months ago

Hi @OsoianMarcel, I'm totally agree with that, I received feedback from different users regarding the same problem. I will solve this problem within this week.

OsoianMarcel commented 7 months ago

I created a PR https://github.com/felanios/murlock/pull/31, hope it helps to resolve the problem. Thx.

felanios commented 7 months ago

@OsoianMarcel I merged the pr, thank you for your contribution.Because of the semantic-release, package bumped to v3.0.1, fyi. https://www.npmjs.com/package/murlock/v/3.0.1,

OsoianMarcel commented 7 months ago

@felanios thanks, it works fine.