Skip to content

Commit

Permalink
refact: monitor mutex lock time using statsd (#9874)
Browse files Browse the repository at this point in the history
  • Loading branch information
kewitz authored Feb 27, 2024
1 parent 680f7d0 commit 73d6f07
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/lib/mutex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import debugLib from 'debug';

import logger from './logger';
import { createRedisClient, RedisInstanceType } from './redis';
import { utils } from './statsd';
import { sleep } from './utils';

const debug = debugLib('mutex');
Expand All @@ -22,6 +23,7 @@ export async function lockUntilResolved<T>(

const start = Date.now();
const _key = `lock:${key}`;
const stopWatch = utils.stopwatch(`mutex.lockUntilResolved.${_key}`);
const lock = () => redis.set(_key, 1, { NX: true, PX: unlockTimeoutMs });

let lockAcquired = await lock();
Expand All @@ -38,6 +40,7 @@ export async function lockUntilResolved<T>(
debug(`Acquired lock ${_key}`);
return until().finally(async () => {
await redis.del(_key);
stopWatch();
debug(`Released lock ${_key}`);
});
}

0 comments on commit 73d6f07

Please sign in to comment.