| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import logger from '@/logger'
- import redis from './redis.lock'
- import RedisLock, { type Lock } from 'redlock'
- const redisLock = new RedisLock(
- // You should have one client for each independent redis node or cluster.
- [redis],
- {
- // The expected clock drift; for more details see:
- // http://redis.io/topics/distlock
- // 容忍的漂移因子
- driftFactor: 0.01, // multiplied by lock ttl to determine drift time
- // The max number of times Redlock will attempt to lock a resource before erroring.
- // 重试次数
- retryCount: 10,
- // the time in ms between attempts
- // 重试间隔
- retryDelay: 200, // time in ms
- // the max time in ms randomly added to retries to improve performance under high contention
- // see https://www.awsarchitectureblog.com/2015/03/backoff.html
- // 随机延迟
- retryJitter: 200, // time in ms
- // The minimum remaining time on a lock before an extension is automatically attempted with the `using` API.
- automaticExtensionThreshold: 500, // time in ms
- }
- )
- const lockKey = 'locks:cloud_log_list'
- const delay = 5 * 60 *1000 // ms
- let lock: Lock | undefined = undefined
- export async function listLock() {
- lock = await redisLock.acquire([...lockKey], delay);
- logger.info('cloud log list lock acquired!');
- }
- export async function extendLock() {
- lock = await lock?.extend(delay)
- logger.info('cloud log list lock extend!');
- }
- export async function listUnlock() {
- await lock?.release();
- logger.info('cloud log list lock released!');
- lock = undefined
- }
- // 检查资源是否已锁定
- export function checkLockStatus() {
- if (lock === undefined) {
- logger.info('cloud log resource is not locked.');
- return false; // 未被锁定
- } else {
- logger.info('cloud log resource is already locked.');
- return true; // 已被锁定
- }
- }
|