From 0e4cdb017126e18e7d78b1089bb693346e47659a Mon Sep 17 00:00:00 2001 From: Tadas Dailyda Date: Tue, 30 May 2023 11:27:21 +0300 Subject: [PATCH] add request timeout config setting --- lib/mixpanel-node.js | 70 ++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/lib/mixpanel-node.js b/lib/mixpanel-node.js index e7dc125..524a2c4 100644 --- a/lib/mixpanel-node.js +++ b/lib/mixpanel-node.js @@ -30,6 +30,7 @@ const DEFAULT_CONFIG = { // set this to true to automatically geolocate based on the client's ip. // e.g., when running under electron geolocate: false, + timeout: 0, }; var create_client = function(token, config) { @@ -41,18 +42,44 @@ var create_client = function(token, config) { token, config: {...DEFAULT_CONFIG}, }; - const {keepAlive} = metrics.config; + + /** + set_config(config) + --- + Modifies the mixpanel config + + config:object an object with properties to override in the + mixpanel client config + */ + metrics.set_config = function(config) { + Object.assign(metrics.config, config); + if (config.host) { + // Split host into host and port + const [host, port] = config.host.split(':'); + metrics.config.host = host; + if (port) { + metrics.config.port = Number(port); + } + } + }; + + if (config) { + metrics.set_config(config); + } + + const {keepAlive, timeout} = metrics.config; // mixpanel constants const MAX_BATCH_SIZE = 50; const REQUEST_LIBS = {http, https}; const REQUEST_AGENTS = { - http: new http.Agent({keepAlive}), - https: new https.Agent({keepAlive}), + http: new http.Agent({keepAlive, timeout}), + https: new https.Agent({keepAlive, timeout}), }; const proxyPath = process.env.HTTPS_PROXY || process.env.HTTP_PROXY; const proxyAgent = proxyPath ? new HttpsProxyAgent(Object.assign(url.parse(proxyPath), { - keepAlive, + keepAlive, + timeout })) : null; /** @@ -151,12 +178,23 @@ var create_client = function(token, config) { }); request.on('error', function(e) { + if (request.aborted) { + return; // error callback already called on timeout + } if (metrics.config.debug) { console.log("Got Error: " + e.message); } callback(e); }); + request.on('timeout', function() { + if (metrics.config.debug) { + console.log("Timed out"); + } + request.abort(); + callback(new Error("Mixpanel Client request timed out after " + metrics.config.timeout + " ms")); + }); + if (method === 'POST') { request.write(content); } @@ -437,30 +475,6 @@ var create_client = function(token, config) { metrics.groups = new MixpanelGroups(metrics); metrics.people = new MixpanelPeople(metrics); - /** - set_config(config) - --- - Modifies the mixpanel config - - config:object an object with properties to override in the - mixpanel client config - */ - metrics.set_config = function(config) { - Object.assign(metrics.config, config); - if (config.host) { - // Split host into host and port - const [host, port] = config.host.split(':'); - metrics.config.host = host; - if (port) { - metrics.config.port = Number(port); - } - } - }; - - if (config) { - metrics.set_config(config); - } - return metrics; };