Add health check and retry logic and circuit breaker

This commit is contained in:
SepehrYahyaee
2026-09-28 17:01:53 +03:30
parent 22371f4a03
commit 03fa5e0f20
67 changed files with 3101 additions and 410 deletions
@@ -84,7 +84,9 @@ export function createDeadlineError(label = 'Inquiry'): Error {
/**
* Transport / transient failures only.
* Does NOT retry 429 (rate limit / ban risk) or business rejects.
* Retries transient HTTP 404s because the configured legacy SOAP gateway is
* known to return 404 intermittently for requests that succeed later. Does
* NOT retry 429 (rate limit / ban risk) or business rejects.
*/
export function isTransportRetryable(error: unknown): boolean {
if (!error || typeof error !== 'object') {
@@ -101,7 +103,7 @@ export function isTransportRetryable(error: unknown): boolean {
if ('response' in error) {
const status = (error as { response?: { status?: number } }).response?.status;
if (typeof status === 'number') {
if (status === 408 || status === 499 || (status >= 500 && status < 600)) {
if (status === 404 || status === 408 || status === 499 || (status >= 500 && status < 600)) {
return true;
}
}
@@ -117,7 +119,7 @@ export function isTransportRetryable(error: unknown): boolean {
return normalized.code !== 'INQUIRY_DEADLINE_EXCEEDED';
}
const providerCode = normalized.providerCode?.trim();
if (providerCode && /^(408|499|5\d\d)$/.test(providerCode)) {
if (providerCode && /^(404|408|499|5\d\d)$/.test(providerCode)) {
return true;
}
}
@@ -149,6 +151,7 @@ export function isAuthTransportRetryable(error: unknown): boolean {
export function toAttemptErrorFields(error: unknown): {
code: string;
providerCode?: string;
message: string;
messageFa?: string;
} {
@@ -158,6 +161,7 @@ export function toAttemptErrorFields(error: unknown): {
);
return {
code: normalized.code,
providerCode: normalized.providerCode,
message: normalized.message,
messageFa: normalized.messageFa,
};
@@ -168,7 +172,7 @@ export function toAttemptErrorFields(error: unknown): {
const code =
error.code === 'ETIMEDOUT' || error.code === 'ECONNABORTED'
? 'PROVIDER_TIMEOUT'
: status && status >= 500
: status
? 'PROVIDER_ERROR'
: 'PROVIDER_NETWORK_ERROR';
const normalized = translateError(
@@ -178,6 +182,7 @@ export function toAttemptErrorFields(error: unknown): {
);
return {
code: normalized.code,
providerCode: status ? String(status) : undefined,
message: normalized.message,
messageFa: normalized.messageFa,
};
@@ -219,6 +224,7 @@ export function recordResilienceAttempt(
attempt: trail.totalAttempts,
provider,
code: fields.code,
providerCode: fields.providerCode,
durationMs: info.durationMs,
message: fields.message,
messageFa: fields.messageFa,
@@ -239,7 +245,7 @@ export interface RunWithResilienceOptions {
* Shared retry + per-attempt timeout + inquiry deadline for all inquiry providers.
*/
export async function runWithProviderResilience<T>(
fn: () => Promise<T>,
fn: (signal?: AbortSignal) => Promise<T>,
options: RunWithResilienceOptions,
): Promise<T> {
const {
@@ -262,7 +268,14 @@ export async function runWithProviderResilience<T>(
throw createDeadlineError(label);
}
const attemptTimeout = Math.min(timeoutMs, remaining);
return withTimeout(fn(), attemptTimeout, label);
return withTimeout(
(signal) => {
context.signal = signal;
return fn(signal);
},
attemptTimeout,
label,
);
},
{
maxAttempts,