diff --git a/packages/node/src/configure/NodeConfig.ts b/packages/node/src/configure/NodeConfig.ts index 9f3a7fed3a..daa037077a 100644 --- a/packages/node/src/configure/NodeConfig.ts +++ b/packages/node/src/configure/NodeConfig.ts @@ -31,6 +31,7 @@ export interface IConfig { readonly proofOfIndex: boolean; readonly mmrPath?: string; readonly ipfs?: string; + readonly dictionaryTimeout: number; } export type MinConfig = Partial> & @@ -46,6 +47,7 @@ const DEFAULT_CONFIG = { indexCountLimit: 10, timestampField: true, proofOfIndex: false, + dictionaryTimeout: 30, }; export class NodeConfig implements IConfig { @@ -135,6 +137,10 @@ export class NodeConfig implements IConfig { return this._config.proofOfIndex; } + get dictionaryTimeout(): number { + return this._config.dictionaryTimeout; + } + get mmrPath(): string { return this._config.mmrPath ?? `.mmr/${this.subqueryName}.mmr`; } diff --git a/packages/node/src/indexer/dictionary.service.ts b/packages/node/src/indexer/dictionary.service.ts index ec5c12c927..6073d418bc 100644 --- a/packages/node/src/indexer/dictionary.service.ts +++ b/packages/node/src/indexer/dictionary.service.ts @@ -12,9 +12,11 @@ import { Injectable, OnApplicationShutdown } from '@nestjs/common'; import { DictionaryQueryCondition, DictionaryQueryEntry } from '@subql/types'; import { buildQuery, GqlNode, GqlQuery, GqlVar, MetaData } from '@subql/utils'; import fetch from 'node-fetch'; +import { NodeConfig } from '../configure/NodeConfig'; import { SubqueryProject } from '../configure/SubqueryProject'; import { getLogger } from '../utils/logger'; import { profiler } from '../utils/profiler'; +import { timeout } from '../utils/promise'; import { getYargsOption } from '../yargs'; export type SpecVersion = { @@ -113,7 +115,10 @@ export class DictionaryService implements OnApplicationShutdown { private client: ApolloClient; private isShutdown = false; - constructor(protected project: SubqueryProject) { + constructor( + protected project: SubqueryProject, + private nodeConfig: NodeConfig, + ) { this.client = new ApolloClient({ cache: new InMemoryCache({ resultCaching: true }), link: new HttpLink({ uri: this.project.network.dictionary, fetch }), @@ -155,10 +160,13 @@ export class DictionaryService implements OnApplicationShutdown { ); try { - const resp = await this.client.query({ - query: gql(query), - variables, - }); + const resp = await timeout( + this.client.query({ + query: gql(query), + variables, + }), + this.nodeConfig.dictionaryTimeout, + ); const blockHeightSet = new Set(); const specVersionBlockHeightSet = new Set(); const entityEndBlock: { [entity: string]: number } = {}; @@ -277,9 +285,12 @@ export class DictionaryService implements OnApplicationShutdown { async getSpecVersionsRaw(): Promise { const { query } = this.specVersionQuery(); try { - const resp = await this.client.query({ - query: gql(query), - }); + const resp = await timeout( + this.client.query({ + query: gql(query), + }), + this.nodeConfig.dictionaryTimeout, + ); const _metadata = resp.data._metadata; const specVersions = resp.data.specVersions; diff --git a/packages/node/src/yargs.ts b/packages/node/src/yargs.ts index ecde9ffd19..dc39ad7770 100644 --- a/packages/node/src/yargs.ts +++ b/packages/node/src/yargs.ts @@ -117,6 +117,11 @@ export function getYargsOption() { describe: 'Specify the dictionary api for this network', type: 'string', }, + 'dictionary-timeout': { + demandOption: false, + describe: 'Max timeout for dictionary query', + type: 'number', + }, 'mmr-path': { alias: 'm', demandOption: false,