Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updated readme for maxRetries. Also documented events

  • Loading branch information...
commit 855f910a77d3258654255540142c75049abda86d 1 parent 8fd5baa
@dannycoates authored
View
82 benchmark/mini_cluster.js
@@ -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
29 benchmark/mini_server.js
@@ -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
34 examples/client.js
@@ -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
20 examples/worker.js
@@ -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
175 readme.md
@@ -67,6 +67,7 @@ var pool = new Pool(
, pingTimeout: 2000 // ping timeout in ms
, retryFilter: undefined // see below
, retryDelay: 20 // see below
+ , maxRetries: 5 // see below
, name: undefined // optional string
}
)
@@ -111,6 +112,11 @@ Math.random() * Math.pow(2, attemptNumber) * retryDelay
```
If `retryDelay` is 20, attemptNumber 1 (the first retry) will delay at most 40ms
+###### maxRetries
+
+The maximum number of attempts to make after the first request fails. This only
+takes effect if maxRetries < pool size.
+
###### ping
When an endpoint is unresponsive the pool will not use it for requests. The ping
@@ -200,171 +206,20 @@ Same arguments as `request` that sets `options.method = 'POST'`
Same arguments as `request` that sets `options.method = 'DELETE'`
----
-
-# Advanced
-
-## Make a pool
-```js
-var pool = new Poolee(http, servers, options)
-```
-
-`servers`: array of strings formatted like 'ip:port'
-
-`options`: defaults and explanations below
+### Events
-```js
-// options
-{
- // number of pending requests allowed
- maxPending: 1000
+##### timing
- // ping path. (default = no ping checks)
-, ping: null
-
-, retryFilter: function (options, response) {
- // return true to reject response and retry
- }
-
- // number in milliseconds
-, retryDelay: 20
-
- // optional string name
-, name: null
-}
-```
-
-### Events emitted by `pool`:
-```js
-pool.on('health', function(messageString) {
- // message string of of the form:
- // "127.0.0.1:8888 health: true"
- // or
- // "127.0.0.1:8888 health: false"
-})
+Emits the request `duration` and `options` after each request
-pool.on('timeout', function(url) {
- // where url is a ip+port+path combination for the timed-out request
-})
-
-pool.on('retrying', function(error) { })
-
-pool.on('timing', function(time, options) {
- // `time`: the time the latest request took to complete
- // `options`: options used to send the request
-})
-```
-
-
-### Get a healthy node
- var node = pool.get_node()
-
-Attached to `node`:
-
- // Counts of interest
- // node.pending;
- // node.successes
- // node.failures
- // node.requestRate
-
- // node.ip;
- // node.port;
- // node.name = node.ip + ':' + node.port;
-
-### Events emitted by `node`
-```js
-node.on('health', function(self) {
- // `self` has all the same properties as `node`
-})
-
-node.on('timeout', function (request) {
- // the request that timed out
-})
-```
+##### retrying
-### Example
+Emits the `error` of why a request is being retried
-Note that this example should fail, because there won't be any nodes running.
-You can also see this code in
-[`examples/`](https://github.com/dannycoates/poolee/tree/master/examples).
+##### timeout
-#### `client.js`
-```js
-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
+Emits the `request` when a request times out
- // healthiest node, populated later on
- , active_node = null
+##### health
-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)
- }
- }
-)
-```
-
-Run this before the above script, then see what happens.
-
-#### `worker.js`
-```js
-// 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)
-```
-
-To see a pool that is 100% healthy:
-
-```sh
-node ./worker.js 8080 &
-node ./worker.js 8081 &
-node ./worker.js 8082 &
-
-echo "running client.js ..."
-node ./client.js
-```
-
-## Running tests
-```sh
-npm -g install mocha
-mocha
-```
+Emits the `endpoint` when a node changes state between healthy/unhealthy
Please sign in to comment.
Something went wrong with that request. Please try again.