mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2026-01-25 12:44:06 +00:00
Added health metrics for application load and database load (#2257)
* Added health metrics for application load * Added health metrics for application load * Fix unit tests * Added more metrics * Added few comments * Added DatabaseLoad Scenario and address comments * Fix unit tests * fix unit tests * Fix unit tests * fix unit tests * fix the mock * Fix unit tests
This commit is contained in:
37
src/Utils/FetchWithTimeout.ts
Normal file
37
src/Utils/FetchWithTimeout.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Perform a fetch with an AbortController-based timeout. Returns the Response or throws (including AbortError on timeout).
|
||||
*
|
||||
* Usage: await fetchWithTimeout(url, { method: 'GET', headers: {...} }, 10000);
|
||||
*
|
||||
* A shared helper to remove duplicated inline implementations across the codebase.
|
||||
*/
|
||||
export async function fetchWithTimeout(
|
||||
url: string,
|
||||
init: RequestInit = {},
|
||||
timeoutMs: number = 5000,
|
||||
): Promise<Response> {
|
||||
const controller = new AbortController();
|
||||
const id = setTimeout(() => controller.abort(), timeoutMs);
|
||||
try {
|
||||
const response = await fetch(url, { ...init, signal: controller.signal });
|
||||
return response;
|
||||
} finally {
|
||||
clearTimeout(id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience wrapper that returns null instead of throwing on timeout / network error.
|
||||
* Useful for feature probing scenarios where failure should be treated as absence.
|
||||
*/
|
||||
export async function tryFetchWithTimeout(
|
||||
url: string,
|
||||
init: RequestInit = {},
|
||||
timeoutMs: number = 5000,
|
||||
): Promise<Response | null> {
|
||||
try {
|
||||
return await fetchWithTimeout(url, init, timeoutMs);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user