Skip to content

Commit 4fb5de2

Browse files
authored
Exit on no ping (#126)
* Exit on no ping We rely on the heartbeat the server sends to the client to make sure the connection is still alive. By calling the terminate function on the websocket, the whole process will exit. Solution based on: https://github.com/websockets/ws?tab=readme-ov-file#how-to-detect-and-close-broken-connections * Add log for receiving and submitting opportunities
1 parent 92b9d51 commit 4fb5de2

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

scripts/swap-beacon/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/swap-beacon/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swap-beacon",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

scripts/swap-beacon/src/index.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ export function getSwapAdapterConfig(chainId: string) {
2525

2626
export class SwapBeacon {
2727
private client: Client;
28-
private adapters: Adapter[];
29-
private chainIds: string[];
28+
private pingTimeout: NodeJS.Timeout | undefined;
29+
private readonly adapters: Adapter[];
30+
private readonly chainIds: string[];
31+
private readonly PING_INTERVAL = 30000;
3032

3133
constructor(endpoint: string, _chainIds: string[]) {
3234
this.client = new Client(
@@ -213,6 +215,7 @@ export class SwapBeacon {
213215
}
214216

215217
async opportunityHandler(opportunity: Opportunity) {
218+
console.log("Received opportunity:", opportunity.opportunityId);
216219
const swapAdapterConfig = getSwapAdapterConfig(opportunity.chainId);
217220

218221
if (
@@ -235,6 +238,10 @@ export class SwapBeacon {
235238
return;
236239
}
237240
await this.client.submitOpportunity(convertedOpportunity);
241+
console.log(
242+
"Submitted converted opportunity. Original id:",
243+
opportunity.opportunityId
244+
);
238245
} catch (error) {
239246
console.error(
240247
`Failed to convert and submit opportunity ${JSON.stringify(
@@ -246,12 +253,25 @@ export class SwapBeacon {
246253
);
247254
}
248255

256+
heartbeat() {
257+
if (this.pingTimeout !== undefined) clearTimeout(this.pingTimeout);
258+
259+
this.pingTimeout = setTimeout(() => {
260+
console.error("Received no ping. Terminating connection.");
261+
this.client.websocket.terminate();
262+
}, this.PING_INTERVAL + 2000); // 2 seconds for latency
263+
}
264+
249265
async start() {
250266
try {
251267
await this.client.subscribeChains(this.chainIds);
252268
console.log(
253269
`Subscribed to chain ${this.chainIds}. Waiting for opportunities...`
254270
);
271+
this.heartbeat();
272+
this.client.websocket.on("ping", () => {
273+
this.heartbeat();
274+
});
255275
} catch (error) {
256276
console.error(error);
257277
this.client.websocket?.close();

0 commit comments

Comments
 (0)