-
Notifications
You must be signed in to change notification settings - Fork 753
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d33b5ed
commit 55c7e89
Showing
9 changed files
with
185 additions
and
30 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
49 changes: 49 additions & 0 deletions
49
services/libs/integrations/src/integrations/groupsio/utils/lock.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { ICache } from '@crowd/types' | ||
|
||
export class RedisSemaphore { | ||
private readonly key: string | ||
private readonly maxConcurrent: number | ||
private readonly cache: ICache | ||
private readonly timeout: number | ||
|
||
constructor({ | ||
integrationId, | ||
apiCallType, | ||
maxConcurrent, | ||
cache, | ||
timeout = 60000, | ||
}: { | ||
integrationId: string | ||
apiCallType: string | ||
maxConcurrent: number | ||
cache: ICache | ||
timeout?: number | ||
}) { | ||
this.key = `groupsio-semaphore:${integrationId}:${apiCallType}` | ||
this.maxConcurrent = maxConcurrent | ||
this.cache = cache | ||
this.timeout = timeout | ||
} | ||
|
||
async acquire(): Promise<boolean> { | ||
const startTime = Date.now() | ||
while (Date.now() - startTime < this.timeout) { | ||
const current = await this.cache.get(this.key) | ||
const currentValue = current ? parseInt(current, 10) : 0 | ||
|
||
if (currentValue < this.maxConcurrent) { | ||
await this.cache.increment(this.key) | ||
return true | ||
} | ||
await new Promise((resolve) => setTimeout(resolve, 100)) | ||
} | ||
throw new Error(`Failed to acquire lock within timeout period for ${this.key}`) | ||
} | ||
|
||
async release(): Promise<void> { | ||
const current = await this.cache.get(this.key) | ||
if (current && parseInt(current, 10) > 0) { | ||
await this.cache.decrement(this.key) | ||
} | ||
} | ||
} |