diff --git a/doc/plugins.markdown b/doc/plugins.markdown index 5577309..1c2278b 100644 --- a/doc/plugins.markdown +++ b/doc/plugins.markdown @@ -122,6 +122,24 @@ Emitted when a retry has been scheduled. Possible reasons are `connect`, `codes` or `validate` and for reason `connect` the `response` is `null`. +**Example: modify request options** + +```js +var hosts = [ + 'ww1.example.com', + 'ww2.example.com', + 'ww3.example.com' +]; + +call.on('retry', function(options, response, reason) { + if (hosts.length > 0) { + options.request.host = hosts.shift(); + } else { + call.abort(); + } +}); +``` + [back to top](#table-of-contents) ## timeout diff --git a/lib/plugins/retry.js b/lib/plugins/retry.js index 3609812..61b988a 100644 --- a/lib/plugins/retry.js +++ b/lib/plugins/retry.js @@ -89,11 +89,9 @@ RetryPlugin.prototype._retry = function(call, options) { RetryPlugin.prototype._interceptError = function(call, options, err) { if (err.syscall === 'connect') { - // TODO: try to auto-correct the config - // TODO: alternate protocols, hosts and/or ports options.retry.limit = options.retry.limit - 1; - this._retry(call); call.emit('retry', options, null, 'connect', err.code); + this._retry(call, options); } else { call.__emit('error', err); } @@ -117,8 +115,8 @@ RetryPlugin.prototype._interceptResponse = function(call, options, response) { function finish() { retry.limit = retry.limit - 1; - self._retry(call); call.emit('retry', options, response, reason, code); + self._retry(call, options); } // check if buffer plugin already handled the response body diff --git a/test/test-http-retry.js b/test/test-http-retry.js index 606ba05..d5b4f3e 100644 --- a/test/test-http-retry.js +++ b/test/test-http-retry.js @@ -111,6 +111,49 @@ suite('http:retry', function() { }).end(); }); + + test('modify request', function(done) { + onrequest = function(request, response) { + var host = request.headers.host; + + if (host === '127.0.0.1:' + common.port) { + response.writeHead(503); + response.end(); + + } else if (host === 'localhost:' + common.port) { + response.writeHead(200); + response.end(); + } + }; + + var retries = 0; + + rail.call({ + port: common.port + }, function(response) { + assert.strictEqual(response.statusCode, 200); + assert.strictEqual(retries, 1); + + response.on('readable', function() { + response.read(); + }); + + response.on('end', function() { + done(); + }); + + }).on('retry', function(options, response, reason) { + ++retries; + + assert(options.retry); + assert.strictEqual(response.statusCode, 503); + assert.strictEqual(reason, 'codes'); + + options.request.host = 'localhost'; + }).end(); + }); + + suiteTeardown(function(done) { server.close(done); });