Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions lib/mixpanel-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ const create_client = function (token, config) {
config: { ...DEFAULT_CONFIG },
};
const { keepAlive } = metrics.config;
const maxSockets = config && config.maxSockets;

// 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, maxSockets }),
https: new https.Agent({ keepAlive, maxSockets }),
};
const proxyPath = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
const proxyAgent = proxyPath
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ var mixpanel = Mixpanel.init("<YOUR_TOKEN>", {
keepAlive: false,
});

// limit the number of concurrent connections to Mixpanel
var mixpanel = Mixpanel.init("<YOUR_TOKEN>", {
maxSockets: 10,
});

// pass the custom logger (default is console)
var mixpanel = Mixpanel.init("<YOUR_TOKEN>", {
debug: true,
Expand Down
23 changes: 23 additions & 0 deletions test/send_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,29 @@ describe("send_request", () => {
expect(getConfig.agent).toBe(agent);
});

it("uses custom maxSockets when configured", () => {
const agent = new https.Agent({ keepAlive: true });
const httpsStub = {
request: vi.fn().mockImplementation((_, cb) => {
cb(res);
return http_emitter;
}),
Agent: vi.fn().mockImplementation(function () {
return agent;
}),
};
delete process.env.HTTP_PROXY;
delete process.env.HTTPS_PROXY;
Mixpanel = proxyquire("../lib/mixpanel-node", {
https: httpsStub,
});
const proxyMixpanel = Mixpanel.init("token", { maxSockets: 10 });
proxyMixpanel.send_request({ endpoint: "", data: {} });

const agentOpts = httpsStub.Agent.mock.calls[0][0];
expect(agentOpts.maxSockets).toBe(10);
});

it("uses correct hostname", () => {
const host = "testhost.fakedomain";
const customHostnameMixpanel = Mixpanel.init("token", { host: host });
Expand Down