Skip to content

Commit

Permalink
syncronous inflightRequest calc
Browse files Browse the repository at this point in the history
  • Loading branch information
retrohacker committed Mar 14, 2019
1 parent 7db83a5 commit 5b26d64
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions lib/server.js
Expand Up @@ -217,12 +217,14 @@ function Server(options) {
return;
}

self._inflightRequests++;
if (!options.noWriteContinue) {
res.writeContinue();
}

self.earliestChain.run(req, res, function earliestDone(ignore) {
if (ignore === false || Boolean(ignore)) {
self._inflightRequests--;
return;
}
self._onRequest(req, res);
Expand All @@ -231,10 +233,12 @@ function Server(options) {

if (options.handleUpgrades) {
this.server.on('upgrade', function onUpgrade(req, socket, head) {
self._inflightRequests++;
var res = upgrade.createResponse(req, socket, head);
req._upgradeRequest = true;
self.earliestChain.run(req, res, function earliestDone(ignore) {
if (ignore === false || Boolean(ignore)) {
self._inflightRequests--;
return;
}
self._onRequest(req, res);
Expand All @@ -243,8 +247,10 @@ function Server(options) {
}

this.server.on('request', function onRequest(req, res) {
self._inflightRequests++;
self.earliestChain.run(req, res, function earliestDone(ignore) {
if (ignore === false || Boolean(ignore)) {
self._inflightRequests--;
return;
}

Expand Down Expand Up @@ -513,17 +519,9 @@ Server.prototype.pre = function pre() {

// eslint-disable-next-line jsdoc/check-param-names
/**
* Gives you hooks that run before restify touches a request. These hooks
* allow you to do processing early in the request/response life cycle without
* the overhead of the restify framework. You can not yield the event loop in
* this handler.
*
* This is useful in cases where you want to make quick decisions about a
* request, for example when shedding load.
*
* The function handler accepts two parameters: req, res. If you want restify
* to ignore this request, return false from your handler. Any other return
* value results in restify handling the request.
* Gives you hooks to run _before_ restify touches the request. This gives you
* a chance to do processing early in the request/response lifecycle without the
* overhead of the restify framework.
*
* @public
* @memberof Server
Expand All @@ -534,17 +532,17 @@ Server.prototype.pre = function pre() {
* This gives you a hook to change request headers and the like if you need to.
* Note that `req.params` will be undefined, as that's filled in *after*
* routing.
* Takes a function, or an array of functions.
* variable number of nested arrays of handler functions
* Takes a function, or a ariable number of nested arrays of handler functions
* @returns {Object} returns self
* @example
* sever.earliest(function(req, res) {
* var error_response = new Error();
* sever.earliest(function(req, res, next) {
* if(server._inflightRequests > 100) {
* res.statusCode = 503;
* res.end();
* return false
* next(error_response);
* }
* return true;
* return next();
* })
*/
Server.prototype.earliest = function earliest() {
Expand Down Expand Up @@ -897,9 +895,6 @@ Server.prototype._onRequest = function _onRequest(req, res) {
// Decorate req and res objects
self._setupRequest(req, res);

// increment number of requests
self._inflightRequests++;

// Run in domain to catch async errors
// It has significant negative performance impact
// Warning: this feature depends on the deprecated domains module
Expand Down

0 comments on commit 5b26d64

Please sign in to comment.