Skip to content
This repository was archived by the owner on Sep 25, 2020. It is now read-only.
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
26 changes: 20 additions & 6 deletions pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
var GO, // GO is global object, for passing to a REPL or finding in a core dump
EventEmitter = require("events").EventEmitter,
Stream = require("stream"),
inherits = require("util").inherits;
inherits = require("util").inherits,
http = require('http');

// Pool - manages a set of equivalent endpoints and distributes requests among them
//
// http: which endpoint HTTP module to use, either http.js or https.js
// protocol: which endpoint HTTP module to use, either http.js or https.js
// endpoints: array of strings formatted like "ip:port"
// options: {
// max_pending: number of pending requests allowed (1000)
Expand All @@ -21,14 +22,14 @@ var GO, // GO is global object, for passing to a REPL or finding in a core dump
// max_retries: number (default = 5)
// agent_options: {} an object for passing options directly to the HTTP(S) Agent
// }
function Pool(http, endpoints, options) {
if (!http || !http.request || !http.Agent) {
function Pool(protocol, endpoints, options) {
if (!protocol || !protocol.request || !protocol.Agent) {
throw new Error("invalid http module");
}
if (! Array.isArray(endpoints)) {
throw new Error("endpoints must be an array");
}
this.http = http;
this.protocol = protocol;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: property name http is more clearly a module to me than protocol which sounds like it is going to be a string "http" or "https"


options = options || {};
options.retry_filter = options.retry_filter || options.retryFilter;
Expand All @@ -52,6 +53,8 @@ function Pool(http, endpoints, options) {
this.max_pending = options.max_pending || options.maxPending || 1000;
this.endpoints = [];
this.endpoints_by_name = {};
this.keep_alive = options.keep_alive || options.keepAlive;
this.agent_options = options.agent_options || options.agentOptions;

this.length = 0;
for (var i = 0; i < endpoints.length; i++) {
Expand All @@ -62,6 +65,17 @@ function Pool(http, endpoints, options) {
throw new Error("no valid endpoints");
}


if (this.keep_alive) {
if (protocol === http) {
this.agent = new GO.KeepAliveAgent.HTTP(this.agent_options);
} else {
this.agent = new GO.KeepAliveAgent.HTTPS(this.agent_options);
}
} else {
this.agent = new protocol.Agent(this.agent_options);
}

// this special endpoint is returned when the pool is overloaded
this.overloaded_endpoint = new GO.PoolEndpoint({Agent: Object}, null, null, {timeout: 0});
this.overloaded_endpoint.special_endpoint = "overloaded";
Expand Down Expand Up @@ -309,7 +323,7 @@ Pool.prototype.add_endpoint = function (host_port) {
var ip = ip_port[0];
var port = +ip_port[1];
if (port > 0 && port < 65536) {
var endpoint = new GO.PoolEndpoint(this.http, ip, port, this.options);
var endpoint = new GO.PoolEndpoint(this.protocol, ip, port, this.options);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like you intended for agent to be a property of options but I don't see that anywhere

endpoint.on("health", this.endpoint_health_changed.bind(this));
endpoint.on("timeout", this.endpoint_timed_out.bind(this));
this.endpoints.push(endpoint);
Expand Down
16 changes: 10 additions & 6 deletions pool_endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ function PoolEndpoint(protocol, ip, port, options) {
this.keep_alive = options.keep_alive || options.keepAlive;
this.agent_options = options.agent_options || options.agentOptions;

if (this.keep_alive) {
if (protocol === http) {
this.agent = new GO.KeepAliveAgent.HTTP(this.agent_options);
if (options.agent) {
this.agent = options.agent;
} else {
if (this.keep_alive) {
if (protocol === http) {
this.agent = new GO.KeepAliveAgent.HTTP(this.agent_options);
} else {
this.agent = new GO.KeepAliveAgent.HTTPS(this.agent_options);
}
} else {
this.agent = new GO.KeepAliveAgent.HTTPS(this.agent_options);
this.agent = new protocol.Agent(this.agent_options);
}
} else {
this.agent = new protocol.Agent(this.agent_options);
}

if (this.agent && typeof this.agent.getName === 'function') {
Expand Down