From d1cf1d8f7dc599f653dedbe8eb03b5604d0ea0a3 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Thu, 9 Dec 2021 17:12:59 +0100 Subject: [PATCH] Add connection-state tracking as getter --- src/streamHandler/InfluxDbLineProtocolWriter.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/streamHandler/InfluxDbLineProtocolWriter.js b/src/streamHandler/InfluxDbLineProtocolWriter.js index 8eef2e5..514c2de 100644 --- a/src/streamHandler/InfluxDbLineProtocolWriter.js +++ b/src/streamHandler/InfluxDbLineProtocolWriter.js @@ -24,12 +24,15 @@ class InfluxDbLineProtocolWriter extends net.Socket{ options.autoReconnectBackoffTime ??= 3000; this._options = options; + this._isConnected = false; + super.setKeepAlive(true, 5000); // Register auto-Reconnect if enabled if(this._options.autoReconnect){ this.on("connect", () => { logger.debug("Connection established!"); + this._isConnected = true; if(this._autoReconnectTimeout) clearInterval(this._autoReconnectTimeout); @@ -38,6 +41,8 @@ class InfluxDbLineProtocolWriter extends net.Socket{ this.on("error", (err) => { logger.error(err.code, "TCP ERROR"); + this._isConnected = false; + if(!this._autoReconnectTimeout) this._autoReconnectTimeout = setInterval(() => { this.connect(); @@ -53,6 +58,8 @@ class InfluxDbLineProtocolWriter extends net.Socket{ get host(){ return this._host; } get port(){ return this._port; } + get isConnected(){ return this._isConnected; } + connect(){ logger.debug("Connecting.."); super.connect(this._port, this._host);