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
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import * as Sentry from "@sentry/nextjs"
|
import * as Sentry from "@sentry/nextjs"
|
||||||
|
|
||||||
|
import { safeTry } from "@/utils/safeTry"
|
||||||
|
|
||||||
import { cacheLogger } from "../logger"
|
import { cacheLogger } from "../logger"
|
||||||
import { API_KEY } from "./client"
|
import { API_KEY } from "./client"
|
||||||
import { getCacheEndpoint } from "./endpoints"
|
import { getCacheEndpoint } from "./endpoints"
|
||||||
@@ -12,18 +14,20 @@ export async function deleteKey(key: string, opts?: { fuzzy?: boolean }) {
|
|||||||
endpoint.searchParams.set("fuzzy", "true")
|
endpoint.searchParams.set("fuzzy", "true")
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(endpoint, {
|
const [response, error] = await safeTry(
|
||||||
method: "DELETE",
|
fetch(endpoint, {
|
||||||
cache: "no-cache",
|
method: "DELETE",
|
||||||
headers: {
|
cache: "no-cache",
|
||||||
"x-api-key": API_KEY,
|
headers: {
|
||||||
},
|
"x-api-key": API_KEY,
|
||||||
})
|
},
|
||||||
|
signal: AbortSignal.timeout(10_000),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response || !response.ok || error) {
|
||||||
if (response.status !== 404) {
|
if (response?.status !== 404) {
|
||||||
Sentry.captureMessage("Unable to DELETE cachekey", {
|
Sentry.captureException(error ?? new Error("Unable to DELETE cachekey"), {
|
||||||
level: "error",
|
|
||||||
extra: {
|
extra: {
|
||||||
cacheKey: key,
|
cacheKey: key,
|
||||||
statusCode: response?.status,
|
statusCode: response?.status,
|
||||||
|
|||||||
@@ -17,10 +17,11 @@ export async function get<T>(key: string) {
|
|||||||
headers: {
|
headers: {
|
||||||
"x-api-key": API_KEY,
|
"x-api-key": API_KEY,
|
||||||
},
|
},
|
||||||
|
signal: AbortSignal.timeout(3_000),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!response || error || !response.ok) {
|
if (!response || !response.ok || error) {
|
||||||
if (response?.status === 404) {
|
if (response?.status === 404) {
|
||||||
cacheLogger.debug(
|
cacheLogger.debug(
|
||||||
`Miss '${key}' took ${(performance.now() - perf).toFixed(2)}ms`
|
`Miss '${key}' took ${(performance.now() - perf).toFixed(2)}ms`
|
||||||
@@ -28,11 +29,9 @@ export async function get<T>(key: string) {
|
|||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
Sentry.captureMessage("Unable to GET cachekey", {
|
Sentry.captureException(error ?? new Error("Unable to GET cachekey"), {
|
||||||
level: "error",
|
|
||||||
extra: {
|
extra: {
|
||||||
cacheKey: key,
|
cacheKey: key,
|
||||||
errorMessage: error instanceof Error ? error.message : undefined,
|
|
||||||
statusCode: response?.status,
|
statusCode: response?.status,
|
||||||
statusText: response?.statusText,
|
statusText: response?.statusText,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -16,15 +16,14 @@ export async function set<T>(key: string, value: T, ttl: CacheTime) {
|
|||||||
},
|
},
|
||||||
body: JSON.stringify({ data: value, ttl: getCacheTimeInSeconds(ttl) }),
|
body: JSON.stringify({ data: value, ttl: getCacheTimeInSeconds(ttl) }),
|
||||||
cache: "no-cache",
|
cache: "no-cache",
|
||||||
|
signal: AbortSignal.timeout(3_000),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!response || error || !response.ok) {
|
if (!response || !response.ok || error) {
|
||||||
Sentry.captureMessage("Unable to SET cachekey", {
|
Sentry.captureException(error ?? new Error("Unable to SET cachekey"), {
|
||||||
level: "error",
|
|
||||||
extra: {
|
extra: {
|
||||||
cacheKey: key,
|
cacheKey: key,
|
||||||
errorMessage: error instanceof Error ? error.message : undefined,
|
|
||||||
statusCode: response?.status,
|
statusCode: response?.status,
|
||||||
statusText: response?.statusText,
|
statusText: response?.statusText,
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user