Permalink
Browse files

Merge pull request #7 from dannycoates/keepalive

Add a new option for different keep-alive behaviour
  • Loading branch information...
2 parents 720a9ee + 855f0eb commit c2e5e765c9183f0d77e7b44f2a200f9bfa00d7e9 @dannycoates committed Nov 16, 2012
Showing with 43 additions and 331 deletions.
  1. +0 −82 benchmark/mini_cluster.js
  2. +0 −29 benchmark/mini_server.js
  3. +0 −34 examples/client.js
  4. +0 −20 examples/worker.js
  5. +16 −4 lib/endpoint.js
  6. +1 −0 lib/pool.js
  7. +1 −1 lib/request_set.js
  8. +3 −1 package.json
  9. +22 −160 readme.md
View
@@ -1,82 +0,0 @@
-var spawn = require('child_process').spawn
-var http = require('http')
-var argv = require('optimist').argv
-var Pool = require('../index')
-
-var instances = argv.n
-var start = argv.start
-
-console.log("starting " + instances + " servers. starting at port " + start)
-
-var exited = 0
-function done(code) {
- exited++
- if (exited === instances) {
- process.exit(0)
- }
-}
-
-var children = []
-var nodes = []
-
-function create(i, port) {
- console.log("spawning server on port " + port)
- var p = spawn
- ( 'node'
- , [ 'mini_server.js'
- , '--port=' + port
- ]
- )
- p.stdout.on('data', function (data) {
- console.log(data.toString())
- })
- p.stderr.on('data', function (data) {
- console.log('\t\033[31m' + data.toString() + '\033[39m')
- })
- p.on('exit', done)
- children.push(p)
- nodes.push('127.0.0.1:' + port)
-}
-
-for (var i = 0; i < instances; i++) {
- create(i, start + i)
-}
-
-var pool = new Pool(http, nodes, {maxPending: 100 })
-pool.on('timing', function (time, op) {
- if (!op.success) {
- console.error('\033[31m' + time + '\033[39m')
- }
- // else {
- // console.error(time)
- // }
-})
-var x = 0
-var start = Date.now()
-var r = 10000
-var a = r
-var delay = 0 + Math.floor(Math.random() * 10)
-
-function result(error, response, body) {
- if (error) {
- console.error('\033[31m' + error.reason + '\033[39m')
- r--
- }
- else {
- x++
- //console.error(pool.nodes.map(function (n) { return n.requestRate }))
- }
- if (x % 1000 === 0) {//if (x === r) {
- console.error(pool.nodes.map(function (n) { return n.requestRate }))
- console.error((a - r) + " failed")
- console.error(Date.now() - start)
- //children.forEach(function (c) { c.kill() })
- }
- }
-
-setInterval(function () {
- for (var i = 0; i < 5; i++) {
- pool.get({ path: "/?delay=" + delay, attempts: 10, retryDelay: 50 }, result)
- }
-}, 1)
-
View
@@ -1,29 +0,0 @@
-var argv = require('optimist').argv
-var http = require('http')
-var url = require('url')
-
-var i = 0
-var port = argv.port
-
-function respond(res) {
- res.writeHead(200, {'Content-Type': 'text/plain'})
- res.end(port + ":" + (i++))
-}
-
-http.createServer(
- function (req, res) {
- var q = url.parse(req.url, true)
- var delay = q.query ? +q.query.delay : 0
- if (delay) {
- setTimeout(
- function () {
- respond(res)
- }
- , delay
- )
- }
- else {
- respond(res)
- }
- }
-).listen(port, '127.0.0.1')
View
@@ -1,34 +0,0 @@
-var Poolee = require('../')
- , http = require('http')
- , ms = require('ms') // converts a time to milliseconds
- , servers = [ '127.0.0.1:8080', '127.0.0.1:8081', '127.0.0.1:8082' ]
- , pool = null
-
- // healthiest node, populated later on
- , active_node = null
-
-pool = new Poolee(http, servers)
-
-pool.on('retrying', function(error) {
- console.log(error.message);
-})
-
-console.log('fib(40) = ...calculating on worker.js...');
-pool.request(
- { method: 'GET'
- , path: '/'
- }
-, function (error, response, body) {
- if (error) {
- console.error(error.message)
- return
- }
- if (response.statusCode === 200) {
- console.log(body)
-
- } else {
- console.log(response.statusCode)
- console.log(body)
- }
- }
-)
View
@@ -1,20 +0,0 @@
-// An http server that does things for you!
-// Do not write your fib server this way, instead use
-// https://gist.github.com/2018811 which this code is based on.
-var http = require('http')
-var PORT = process.argv[2]
-
-function fib(n) {
- if (n < 2) {
- return 1
- } else {
- return fib(n - 2) + fib(n - 1)
- }
-}
-
-var server = http.createServer(function(req, res) {
- res.writeHead(200)
- res.end(fib(40) + "\n")
-})
-server.listen(PORT)
-console.log("worker.js online at http://localhost:" + PORT)
View
@@ -1,4 +1,6 @@
var Stream = require('stream')
+var http = require('http')
+var KeepAlive = require('agentkeepalive')
module.exports = function (inherits, EventEmitter) {
var MAX_COUNT = Math.pow(2, 31) // largest smi value
@@ -16,20 +18,30 @@ module.exports = function (inherits, EventEmitter) {
// maxSockets: max concurrent open sockets (20)
// 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
// }
- function Endpoint(http, ip, port, options) {
+ function Endpoint(protocol, ip, port, options) {
options = options || {}
- this.http = http
+ this.http = protocol
this.ip = ip
this.port = port
this.healthy = true
this.name = this.ip + ':' + this.port
this.address = this.ip
this.pingPath = options.ping
this.pingTimeout = options.pingTimeout || 2000
-
- this.agent = new http.Agent()
+ if (options.keepAlive) {
+ if (protocol === http) {
+ this.agent = new KeepAlive()
+ }
+ else {
+ this.agent = new KeepAlive.HttpsAgent()
+ }
+ }
+ else {
+ this.agent = new protocol.Agent()
+ }
this.agent.maxSockets = options.maxSockets || 20
this.requests = {}
View
@@ -13,6 +13,7 @@ module.exports = function (inherits, EventEmitter, Endpoint, RequestSet) {
// pingTimeout: number (milliseconds) default 2000
// retryFilter: function (response) { return true to reject response and retry }
// retryDelay: number (milliseconds) default 20
+ // keepAlive: use an alternate Agent that does keep-alive properly (boolean) default false
// name: string (optional)
// maxRetries: number (default = 5)
// }
View
@@ -34,7 +34,7 @@ function attemptsFu(options, pool) {
if (options.data instanceof Stream) {
return 1
}
- return options.attempts || Math.min(pool.options.maxRetries, Math.max(pool.length, 2))
+ return options.attempts || Math.min(pool.options.maxRetries + 1, Math.max(pool.length, 2))
}
function exponentialBackoff(attempt, delay) {
View
@@ -10,7 +10,9 @@
"url": "http://github.com/dannycoates/poolee.git"
},
"engines" : { "node": ">=0.6.0" },
- "dependencies": {},
+ "dependencies": {
+ "agentkeepalive": "*"
+ },
"devDependencies": {
"mocha": "*",
"optimist": "*",
Oops, something went wrong.

0 comments on commit c2e5e76

Please sign in to comment.