From b977877e935b6c67cea6b308d4257ebbc3cc0cf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20J=C3=A4derberg?= Date: Wed, 7 May 2025 09:15:58 +0000 Subject: [PATCH] Merged in fix/limit-time-to-response-for-cache-calls (pull request #1990) fix: add timeout for redis-api calls SW-2635 Approved-by: Michael Zetterberg --- .../dataCache/DistributedCache/deleteKey.ts | 26 +++++++++++-------- .../dataCache/DistributedCache/get.ts | 7 +++-- .../dataCache/DistributedCache/set.ts | 7 +++-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/apps/scandic-web/services/dataCache/DistributedCache/deleteKey.ts b/apps/scandic-web/services/dataCache/DistributedCache/deleteKey.ts index 25eaa8a18..d9071fda8 100644 --- a/apps/scandic-web/services/dataCache/DistributedCache/deleteKey.ts +++ b/apps/scandic-web/services/dataCache/DistributedCache/deleteKey.ts @@ -1,5 +1,7 @@ import * as Sentry from "@sentry/nextjs" +import { safeTry } from "@/utils/safeTry" + import { cacheLogger } from "../logger" import { API_KEY } from "./client" import { getCacheEndpoint } from "./endpoints" @@ -12,18 +14,20 @@ export async function deleteKey(key: string, opts?: { fuzzy?: boolean }) { endpoint.searchParams.set("fuzzy", "true") } - const response = await fetch(endpoint, { - method: "DELETE", - cache: "no-cache", - headers: { - "x-api-key": API_KEY, - }, - }) + const [response, error] = await safeTry( + fetch(endpoint, { + method: "DELETE", + cache: "no-cache", + headers: { + "x-api-key": API_KEY, + }, + signal: AbortSignal.timeout(10_000), + }) + ) - if (!response.ok) { - if (response.status !== 404) { - Sentry.captureMessage("Unable to DELETE cachekey", { - level: "error", + if (!response || !response.ok || error) { + if (response?.status !== 404) { + Sentry.captureException(error ?? new Error("Unable to DELETE cachekey"), { extra: { cacheKey: key, statusCode: response?.status, diff --git a/apps/scandic-web/services/dataCache/DistributedCache/get.ts b/apps/scandic-web/services/dataCache/DistributedCache/get.ts index c7802e80e..e8c262089 100644 --- a/apps/scandic-web/services/dataCache/DistributedCache/get.ts +++ b/apps/scandic-web/services/dataCache/DistributedCache/get.ts @@ -17,10 +17,11 @@ export async function get(key: string) { headers: { "x-api-key": API_KEY, }, + signal: AbortSignal.timeout(3_000), }) ) - if (!response || error || !response.ok) { + if (!response || !response.ok || error) { if (response?.status === 404) { cacheLogger.debug( `Miss '${key}' took ${(performance.now() - perf).toFixed(2)}ms` @@ -28,11 +29,9 @@ export async function get(key: string) { return undefined } - Sentry.captureMessage("Unable to GET cachekey", { - level: "error", + Sentry.captureException(error ?? new Error("Unable to GET cachekey"), { extra: { cacheKey: key, - errorMessage: error instanceof Error ? error.message : undefined, statusCode: response?.status, statusText: response?.statusText, }, diff --git a/apps/scandic-web/services/dataCache/DistributedCache/set.ts b/apps/scandic-web/services/dataCache/DistributedCache/set.ts index 8c923202f..de89ed1d6 100644 --- a/apps/scandic-web/services/dataCache/DistributedCache/set.ts +++ b/apps/scandic-web/services/dataCache/DistributedCache/set.ts @@ -16,15 +16,14 @@ export async function set(key: string, value: T, ttl: CacheTime) { }, body: JSON.stringify({ data: value, ttl: getCacheTimeInSeconds(ttl) }), cache: "no-cache", + signal: AbortSignal.timeout(3_000), }) ) - if (!response || error || !response.ok) { - Sentry.captureMessage("Unable to SET cachekey", { - level: "error", + if (!response || !response.ok || error) { + Sentry.captureException(error ?? new Error("Unable to SET cachekey"), { extra: { cacheKey: key, - errorMessage: error instanceof Error ? error.message : undefined, statusCode: response?.status, statusText: response?.statusText, },