This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Student { | |
id: number; | |
name: string; | |
dateOfBirth: Date; | |
} | |
class StudentRepository { | |
someDbClient: any; | |
constructor() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Inject } from '@nestjs/common'; | |
import { DEFAULT_LOCK_SECONDS } from '../common/constants'; | |
import { UseKeyGenerator } from '../common/types'; | |
import { LockAcquireException } from '../exceptions/lock-acquire.exception'; | |
import { LockService } from '../lock.service'; | |
export function ConcurrencyLock(options: { | |
useKey: string | UseKeyGenerator; | |
releaseAfterSeconds?: number; | |
errorMessage?: string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class SomeClass { | |
ConcurrencyLock({ | |
useKey: (input: CreateHelpSessionInput) => `create_help_session_${input.userId}` | |
releaseAfterSeconds: 30, | |
errorMessage?: `Failed to acquire lock for the create help session. Try after sometime.` | |
}) | |
public createHelpSession(input: CreateHelpSessionInput) { | |
// Existing code.. No changes here. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class SomeClass { | |
public createHelpSession(input: CreateHelpSessionInput) { | |
const key = `create_help_session_${input.userId}` | |
await this.lockService.acquireLock(key); | |
try { | |
// entire existing code goes here | |
} finally() { | |
await this.releaseLock(key); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Inject, Injectable, Logger } from '@nestjs/common'; | |
import { CustomLogger } from 'src/common/decorators/custom-logger.decorator'; | |
import { Redis } from '../redis/redis'; | |
import { LockAcquireException } from './exceptions/lock-acquire.exception'; | |
@Injectable() | |
export class LockService { | |
@CustomLogger() | |
private readonly logger: Logger; |