Skip to content

Commit

Permalink
Fix load limit condition. Closes #1536
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Mar 28, 2014
1 parent c25f190 commit 9de8cf0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ internals.Server.prototype._dispatch = function (options) {
// Check load

if (limits.sampleInterval &&
((limits.maxEventLoopDelay && (load.eventLoopDelay > limits.maxEventLoopDelay || self._loadBench.elapsed() - limits.sampleInterval > limits.maxEventLoopDelay)) ||
((limits.maxEventLoopDelay && (load.eventLoopDelay > limits.maxEventLoopDelay || self._loadBench.elapsed() > limits.maxEventLoopDelay)) ||
(limits.maxHeapUsedBytes && load.heapUsed > limits.maxHeapUsedBytes) ||
(limits.maxRssBytes && load.rss > limits.maxRssBytes))) {

Expand Down Expand Up @@ -373,8 +373,7 @@ internals.Server.prototype._stop = function (options, callback) {

Object.keys(self._connections).forEach(function (key) {

var connection = self._connections[key];
return connection && connection.destroy();
self._connections[key].destroy();
});
}, options.timeout);

Expand Down Expand Up @@ -457,7 +456,7 @@ internals.Server.prototype.inject = function (options, callback) {
delete res.raw.res._hapi;
}
else {
res.result = res.result || res.payload;
res.result = res.payload;
}

return callback(res);
Expand Down
42 changes: 42 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,38 @@ describe('Server', function () {
});
});

it('rejects request due to high event loop delay load before next sample', function (done) {

var server = new Hapi.Server(0, { load: { sampleInterval: 500, maxEventLoopDelay: 1 } });
var handler = function (request, reply) {

var start = Date.now();
while (Date.now() - start < 10);
reply('ok');
};

server.route({ method: 'GET', path: '/', handler: handler });
server.start(function (err) {

server.inject('/', function (res) {

expect(res.statusCode).to.equal(200);

setImmediate(function () {

server.inject('/', function (res) {

expect(res.statusCode).to.equal(503);
server.stop(function () {

done();
});
});
});
});
});
});

it('reuses the same cache segment', function (done) {

var server = new Hapi.Server({ cache: { engine: 'catbox-memory', shared: true } });
Expand Down Expand Up @@ -1041,6 +1073,16 @@ describe('Server', function () {
expect(server._stateDefinitions['steve']).deep.equal(Defaults.state);
done();
});

it('throws when missing name', function (done) {

var server = new Hapi.Server();
expect(function () {

server.state();
}).to.throw('Invalid name');
done();
});
});

describe('Timeouts', function () {
Expand Down

0 comments on commit 9de8cf0

Please sign in to comment.