Skip to content

Commit 9fb34e9

Browse files
committed
fix(apps/price_pusher): better price feed listener error handling
1 parent 623047b commit 9fb34e9

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

apps/price_pusher/src/pyth-price-listener.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from "@pythnetwork/hermes-client";
66
import { PriceInfo, IPriceListener, PriceItem } from "./interface";
77
import { Logger } from "pino";
8+
import { sleep } from "./utils";
89

910
type TimestampInMs = number & { readonly _: unique symbol };
1011

@@ -34,6 +35,24 @@ export class PythPriceListener implements IPriceListener {
3435
// This method should be awaited on and once it finishes it has the latest value
3536
// for the given price feeds (if they exist).
3637
async start() {
38+
this.startListening();
39+
40+
// Store health check interval reference
41+
this.healthCheckInterval = setInterval(() => {
42+
if (
43+
this.lastUpdated === undefined ||
44+
this.lastUpdated < Date.now() - 30 * 1000
45+
) {
46+
throw new Error("Hermes Price feeds are not updating.");
47+
}
48+
}, 5000);
49+
}
50+
51+
async startListening() {
52+
this.logger.info(
53+
`Starting to listen for price updates from Hermes for ${this.priceIds.length} price feeds.`,
54+
);
55+
3756
const eventSource = await this.hermesClient.getPriceUpdatesStream(
3857
this.priceIds,
3958
{
@@ -71,20 +90,12 @@ export class PythPriceListener implements IPriceListener {
7190
});
7291
};
7392

74-
eventSource.onerror = (error: Event) => {
93+
eventSource.onerror = async (error: Event) => {
7594
console.error("Error receiving updates from Hermes:", error);
7695
eventSource.close();
96+
await sleep(5000); // Wait a bit before trying to reconnect
97+
this.startListening(); // Attempt to restart the listener
7798
};
78-
79-
// Store health check interval reference
80-
this.healthCheckInterval = setInterval(() => {
81-
if (
82-
this.lastUpdated === undefined ||
83-
this.lastUpdated < Date.now() - 30 * 1000
84-
) {
85-
throw new Error("Hermes Price feeds are not updating.");
86-
}
87-
}, 5000);
8899
}
89100

90101
getLatestPriceInfo(priceId: HexString): PriceInfo | undefined {

0 commit comments

Comments
 (0)