Skip to content

Commit

Permalink
refactor: Improve locking mechanism in Semaphore class
Browse files Browse the repository at this point in the history
- Modify the Semaphore class in Semaphore.ts to include a namespace parameter in the lock method.
- Update the lockTimeout default value from 1000 to 5000 milliseconds.
- Append the namespace to the key when creating a new Mutex instance.
- Add a try-catch block to handle errors when acquiring and releasing the mutex.
  • Loading branch information
simlarsen committed Oct 9, 2024
1 parent 8590cfa commit 8270934
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Common/Server/Infrastructure/Semaphore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ export default class Semaphore {
// returns the mutex id
public static async lock(data: {
key: string;
namespace: string;
lockTimeout?: number;
}): Promise<SemaphoreMutex> {
if (!data.lockTimeout) {
data.lockTimeout = 1000;
data.lockTimeout = 5000;
}

const { key } = data;
Expand All @@ -21,9 +22,13 @@ export default class Semaphore {
throw new Error("Redis client is not connected");
}

const mutex: SemaphoreMutex = new Mutex(client, key, {
lockTimeout: data.lockTimeout,
});
const mutex: SemaphoreMutex = new Mutex(
client,
data.namespace + "-" + key,
{
lockTimeout: data.lockTimeout,
},
);

await mutex.acquire();

Expand Down
20 changes: 20 additions & 0 deletions Common/Server/Utils/Monitor/MonitorResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,23 @@ import { TelemetryQuery } from "../../../Types/Telemetry/TelemetryQuery";
import MonitorIncident from "./MonitorIncident";
import MonitorAlert from "./MonitorAlert";
import MonitorStatusTimelineUtil from "./MonitorStatusTimeline";
import Semaphore, { SemaphoreMutex } from "../../Infrastructure/Semaphore";

export default class MonitorResourceUtil {
public static async monitorResource(
dataToProcess: DataToProcess,
): Promise<ProbeApiIngestResponse> {
let mutex: SemaphoreMutex | null = null;

try {
mutex = await Semaphore.lock({
key: dataToProcess.monitorId.toString(),
namespace: "MonitorResourceUtil.monitorResource",
});
} catch (err) {
logger.error(err);
}

let response: ProbeApiIngestResponse = {
monitorId: dataToProcess.monitorId,
criteriaMetId: undefined,
Expand Down Expand Up @@ -532,6 +544,14 @@ export default class MonitorResourceUtil {
}
}

if (mutex) {
try {
await Semaphore.release(mutex);
} catch (err) {
logger.error(err);
}
}

return response;
}

Expand Down
1 change: 1 addition & 0 deletions Ingestor/API/Monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ router.post(
try {
mutex = await Semaphore.lock({
key: probeId.toString(),
namespace: "MonitorAPI.monitor-list",
});
} catch (err) {
logger.error(err);
Expand Down
1 change: 1 addition & 0 deletions Ingestor/API/Probe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ router.post(
): Promise<void> => {
try {
const data: JSONObject = req.body;

const statusReport: ProbeStatusReport = JSONFunctions.deserialize(
(data as JSONObject)["statusReport"] as any,
) as any;
Expand Down

0 comments on commit 8270934

Please sign in to comment.