New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create inflightRequestThrottle plugin #1431
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
var assert = require('assert-plus'); | ||
var ServiceUnavailableError = require('restify-errors').ServiceUnavailableError; | ||
var defaultResponse = new ServiceUnavailableError('resource exhausted'); | ||
|
||
/** | ||
* inflightRequestThrottle | ||
* | ||
* Place an upper limit on the number of inlfight requests restify will accept. | ||
* For every request that exceeds this threshold, restify will respond with an | ||
* error. This plugin should be registered as early as possible in the | ||
* middleware stack using `pre` to avoid performing unnecessary work. | ||
* | ||
* @param {Object} opts configure this plugin | ||
* @param {Number} opts.limit maximum number of inflight requests the server | ||
* will handle before returning an error | ||
* @param {Error} opts.err A restify error used as a response when the inflight | ||
* request limit is exceeded | ||
* @param {Function} opts.server the instance of the restify server this plugin | ||
* will throttle. | ||
* @returns {Function} middleware to be registered on server.pre | ||
*/ | ||
function inflightRequestThrottle (opts) { | ||
|
||
// Scrub input and populate our configuration | ||
assert.object(opts, 'opts'); | ||
assert.number(opts.limit, 'opts.limit'); | ||
assert.object(opts.server, 'opts.server'); | ||
assert.func(opts.server.inflightRequests, 'opts.server.inflightRequests'); | ||
|
||
if (opts.err !== undefined && opts.err !== null) { | ||
assert.ok(opts.err instanceof Error, 'opts.res must be an error'); | ||
assert.optionalNumber(opts.err.statusCode, 'opts.err.statusCode'); | ||
} | ||
|
||
var self = {}; | ||
self._err = opts.err || defaultResponse; | ||
self._limit = opts.limit; | ||
self._server = opts.server; | ||
|
||
function onRequest (req, res, next) { | ||
var inflightRequests = self._server.inflightRequests(); | ||
|
||
if (inflightRequests > self._limit) { | ||
req.log.trace({ | ||
plugin: 'inflightRequestThrottle', | ||
inflightRequests: inflightRequests, | ||
limit: self._limit | ||
}, 'maximum inflight requests exceeded, rejecting request'); | ||
return res.send(self._err); | ||
} | ||
|
||
return next(); | ||
} | ||
|
||
// We need to bind in order to keep our `this` context when passed back | ||
// out of the constructor. | ||
return onRequest; | ||
} | ||
|
||
inflightRequestThrottle.prototype.onRequest = | ||
|
||
module.exports = inflightRequestThrottle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
'use strict'; | ||
|
||
var assert = require('chai').assert; | ||
var restify = require('../../lib/index.js'); | ||
var restifyClients = require('restify-clients'); | ||
var inflightRequestThrottle = restify.plugins.inflightRequestThrottle; | ||
|
||
function fakeServer(count) { | ||
return { | ||
inflightRequests: function () { | ||
return count; | ||
} | ||
}; | ||
} | ||
|
||
describe('inlfightRequestThrottle', function () { | ||
|
||
it('Unit: Should shed load', function (done) { | ||
var logged = false; | ||
var opts = { server: fakeServer(10), limit: 1 }; | ||
var plugin = inflightRequestThrottle(opts); | ||
function send (body) { | ||
assert(logged, 'Should have emitted a log'); | ||
assert.equal(body.statusCode, 503, 'Defaults to 503 status'); | ||
assert(body instanceof Error, 'Defaults to error body'); | ||
done(); | ||
} | ||
function next () { | ||
assert(false, 'Should not call next'); | ||
done(); | ||
} | ||
function trace () { | ||
logged = true; | ||
} | ||
var log = { trace: trace }; | ||
var fakeReq = { log: log }; | ||
plugin(fakeReq, { send: send }, next); | ||
}); | ||
|
||
it('Unit: Should support custom response', function (done) { | ||
var server = fakeServer(10); | ||
var err = new Error('foo'); | ||
var opts = { server: server, limit: 1, err: err }; | ||
var plugin = inflightRequestThrottle(opts); | ||
function send (body) { | ||
assert.equal(body, err, 'Overrides body'); | ||
done(); | ||
} | ||
function next () { | ||
assert(false, 'Should not call next'); | ||
done(); | ||
} | ||
var fakeReq = { log : { trace: function () {} } }; | ||
plugin(fakeReq, { send: send }, next); | ||
}); | ||
|
||
it('Unit: Should let request through when not under load', function (done) { | ||
var opts = { server: fakeServer(1), limit: 2 }; | ||
var plugin = inflightRequestThrottle(opts); | ||
function send () { | ||
assert(false, 'Should not call send'); | ||
done(); | ||
} | ||
function next () { | ||
assert(true, 'Should call next'); | ||
done(); | ||
} | ||
var fakeReq = { log : { trace: function () {} } }; | ||
plugin(fakeReq, { send: send }, next); | ||
}); | ||
|
||
it('Integration: Should shed load', function (done) { | ||
var server = restify.createServer(); | ||
var client = { | ||
close: function () {} | ||
}; | ||
var isDone = false; | ||
var to; | ||
function finish() { | ||
if (isDone) { | ||
return null; | ||
} | ||
clearTimeout(to); | ||
isDone = true; | ||
client.close(); | ||
server.close(); | ||
return done(); | ||
} | ||
to = setTimeout(finish, 2000); | ||
var err = new Error('foo'); | ||
err.statusCode = 555; | ||
var opts = { server: server, limit: 1, err: err }; | ||
server.pre(inflightRequestThrottle(opts)); | ||
var RES; | ||
server.get('/foo', function (req, res) { | ||
if (RES) { | ||
res.send(999); | ||
} else { | ||
RES = res; | ||
} | ||
}); | ||
server.listen(0, '127.0.0.1', function () { | ||
client = restifyClients.createJsonClient({ | ||
url: 'http://127.0.0.1:' + server.address().port, | ||
retry: false | ||
}); | ||
client.get({ path: '/foo' }, function (e, _, res) { | ||
assert(e === null || e === undefined, | ||
'First request isnt shed'); | ||
assert.equal(res.statusCode, 200, '200 returned on success'); | ||
finish(); | ||
}); | ||
client.get({ path: '/foo' }, function (e, _, res) { | ||
assert(e, 'Second request is shed'); | ||
assert.equal(e.name, | ||
'InternalServerError', 'Default err returned'); | ||
assert.equal(res.statusCode, 555, | ||
'Default shed status code returned'); | ||
RES.send(200); | ||
}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may want to specify both here and in docs that users should register this plugin first as order matters and you want the throttle check to occur as early as possible.