From 33b45768a21ed5fb45c043a1544f8769e433daca Mon Sep 17 00:00:00 2001 From: Quinn Damerell Date: Mon, 21 Feb 2022 11:18:03 -0800 Subject: [PATCH] Fixing a short WS connect timeout in SockJs (#4427) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adding a longer default sockjs websocket timeout to improve remote OctoPrint connections. * Fixing pre-commit issues. * ♻️ sockjsWebsocketConnectTimeout -> transportTimeout Co-authored-by: Gina Häußge --- src/octoprint/static/js/app/client/socket.js | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/octoprint/static/js/app/client/socket.js b/src/octoprint/static/js/app/client/socket.js index 5e61b7cf4..8408e5606 100644 --- a/src/octoprint/static/js/app/client/socket.js +++ b/src/octoprint/static/js/app/client/socket.js @@ -15,6 +15,7 @@ this.options = { timeouts: [0, 1, 1, 2, 3, 5, 8, 13, 20, 40, 100], connectTimeout: 5000, + transportTimeout: 4000, rateSlidingWindowSize: 20 }; @@ -168,6 +169,31 @@ }, timeout); } + /* + * We define both a connectTimeout and a transportTimeout because they do + * different things: + * + * - connectTimeout defines how long this socket class abstraction will wait + * for sockjs to get to a connected state, regardless of which transport + * connects. + * - transportTimeout defines how long sockjs will wait on the + * initial websocket to connect before falling back to a worse + * (but might work) transport. + * + * We need to define transportTimeout because the default in + * sockjs is very low, around 200ms. This limit hinders the performance of + * remote OctoPrint plugins and other remote OctoPrint connections and forces + * them to fall back to the inferior http polling based sockjs transports. + * This change will also help lower powered OctoPrint devices that might + * need more time to process the influx of requests on OctoPrint initial load. + */ + var transportTimeout = self.options.transportTimeout; + if (opts.hasOwnProperty("transportTimeout")) { + transportTimeout = opts.transportTimeout; + delete opts.transportTimeout; + } + opts.timeout = transportTimeout; + self.socket = new SockJS(url + "sockjs", undefined, opts); self.socket.onopen = onOpen; self.socket.onclose = onClose;