Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dictionary query timeout option #1177

Merged
merged 2 commits into from
Jul 10, 2022
Merged
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
6 changes: 6 additions & 0 deletions packages/node/src/configure/NodeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface IConfig {
readonly proofOfIndex: boolean;
readonly mmrPath?: string;
readonly ipfs?: string;
readonly dictionaryTimeout: number;
}

export type MinConfig = Partial<Omit<IConfig, 'subquery'>> &
Expand All @@ -46,6 +47,7 @@ const DEFAULT_CONFIG = {
indexCountLimit: 10,
timestampField: true,
proofOfIndex: false,
dictionaryTimeout: 30,
};

export class NodeConfig implements IConfig {
Expand Down Expand Up @@ -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`;
}
Expand Down
27 changes: 19 additions & 8 deletions packages/node/src/indexer/dictionary.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -113,7 +115,10 @@ export class DictionaryService implements OnApplicationShutdown {
private client: ApolloClient<NormalizedCacheObject>;
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 }),
Expand Down Expand Up @@ -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<number>();
const specVersionBlockHeightSet = new Set<number>();
const entityEndBlock: { [entity: string]: number } = {};
Expand Down Expand Up @@ -277,9 +285,12 @@ export class DictionaryService implements OnApplicationShutdown {
async getSpecVersionsRaw(): Promise<SpecVersionDictionary> {
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;
Expand Down
5 changes: 5 additions & 0 deletions packages/node/src/yargs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down