Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request timeout config setting #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 42 additions & 28 deletions lib/mixpanel-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
};

Expand Down