From 95107eb2eb87f8e93ec9477f568176ae82c46ee1 Mon Sep 17 00:00:00 2001 From: liamcottle Date: Fri, 29 Aug 2025 21:26:28 +1200 Subject: [PATCH] implement timeout for trace path --- src/connection/connection.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/connection/connection.js b/src/connection/connection.js index 99c8e15..d6e2986 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -1825,13 +1825,31 @@ class Connection extends EventEmitter { } - tracePath(path) { + tracePath(path, extraTimeoutMillis = 0) { return new Promise(async (resolve, reject) => { try { // generate a random tag for this trace, so we can listen for the correct response const tag = RandomUtils.getRandomInt(0, 4294967295); + // listen for sent response so we can get estimated timeout + var timeoutHandler = null; + const onSent = (response) => { + + // remove error listener since we received sent response + this.off(Constants.ResponseCodes.Err, onErr); + + // reject trace request as timed out after estimated delay, plus a bit extra + const estTimeout = response.estTimeout + extraTimeoutMillis; + timeoutHandler = setTimeout(() => { + this.off(Constants.ResponseCodes.Sent, onSent); + this.off(Constants.PushCodes.TraceData, onTraceDataPush); + this.off(Constants.ResponseCodes.Err, onErr); + reject("timeout"); + }, estTimeout); + + } + // resolve promise when we receive trace data const onTraceDataPush = (response) => { @@ -1842,6 +1860,8 @@ class Connection extends EventEmitter { } // resolve + clearTimeout(timeoutHandler); + this.off(Constants.ResponseCodes.Sent, onSent); this.off(Constants.PushCodes.TraceData, onTraceDataPush); this.off(Constants.ResponseCodes.Err, onErr); resolve(response); @@ -1850,12 +1870,15 @@ class Connection extends EventEmitter { // reject promise when we receive err const onErr = () => { + clearTimeout(timeoutHandler); + this.off(Constants.ResponseCodes.Sent, onSent); this.off(Constants.PushCodes.TraceData, onTraceDataPush); this.off(Constants.ResponseCodes.Err, onErr); reject(); } // listen for events + this.once(Constants.ResponseCodes.Sent, onSent); this.on(Constants.PushCodes.TraceData, onTraceDataPush); this.once(Constants.ResponseCodes.Err, onErr);