Permalink
Browse files

added agentOptions to Pool constructor for passing options to the und…

…erlying Agent
  • Loading branch information...
dannycoates committed Jan 7, 2013
1 parent 858fc1f commit 329197d275be4368f2dd4afd4f78cfbdd529fc6a
Showing with 75 additions and 3 deletions.
  1. +4 −3 lib/endpoint.js
  2. +1 −0 lib/pool.js
  3. +6 −0 readme.md
  4. +24 −0 test/endpoint_test.js
  5. +40 −0 test/integration_test.js
View
@@ -19,6 +19,7 @@ module.exports = function (inherits, EventEmitter, Pinger) {
// timeout: default request timeout in ms (60000)
// resolution: how often timeouts are checked in ms (1000)
// keepAlive: use an alternate Agent that does keep-alive properly (boolean) default false
+ // agentOptions: {} an object for passing options directly to the Http Agent
// }
function Endpoint(protocol, ip, port, options) {
options = options || {}
@@ -39,14 +40,14 @@ module.exports = function (inherits, EventEmitter, Pinger) {
this.pingTimeout = options.pingTimeout || 2000
if (options.keepAlive) {
if (protocol === http) {
- this.agent = new KeepAlive()
+ this.agent = new KeepAlive(options.agentOptions)
}
else {
- this.agent = new KeepAlive.Secure()
+ this.agent = new KeepAlive.Secure(options.agentOptions)
}
}
else {
- this.agent = new protocol.Agent()
+ this.agent = new protocol.Agent(options.agentOptions)
}
this.agent.maxSockets = options.maxSockets || 20
View
@@ -16,6 +16,7 @@ module.exports = function (inherits, EventEmitter, Endpoint, RequestSet) {
// keepAlive: use an alternate Agent that does keep-alive properly (boolean) default false
// name: string (optional)
// maxRetries: number (default = 5)
+ // agentOptions: {} an object for passing options directly to the Http Agent
// }
function Pool(http, nodes, options) {
options = options || {}
View
@@ -70,6 +70,7 @@ var pool = new Pool(
, retryDelay: 20 // see below
, maxRetries: 5 // see below
, name: undefined // optional string
+ , agentOptions: undefined// an object for passing options directly to the Http Agent
}
)
```
@@ -123,6 +124,11 @@ If `retryDelay` is 20, attemptNumber 1 (the first retry) will delay at most 40ms
The maximum number of attempts to make after the first request fails. This only
takes effect if maxRetries < pool size.
+###### agentOptions
+
+These options are passed directly to the underlying Agents used in the pool. This
+is nice for passing options like `cert` and `key` that are required for client certificates.
+
###### ping
When an endpoint is unresponsive the pool will not use it for requests. The ping
View
@@ -2,6 +2,7 @@ var assert = require("assert")
var EventEmitter = require("events").EventEmitter
var inherits = require('util').inherits
var http = require('http')
+var https = require('https')
var Stream = require('stream')
var noop = function () {}
@@ -11,6 +12,29 @@ var Endpoint = require("../lib/endpoint")(inherits, EventEmitter, Pinger)
describe("Endpoint", function () {
+ it("passes nothing to the Agent constructor when no agentOptions are given", function () {
+ var e = new Endpoint(http, '127.0.0.1', 6969, { bogus: true })
+ assert.equal(e.agent.options.bogus, undefined)
+ })
+
+ it("passes agentOptions to the underlying Agent (no keep-alive)", function () {
+ var e = new Endpoint(http, '127.0.0.1', 6969, { agentOptions: { cert: 'foo', key: 'bar'}})
+ assert.equal(e.agent.options.cert, 'foo')
+ assert.equal(e.agent.options.key, 'bar')
+ })
+
+ it("passes agentOptions to the underlying Agent (keep-alive)", function () {
+ var e = new Endpoint(http, '127.0.0.1', 6969, {keepAlive: true, agentOptions: { cert: 'foo', key: 'bar'}})
+ assert.equal(e.agent.options.cert, 'foo')
+ assert.equal(e.agent.options.key, 'bar')
+ })
+
+ it("passes agentOptions to the underlying Agent (keep-alive secure)", function () {
+ var e = new Endpoint(https, '127.0.0.1', 6969, {keepAlive: true, agentOptions: { cert: 'foo', key: 'bar'}})
+ assert.equal(e.agent.options.cert, 'foo')
+ assert.equal(e.agent.options.key, 'bar')
+ })
+
//
// unhealthy
//
View
@@ -0,0 +1,40 @@
+var assert = require("assert")
+var Pool = require('../index')
+var http = require('http')
+
+var noop = function () {}
+
+describe('Pool', function () {
+ var pool
+
+ beforeEach(function () {
+ pool = new Pool(http, ['127.0.0.1:6969'])
+ })
+
+ //
+ // request
+ //
+ //////////////////////////////////////////////////////////////////////////////
+
+ describe("request()", function () {
+
+ it("passes options all the way to the endpoint request", function (done) {
+ var s = http.createServer(function (req, res) {
+ res.end("foo")
+ s.close()
+ })
+ s.on('listening', function () {
+ pool.request({
+ path: '/foo',
+ method: 'GET',
+ ca: 'bar.ca'
+ }, null, function (e, r, b) {
+ done()
+ })
+ var req = pool.get_node().requests[0]
+ assert.equal(req.options.ca, 'bar.ca')
+ })
+ s.listen(6969)
+ })
+ })
+})

0 comments on commit 329197d

Please sign in to comment.