Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request paritytech#61 from subspace/relayer-fetch-parabloc…
Browse files Browse the repository at this point in the history
…k-timeout

Relayer fetch parablock timeout
  • Loading branch information
isSerge authored Oct 8, 2021
2 parents c50596e + 8db4e60 commit a318873
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
20 changes: 16 additions & 4 deletions relayer/src/parachain.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import fetch from "node-fetch";
import fetch, { RequestInit } from "node-fetch";
import { EMPTY, defer, from, Observable, catchError } from 'rxjs';
import { retry, shareReplay, timeout } from "rxjs/operators";
import { retry, shareReplay } from "rxjs/operators";
import { Hash, SignedBlock } from "@polkadot/types/interfaces";
import { U64 } from "@polkadot/types/primitive";
import { AddressOrPair } from "@polkadot/api/submittable/types";
Expand All @@ -10,6 +10,18 @@ import { Logger } from "pino";
import { ChainName } from './types';
import { isValidBlock } from './utils';


async function fetchWithTimeout(url: string, options: RequestInit) {
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), options.timeout);
const response = await fetch(url, {
...options,
signal: controller.signal
});
clearTimeout(id);
return response;
}

interface ParachainConstructorParams {
feedId: U64;
url: string;
Expand Down Expand Up @@ -45,11 +57,12 @@ class Parachain {
params: [hash],
}),
headers: { "Content-Type": "application/json" },
timeout: 8000,
};

this.logger.info(`Fetching ${this.chain} parablock: ${hash}`);

return defer(() => from(fetch(this.url, options)
return defer(() => from(fetchWithTimeout(this.url, options)
.then(response => {
if (!response.ok) {
throw new Error(`Could not fetch ${this.chain} parablock ${hash} from ${this.url}: ${response.statusText}`);
Expand All @@ -68,7 +81,6 @@ class Parachain {
return data.result;
})))
.pipe(
timeout(8000),
retry(3),
catchError((error) => {
this.logger.error(error);
Expand Down
6 changes: 4 additions & 2 deletions relayer/src/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ class Target {
const { address } = (signer as KeyringPair);
this.logger.info(`Checking feed for ${address}`);

const file = await fs.promises.readFile('./state/feeds.json', 'utf8');
const filePath = './state/feeds.json';

const file = await fs.promises.readFile(filePath, 'utf8');
const feeds = JSON.parse(file);

if (feeds[address]) {
Expand All @@ -154,7 +156,7 @@ class Target {

feeds[address] = feedId.toBn();

await fs.promises.writeFile('./feeds.json', JSON.stringify(feeds, null, 4));
await fs.promises.writeFile(filePath, JSON.stringify(feeds, null, 4));

return feedId;
}
Expand Down

0 comments on commit a318873

Please sign in to comment.