From 75578ac9fa5ac4b08bd19fa332bc184c16f09a3c Mon Sep 17 00:00:00 2001 From: Mononaut Date: Tue, 21 Nov 2023 17:20:11 +0900 Subject: [PATCH] Improve timeout handling in esplora api health monitoring & logs --- backend/src/api/bitcoin/esplora-api.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/backend/src/api/bitcoin/esplora-api.ts b/backend/src/api/bitcoin/esplora-api.ts index 2f4bcee85..b8e3784c7 100644 --- a/backend/src/api/bitcoin/esplora-api.ts +++ b/backend/src/api/bitcoin/esplora-api.ts @@ -1,5 +1,5 @@ import config from '../../config'; -import axios, { AxiosResponse } from 'axios'; +import axios, { AxiosResponse, isAxiosError } from 'axios'; import http from 'http'; import { AbstractBitcoinApi } from './bitcoin-api-abstract-factory'; import { IEsploraApi } from './esplora-api.interface'; @@ -10,6 +10,7 @@ interface FailoverHost { host: string, rtts: number[], rtt: number, + timedOut?: boolean, failures: number, latestHeight?: number, socket?: boolean, @@ -108,11 +109,17 @@ class FailoverRouter { host.rtts = []; host.rtt = Infinity; } + host.timedOut = false; } catch (e) { host.outOfSync = true; host.unreachable = true; host.rtts = []; host.rtt = Infinity; + if (isAxiosError(e) && (e.code === 'ECONNABORTED' || e.code === 'ETIMEDOUT')) { + host.timedOut = true; + } else { + host.timedOut = false; + } } host.checked = true; @@ -143,7 +150,7 @@ class FailoverRouter { private formatRanking(index: number, host: FailoverHost, active: FailoverHost, maxHeight: number): string { const heightStatus = !host.checked ? '⏳' : (host.outOfSync ? '🚫' : (host.latestHeight && host.latestHeight < maxHeight ? '🟧' : '✅')); - return `${host === active ? '⭐️' : ' '} ${host.rtt < Infinity ? Math.round(host.rtt).toString().padStart(5, ' ') + 'ms' : ' - '} ${!host.checked ? '⏳' : (host.unreachable ? '🔥' : '✅')} | block: ${host.latestHeight || '??????'} ${heightStatus} | ${host.host} ${host === active ? '⭐️' : ' '}`; + return `${host === active ? '⭐️' : ' '} ${host.rtt < Infinity ? Math.round(host.rtt).toString().padStart(5, ' ') + 'ms' : (host.timedOut ? ' ⌛️💥 ' : ' - ')} ${!host.checked ? '⏳' : (host.unreachable ? '🔥' : '✅')} | block: ${host.latestHeight || '??????'} ${heightStatus} | ${host.host} ${host === active ? '⭐️' : ' '}`; } private updateFallback(): FailoverHost[] {