Skip to content

Commit

Permalink
feat: deal inactive channel status
Browse files Browse the repository at this point in the history
  • Loading branch information
cool-firer committed Oct 15, 2024
1 parent 8b01ffd commit a12aa7a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/network-support/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright 2020-2022 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { OrderManager } from './orderManager';
import { OrderManager, ResponseFormat } from './orderManager';
import { customFetch, generateUniqueId, Logger, safeJSONParse } from './utils';
import { OrderType } from './types';
import { ScoreType } from './scoreManager';
import { Base64 } from 'js-base64';
import { ActiveType } from './stateManager';

// prettier-ignore
const fatalErrorCodes = new Set([
Expand Down Expand Up @@ -97,6 +98,7 @@ export function createFetch(
}
const { url, headers, type, runner, channelId } = requestParams;
let httpVersion = 1;
let resHeaders: Headers | undefined;

try {
if (type === OrderType.fallback) {
Expand All @@ -116,6 +118,7 @@ export function createFetch(
url,
{
headers: {
'x-reqwst-id': requestId,
...(init.headers || {}),
...headers,
},
Expand All @@ -125,6 +128,7 @@ export function createFetch(
overrideFetch
);
const after = Date.now();
resHeaders = _res.headers;
httpVersion = Number(_res.headers.get('httpVersion')) || 1;

let res: object | undefined;
Expand Down Expand Up @@ -197,7 +201,7 @@ export function createFetch(
errorMsg = (e as Error)?.message || '';

if (!triedFallback && (retries < maxRetries || orderManager.fallbackServiceUrl)) {
const [needRetry, scoreType] = handleErrorMsg(errorMsg);
const [needRetry, scoreType] = handleErrorMsg(errorMsg, resHeaders);

if (needRetry) {
logger?.error({
Expand All @@ -211,6 +215,7 @@ export function createFetch(
stack: e.stack,
fallbackServiceUrl: orderManager.fallbackServiceUrl,
rid,
scoreType,
});
const extraLog = {
requestId,
Expand Down Expand Up @@ -261,7 +266,7 @@ export function createFetch(
};
}

function handleErrorMsg(errorMsg: string): [boolean, ScoreType] {
function handleErrorMsg(errorMsg: string, resHeaders?: Headers): [boolean, ScoreType] {
let needRetry = true;
let scoreType = ScoreType.RPC;
const errorObj = safeJSONParse(errorMsg);
Expand All @@ -281,6 +286,16 @@ function handleErrorMsg(errorMsg: string): [boolean, ScoreType] {
} else {
needRetry = false;
}
} else {
const fmt = resHeaders?.get('X-Indexer-Response-Format');
const state = resHeaders?.get('X-Channel-State');
if (fmt === ResponseFormat.Inline && state) {
const data = Base64.toUint8Array(state);
const active = data[0] as ActiveType;
if (active === ActiveType.Inactive2) {
scoreType = ScoreType.FATAL_INACTIVE;
}
}
}
return [needRetry, scoreType];
}
Expand Down
2 changes: 2 additions & 0 deletions packages/network-support/src/scoreManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export enum ScoreType {
RPC = 'RPC',
SUCCESS = 'success',
FATAL = 'fatal',
FATAL_INACTIVE = 'fatal_inactive',
}

const scoresDelta = {
Expand All @@ -35,6 +36,7 @@ const scoresDelta = {
[ScoreType.RPC]: -10,
[ScoreType.SUCCESS]: 50,
[ScoreType.FATAL]: -100,
[ScoreType.FATAL_INACTIVE]: -100,
};

type ScoreStoreType = {
Expand Down

0 comments on commit a12aa7a

Please sign in to comment.