Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
retry: allow modification of options in retry listener
Browse files Browse the repository at this point in the history
  • Loading branch information
skenqbx committed Oct 2, 2015
1 parent a8fcbbd commit 0d97258
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
18 changes: 18 additions & 0 deletions doc/plugins.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions lib/plugins/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
Expand Down
43 changes: 43 additions & 0 deletions test/test-http-retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit 0d97258

Please sign in to comment.