Skip to content

Commit

Permalink
fix(inflightRequestThrottle): properly handle next (#1471)
Browse files Browse the repository at this point in the history
* Always call next
* Abort middleware chain by passing `false` when shedding load
* Fix error messages
* Remove accidental onRequest on prototype
  • Loading branch information
William Blankenship committed Sep 7, 2017
1 parent 9373cb5 commit 4db404f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
9 changes: 3 additions & 6 deletions lib/plugins/inflightRequestThrottle.js
Expand Up @@ -30,7 +30,7 @@ function inflightRequestThrottle (opts) {
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.ok(opts.err instanceof Error, 'opts.err must be an error');
assert.optionalNumber(opts.err.statusCode, 'opts.err.statusCode');
}

Expand All @@ -48,17 +48,14 @@ function inflightRequestThrottle (opts) {
inflightRequests: inflightRequests,
limit: self._limit
}, 'maximum inflight requests exceeded, rejecting request');
return res.send(self._err);
res.send(self._err);
return next(false);
}

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;
16 changes: 8 additions & 8 deletions test/plugins/inflightRequestThrottle.test.js
Expand Up @@ -25,9 +25,8 @@ describe('inlfightRequestThrottle', function () {
assert(body instanceof Error, 'Defaults to error body');
done();
}
function next () {
assert(false, 'Should not call next');
done();
function next (cont) {
assert.isFalse(cont, 'Should call next with false');
}
function trace () {
logged = true;
Expand All @@ -48,7 +47,6 @@ describe('inlfightRequestThrottle', function () {
}
function next () {
assert(false, 'Should not call next');
done();
}
var fakeReq = { log : { trace: function () {} } };
plugin(fakeReq, { send: send }, next);
Expand All @@ -59,10 +57,9 @@ describe('inlfightRequestThrottle', function () {
var plugin = inflightRequestThrottle(opts);
function send () {
assert(false, 'Should not call send');
done();
}
function next () {
assert(true, 'Should call next');
function next (cont) {
assert.isUndefined(cont, 'Should call next');
done();
}
var fakeReq = { log : { trace: function () {} } };
Expand Down Expand Up @@ -116,7 +113,10 @@ describe('inlfightRequestThrottle', function () {
'InternalServerError', 'Default err returned');
assert.equal(res.statusCode, 555,
'Default shed status code returned');
RES.send(200);

if (RES) {
RES.send(200);
}
});
});
});
Expand Down

0 comments on commit 4db404f

Please sign in to comment.