Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ export const HEADER_KEYS: Record<string, string> = {
CONTENT_TYPE: `Content-Type`,
};

export const RESPONSE_HEADER_KEYS: Record<string, string> = {
export const RESPONSE_HEADER_KEYS = {
RETRY_ATTEMPT_COUNT: `x-${POWERED_BY}-retry-attempt-count`,
LAST_USED_OPTION_INDEX: `x-${POWERED_BY}-last-used-option-index`,
LAST_USED_OPTION_PARAMS: `x-${POWERED_BY}-last-used-option-params`,
CACHE_STATUS: `x-${POWERED_BY}-cache-status`,
TRACE_ID: `x-${POWERED_BY}-trace-id`,
CIRCUIT_BREAKER_TRIGGERED: `x-${POWERED_BY}-circuit-breaker-triggered`,
};

export const RETRY_STATUS_CODES = [429, 500, 502, 503, 504];
Expand Down
33 changes: 25 additions & 8 deletions src/handlers/handlerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ export async function tryPost(
requestHeaders: Record<string, string>,
fn: endpointStrings,
currentIndex: number | string,
method: string = 'POST'
method: string = 'POST',
isCircuitBreakerOpen: boolean = false
): Promise<Response> {
const overrideParams = providerOption?.overrideParams || {};
let params: Params =
Expand Down Expand Up @@ -495,7 +496,8 @@ export async function tryPost(
cacheStatus,
retryCount ?? 0,
requestHeaders[HEADER_KEYS.TRACE_ID] ?? '',
provider
provider,
isCircuitBreakerOpen
);

c.set('requestOptions', [
Expand Down Expand Up @@ -590,7 +592,8 @@ export async function tryTargetsRecursively(
fn: endpointStrings,
method: string,
jsonPath: string,
inheritedConfig: Record<string, any> = {}
inheritedConfig: Record<string, any> = {},
isCircuitBreakerOpen: boolean = false
): Promise<Response> {
const currentTarget: any = { ...targetGroup };
let currentJsonPath = jsonPath;
Expand Down Expand Up @@ -762,6 +765,9 @@ export async function tryTargetsRecursively(
.filter((t: any) => !t.isOpen);

if (healthyTargets.length) {
if (healthyTargets.length < currentTarget.targets.length) {
isCircuitBreakerOpen = true;
}
currentTarget.targets = healthyTargets;
}
}
Expand Down Expand Up @@ -818,7 +824,8 @@ export async function tryTargetsRecursively(
fn,
method,
currentJsonPath,
currentInheritedConfig
currentInheritedConfig,
isCircuitBreakerOpen
);
break;
}
Expand Down Expand Up @@ -862,7 +869,8 @@ export async function tryTargetsRecursively(
fn,
method,
`${currentJsonPath}.targets[${originalIndex}]`,
currentInheritedConfig
currentInheritedConfig,
isCircuitBreakerOpen
);
break;
}
Expand All @@ -877,7 +885,8 @@ export async function tryTargetsRecursively(
fn,
method,
`${currentJsonPath}.targets[${originalIndex}]`,
currentInheritedConfig
currentInheritedConfig,
isCircuitBreakerOpen
);
break;

Expand All @@ -890,7 +899,8 @@ export async function tryTargetsRecursively(
requestHeaders,
fn,
currentJsonPath,
method
method,
isCircuitBreakerOpen
);
if (isHandlingCircuitBreaker) {
await c.get('handleCircuitBreakerResponse')?.(
Expand Down Expand Up @@ -959,7 +969,8 @@ export function updateResponseHeaders(
cacheStatus: string | undefined,
retryAttempt: number,
traceId: string,
provider: string
provider: string,
isCircuitBreakerOpen: boolean
) {
response.headers.append(
RESPONSE_HEADER_KEYS.LAST_USED_OPTION_INDEX,
Expand Down Expand Up @@ -991,6 +1002,12 @@ export function updateResponseHeaders(
if (provider && provider !== POWERED_BY) {
response.headers.append(HEADER_KEYS.PROVIDER, provider);
}
if (isCircuitBreakerOpen) {
response.headers.append(
RESPONSE_HEADER_KEYS.CIRCUIT_BREAKER_TRIGGERED,
'true'
);
}
}

export function constructConfigFromRequestHeaders(
Expand Down