[
  {
    "path": ".gitignore",
    "content": "\n# Created by https://www.gitignore.io/api/node\n\n### Node ###\n# Logs\nlogs\n*.log\nnpm-debug.log*\nyarn-debug.log*\nyarn-error.log*\n\n# Runtime data\npids\n*.pid\n*.seed\n*.pid.lock\n\n# Directory for instrumented libs generated by jscoverage/JSCover\nlib-cov\n\n# Coverage directory used by tools like istanbul\ncoverage\n\n# nyc test coverage\n.nyc_output\n\n# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)\n.grunt\n\n# Bower dependency directory (https://bower.io/)\nbower_components\n\n# node-waf configuration\n.lock-wscript\n\n# Compiled binary addons (http://nodejs.org/api/addons.html)\nbuild/Release\n\n# Dependency directories\nnode_modules/\njspm_packages/\n\n# Typescript v1 declaration files\ntypings/\n\n# Optional npm cache directory\n.npm\n\n# Optional eslint cache\n.eslintcache\n\n# Optional REPL history\n.node_repl_history\n\n# Output of 'npm pack'\n*.tgz\n\n# Yarn Integrity file\n.yarn-integrity\n\n# dotenv environment variables file\n.env\n\n\n# End of https://www.gitignore.io/api/node\n"
  },
  {
    "path": ".travis.yml",
    "content": "language: node_js\nsudo: false\nnode_js:\n- '8'\n- '10'\n- '12'\nscript:\n- npm run ci\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2017 David Mark Clements\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n"
  },
  {
    "path": "benchmarks/express/excluded.js",
    "content": "'use strict'\n\nvar app = require('express')()\n\napp.get('/', function (req, res) {\n  res.send('content')\n})\n\nmodule.exports = app\n"
  },
  {
    "path": "benchmarks/express/included.js",
    "content": "'use strict'\n\nvar app = require('express')()\nvar protect = require('../..')('express')\n\napp.use(protect)\n\napp.get('/', function (req, res) {\n  res.send('content')\n})\n\nmodule.exports = app\n"
  },
  {
    "path": "benchmarks/express/index.js",
    "content": "'use strict'\nvar autocannon = require('autocannon')\n\nvar included = require('./included')\nvar excluded = require('./excluded')\n\nconsole.log('express with overload protection:')\nincluded = included.listen(3000)\nvar instance = autocannon({\n  url: 'http://localhost:3000',\n  connections: 10,\n  pipelining: 1,\n  duration: 10\n}, function () {\n  instance.stop()\n  included.close()\n  console.log('\\nexpress without overload protection:')\n  excluded = excluded.listen(3000)\n  instance = autocannon({\n    url: 'http://localhost:3000',\n    connections: 10,\n    pipelining: 1,\n    duration: 10\n  }, function () {\n    instance.stop()\n    excluded.close()\n  })\n  autocannon.track(instance, {renderProgressBar: true})\n})\n\n// just render results\nautocannon.track(instance, {renderProgressBar: true})\n"
  },
  {
    "path": "benchmarks/http/excluded.js",
    "content": "'use strict'\n\nvar http = require('http')\nvar server = http.createServer(serve)\n\nfunction serve (req, res) {\n  res.end('content')\n}\n\nmodule.exports = server\n"
  },
  {
    "path": "benchmarks/http/included.js",
    "content": "'use strict'\n\nvar http = require('http')\nvar server = http.createServer(serve)\nvar protect = require('../..')('http')\n\nfunction serve (req, res) {\n  if (protect(req, res) === true) return\n  res.end('content')\n}\n\nmodule.exports = server\n"
  },
  {
    "path": "benchmarks/http/index.js",
    "content": "'use strict'\nvar autocannon = require('autocannon')\n\nvar included = require('./included')\nvar excluded = require('./excluded')\n\nconsole.log('http with overload protection:')\nincluded.listen(3000)\nvar instance = autocannon({\n  url: 'http://localhost:3000',\n  connections: 10,\n  pipelining: 1,\n  duration: 10\n}, function () {\n  instance.stop()\n  included.close()\n  console.log('\\nhttp without overload protection:')\n  excluded.listen(3000)\n  instance = autocannon({\n    url: 'http://localhost:3000',\n    connections: 10,\n    pipelining: 1,\n    duration: 10\n  }, function () {\n    instance.stop()\n    excluded.close()\n  })\n  autocannon.track(instance, {renderProgressBar: true})\n})\n\n// just render results\nautocannon.track(instance, {renderProgressBar: true})\n"
  },
  {
    "path": "benchmarks/koa/excluded.js",
    "content": "'use strict'\n\nvar Koa = require('koa')\nvar Router = require('koa-router')\n\nvar router = new Router()\nvar app = new Koa()\n\nrouter.get('/', async function (ctx) {\n  ctx.body = 'content'\n})\n\napp.use(router.routes())\n\nmodule.exports = app\n"
  },
  {
    "path": "benchmarks/koa/included.js",
    "content": "'use strict'\n\nvar Koa = require('koa')\nvar Router = require('koa-router')\nvar protect = require('../..')('koa')\n\nvar router = new Router()\nvar app = new Koa()\n\napp.use(protect)\n\nrouter.get('/', async function (ctx) {\n  ctx.body = 'content'\n})\n\napp.use(router.routes())\n\nmodule.exports = app\n"
  },
  {
    "path": "benchmarks/koa/index.js",
    "content": "'use strict'\nvar autocannon = require('autocannon')\n\nvar included = require('./included')\nvar excluded = require('./excluded')\n\nconsole.log('koa with overload protection:')\nincluded = included.listen(3000)\nvar instance = autocannon({\n  url: 'http://localhost:3000',\n  connections: 10,\n  pipelining: 1,\n  duration: 10\n}, function () {\n  instance.stop()\n  included.close()\n  console.log('\\nkoa without overload protection:')\n  excluded = excluded.listen(3000)\n  instance = autocannon({\n    url: 'http://localhost:3000',\n    connections: 10,\n    pipelining: 1,\n    duration: 10\n  }, function () {\n    instance.stop()\n    excluded.close()\n  })\n  autocannon.track(instance, {renderProgressBar: true})\n})\n\n// just render results\nautocannon.track(instance, {renderProgressBar: true})\n"
  },
  {
    "path": "benchmarks/restify/excluded.js",
    "content": "'use strict'\n\nvar app = require('restify').createServer({\n  name: 'myapp',\n  version: '1.0.0'\n})\n\napp.get('/', function (req, res) {\n  res.send('content')\n})\n\nmodule.exports = app\n"
  },
  {
    "path": "benchmarks/restify/included.js",
    "content": "'use strict'\n\nvar app = require('restify').createServer({\n  name: 'myapp',\n  version: '1.0.0'\n})\nvar protect = require('../..')('restify')\n\napp.use(protect)\n\napp.get('/', function (req, res) {\n  res.send('content')\n})\n\nmodule.exports = app\n"
  },
  {
    "path": "benchmarks/restify/index.js",
    "content": "'use strict'\nvar autocannon = require('autocannon')\n\nvar included = require('./included')\nvar excluded = require('./excluded')\n\nconsole.log('restify with overload protection:')\nincluded = included.listen(3000)\nvar instance = autocannon({\n  url: 'http://localhost:3000',\n  connections: 10,\n  pipelining: 1,\n  duration: 10\n}, function () {\n  instance.stop()\n  included.close()\n  console.log('\\nrestify without overload protection:')\n  excluded = excluded.listen(3000)\n  instance = autocannon({\n    url: 'http://localhost:3000',\n    connections: 10,\n    pipelining: 1,\n    duration: 10\n  }, function () {\n    instance.stop()\n    excluded.close()\n  })\n  autocannon.track(instance, {renderProgressBar: true})\n})\n\n// just render results\nautocannon.track(instance, {renderProgressBar: true})\n"
  },
  {
    "path": "demos/express/index.js",
    "content": "'use strict'\n\nvar app = require('express')()\nvar protect = require('../..')('express')\n\napp.use(protect)\n\napp.get('/', function (req, res) {\n  res.send('content')\n})\n\napp.listen(3000, function () {\n  var req = require('http').get('http://localhost:3000')\n\n  req.on('response', function (res) {\n    console.log('got status code', res.statusCode)\n    console.log('retry after', res.headers['retry-after'])\n\n    setTimeout(function () {\n      console.log('protect.overload after load', protect.overload)\n      var req = require('http').get('http://localhost:3000')\n\n      req.on('response', function (res) {\n        console.log('got status code', res.statusCode)\n\n        protect.stop()\n        process.exit()\n      }).end()\n    }, parseInt(res.headers['retry-after'], 10))\n  }).end()\n\n  setImmediate(function () {\n    console.log('eventLoopDelay after active sleeping', protect.eventLoopDelay)\n  })\n\n  sleep(500)\n})\n\nfunction sleep (msec) {\n  var start = Date.now()\n  while (Date.now() - start < msec) {}\n}\n"
  },
  {
    "path": "demos/http/index.js",
    "content": "'use strict'\n\nvar http = require('http')\nvar server = http.createServer(serve)\nvar protect = require('../..')('http')\n\nfunction sleep (msec) {\n  var start = Date.now()\n  while (Date.now() - start < msec) {}\n}\n\nfunction serve (req, res) {\n  if (protect(req, res) === true) return\n  res.end('content')\n}\n\nserver.listen(0, function () {\n  var req = http.get(server.address())\n\n  req.on('response', function (res) {\n    console.log('got status code', res.statusCode)\n    console.log('retry after', res.headers['retry-after'])\n\n    setTimeout(function () {\n      console.log('protect.overload after load', protect.overload)\n      var req = http.get(server.address())\n\n      req.on('response', function (res) {\n        console.log('got status code', res.statusCode)\n\n        protect.stop()\n        server.close()\n      }).end()\n    }, parseInt(res.headers['retry-after'], 10))\n  }).end()\n\n  setImmediate(function () {\n    console.log('eventLoopDelay after active sleeping', protect.eventLoopDelay)\n  })\n\n  sleep(500)\n})\n"
  },
  {
    "path": "demos/koa/index.js",
    "content": "'use strict'\n\nvar Koa = require('koa')\nvar Router = require('koa-router')\nvar protect = require('../..')('koa')\n\nvar router = new Router()\nvar app = new Koa()\n\napp.use(protect)\n\nrouter.get('/', async function (ctx) {\n  ctx.body = 'content'\n})\n\napp.use(router.routes())\napp.listen(3000, function () {\n  var req = require('http').get('http://localhost:3000')\n\n  req.on('response', function (res) {\n    console.log('got status code', res.statusCode)\n    console.log('retry after', res.headers['retry-after'])\n\n    setTimeout(function () {\n      console.log('protect.overload after load', protect.overload)\n      var req = require('http').get('http://localhost:3000')\n\n      req.on('response', function (res) {\n        console.log('got status code', res.statusCode)\n\n        protect.stop()\n        process.exit()\n      }).end()\n    }, parseInt(res.headers['retry-after'], 10))\n  }).end()\n\n  setImmediate(function () {\n    console.log('eventLoopDelay after active sleeping', protect.eventLoopDelay)\n  })\n\n  sleep(500)\n})\n\nfunction sleep (msec) {\n  var start = Date.now()\n  while (Date.now() - start < msec) {}\n}\n"
  },
  {
    "path": "demos/restify/index.js",
    "content": "'use strict'\n\nvar app = require('restify').createServer({\n  name: 'myapp',\n  version: '1.0.0'\n})\nvar protect = require('../..')('restify')\n\napp.use(protect)\n\napp.get('/', function (req, res) {\n  res.send('content')\n})\n\napp.listen(3000, function () {\n  var req = require('http').get('http://localhost:3000')\n\n  req.on('response', function (res) {\n    console.log('got status code', res.statusCode)\n    console.log('retry after', res.headers['retry-after'])\n\n    setTimeout(function () {\n      console.log('protect.overload after load', protect.overload)\n      var req = require('http').get('http://localhost:3000')\n\n      req.on('response', function (res) {\n        console.log('got status code', res.statusCode)\n\n        protect.stop()\n        process.exit()\n      }).end()\n    }, parseInt(res.headers['retry-after'], 10))\n  }).end()\n\n  setImmediate(function () {\n    console.log('eventLoopDelay after active sleeping', protect.eventLoopDelay)\n  })\n\n  sleep(500)\n})\n\nfunction sleep (msec) {\n  var start = Date.now()\n  while (Date.now() - start < msec) {}\n}\n"
  },
  {
    "path": "index.js",
    "content": "'use strict'\n\nvar loopbench = require('loopbench')\n\nvar frameworks = {\n  express: require('./lib/express'),\n  http: require('./lib/http'),\n  koa: require('./lib/koa'),\n  restify: require('./lib/restify')\n}\n\nvar defaults = {\n  production: process.env.NODE_ENV === 'production',\n  errorPropagationMode: false,\n  clientRetrySecs: 1,\n  sampleInterval: 5,\n  maxEventLoopDelay: 42,\n  maxHeapUsedBytes: 0,\n  maxRssBytes: 0,\n  logging: false,\n  logStatsOnReq: false\n}\n\nfunction protect (framework, opts) {\n  opts = Object.assign({}, defaults, opts)\n  if (typeof framework === 'undefined') {\n    throw Error('Please specify a framework')\n  }\n  if (!(framework in frameworks)) {\n    throw Error(opts.integrate + ' not supported.')\n  }\n  if (opts.maxRssBytes <= 0 && opts.maxHeapUsedBytes <= 0 && opts.eventLoopDelay <= 0) {\n    throw Error('At least one threshold (eventLoopDelay, maxHeapUsedBytes, maxRssBytes) should be above 0')\n  }\n  if (opts.logStatsOnReq && opts.logging === false) {\n    throw Error('logStatsOnReq cannot be enabled unless logging is also enabled')\n  }\n  var update = (opts.maxEventLoopDelay > 0)\n    ? function update () {\n      profiler.eventLoopOverload = eventLoopProfiler.overLimit\n      profiler.eventLoopDelay = eventLoopProfiler.delay\n      profiler.overload = profiler.eventLoopOverload ||\n          profiler.heapUsedOverload ||\n          profiler.rssOverload\n    }\n    : function update () {\n      profiler.overload = profiler.heapUsedOverload || profiler.rssOverload\n    }\n\n  if (opts.maxEventLoopDelay > 0) {\n    var eventLoopProfiler = loopbench({\n      sampleInterval: opts.sampleInterval,\n      limit: opts.maxEventLoopDelay\n    })\n\n    eventLoopProfiler.on('load', update)\n    eventLoopProfiler.on('unload', update)\n  }\n\n  var maxHeapUsedBytes = opts.maxHeapUsedBytes\n  var maxRssBytes = opts.maxRssBytes\n\n  var timer = (maxHeapUsedBytes > 0 || maxRssBytes > 0) &&\n    setInterval(checkMemory, opts.sampleInterval).unref()\n\n  var profiler = {\n    overload: false,\n    eventLoopOverload: false,\n    heapUsedOverload: false,\n    rssOverload: false,\n    eventLoopDelay: 0,\n    maxEventLoopDelay: opts.maxEventLoopDelay,\n    maxHeapUsedBytes: opts.maxHeapUsedBytes,\n    maxRssBytes: opts.maxRssBytes,\n    stop: stop\n  }\n\n  var integrate = frameworks[framework](opts, profiler)\n  if (Object.setPrototypeOf) {\n    Object.setPrototypeOf(profiler, Function.prototype)\n    Object.setPrototypeOf(integrate, profiler)\n  } else {\n    profiler.__proto__ = Function.prototype // eslint-disable-line\n    integrate.__proto__ = profiler // eslint-disable-line\n  }\n\n  return integrate\n\n  function checkMemory () {\n    var mem = process.memoryUsage()\n    var heapUsed = mem.heapUsed\n    var rss = mem.rss\n    profiler.heapUsedOverload = (maxHeapUsedBytes > 0 && heapUsed > maxHeapUsedBytes)\n    profiler.rssOverload = (maxRssBytes > 0 && rss > maxRssBytes)\n    update()\n  }\n\n  function stop () {\n    if (eventLoopProfiler) eventLoopProfiler.stop()\n    clearInterval(timer)\n  }\n}\n\nmodule.exports = protect\n"
  },
  {
    "path": "lib/explain.js",
    "content": "'use strict'\n\nmodule.exports = explain\n\nfunction explain (protect) {\n  var explanation = 'Server experiencing heavy load: ('\n  var c = 0\n  if (protect.eventLoopOverload === true) {\n    c += 1\n    explanation += 'event loop'\n  }\n  if (protect.heapUsedOverload === true) {\n    explanation += c > 0 ? ', heap' : 'heap'\n    c += 1\n  }\n  if (protect.rssOverload === true) {\n    explanation += c > 0 ? ', rss' : 'rss'\n  }\n\n  explanation += ')'\n\n  return explanation\n}\n"
  },
  {
    "path": "lib/express.js",
    "content": "'use strict'\nmodule.exports = require('./http')\n"
  },
  {
    "path": "lib/http.js",
    "content": "'use strict'\n\nvar explain = require('./explain')\nvar OverloadProtectionStats = require('./stats')\n\nmodule.exports = http\n\nfunction http (opts, protect) {\n  var clientRetrySecs = opts.clientRetrySecs\n  var sendRetryHeader = clientRetrySecs > 0\n  var logStatsOnReq = opts.logStatsOnReq\n  var logging = opts.logging\n  var loggingOn = typeof logging === 'string' || typeof logging === 'function'\n  var log4jLogging = typeof logging === 'string'\n  var errorPropagationMode = opts.errorPropagationMode\n  var production = opts.production\n  var expose = !production\n\n  return overloadProtection\n\n  function overloadProtection (req, res, next) {\n    if (logStatsOnReq) {\n      const stats = new OverloadProtectionStats(\n        protect.overload,\n        protect.eventLoopOverload,\n        protect.heapUsedOverload,\n        protect.rssOverload,\n        protect.eventLoopDelay,\n        protect.maxEventLoopDelay,\n        protect.maxHeapUsedBytes,\n        protect.maxRssBytes\n      )\n      if (log4jLogging) req.log && req.log[logging] && req.log[logging](stats)\n      else logging(stats)\n    }\n    if (protect.overload === true) {\n      res.statusCode = 503\n      if (sendRetryHeader) res.setHeader('Retry-After', clientRetrySecs)\n      if (loggingOn) {\n        if (log4jLogging) req.log && req.log[logging] && req.log[logging](explain(protect))\n        else logging(explain(protect))\n      }\n      if (errorPropagationMode && typeof next === 'function') {\n        var err = Error(explain(protect))\n        err.expose = expose\n        err.statusCode = 503\n        next(err)\n        return\n      }\n\n      res.end(production ? 'Service Unavailable' : explain(protect))\n\n      if (arguments.length < 3) return true\n      else return\n    }\n    if (arguments.length >= 3 && typeof next === 'function') next()\n    else return false\n  }\n}\n"
  },
  {
    "path": "lib/koa.js",
    "content": "'use strict'\nmodule.exports = koa\n\nvar explain = require('./explain')\nvar OverloadProtectionStats = require('./stats')\n\nfunction koa (opts, protect) {\n  var clientRetrySecs = opts.clientRetrySecs\n  var sendRetryHeader = clientRetrySecs > 0\n  var logStatsOnReq = opts.logStatsOnReq\n  var logging = opts.logging\n  var loggingOn = typeof logging === 'string' || typeof logging === 'function'\n  var log4jLogging = typeof logging === 'string'\n  var errorPropagationMode = opts.errorPropagationMode\n  var production = opts.production\n  var expose = !production\n\n  return overloadProtection\n\n  function overloadProtection (ctx, next) {\n    if (logStatsOnReq) {\n      const stats = new OverloadProtectionStats(\n        protect.overload,\n        protect.eventLoopOverload,\n        protect.heapUsedOverload,\n        protect.rssOverload,\n        protect.eventLoopDelay,\n        protect.maxEventLoopDelay,\n        protect.maxHeapUsedBytes,\n        protect.maxRssBytes\n      )\n      if (log4jLogging) ctx.log && ctx.log[logging] && ctx.log[logging](stats)\n      else logging(stats)\n    }\n    if (protect.overload === true) {\n      if (sendRetryHeader) ctx.set('Retry-After', clientRetrySecs)\n      if (loggingOn) {\n        if (log4jLogging) ctx.log && ctx.log[logging] && ctx.log[logging](explain(protect))\n        else logging(explain(protect))\n      }\n      if (errorPropagationMode) {\n        var err = Error(explain(protect))\n        err.status = 503\n        err.expose = expose\n\n        // if exposing to client in dev,\n        // we also want to output\n        // the error in console\n        if (err.expose) {\n          ctx.app.emit('error', Error(explain(protect)), ctx)\n        }\n\n        throw err\n      }\n      ctx.status = 503\n\n      ctx.res.end(production ? 'Service Unavailable' : explain(protect))\n      return\n    }\n    return next()\n  }\n}\n"
  },
  {
    "path": "lib/restify.js",
    "content": "'use strict'\nmodule.exports = require('./http')\n"
  },
  {
    "path": "lib/stats.js",
    "content": "function OverloadProtectionStats (overload, eventLoopOverload, heapUsedOverload, rssOverload, eventLoopDelay, maxEventLoopDelay, maxHeapUsedBytes, maxRssBytes) {\n  this.overload = overload\n  this.eventLoopOverload = eventLoopOverload\n  this.heapUsedOverload = heapUsedOverload\n  this.rssOverload = rssOverload\n  this.eventLoopDelay = eventLoopDelay\n  this.maxEventLoopDelay = maxEventLoopDelay\n  this.maxHeapUsedBytes = maxHeapUsedBytes\n  this.maxRssBytes = maxRssBytes\n}\n\nmodule.exports = OverloadProtectionStats\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"overload-protection\",\n  \"version\": \"1.2.3\",\n  \"main\": \"index.js\",\n  \"scripts\": {\n    \"test\": \"tap test\",\n    \"lint\": \"standard\",\n    \"ci\": \"npm run lint && npm run cov\",\n    \"cov\": \"tap --cov test\",\n    \"covr\": \"tap --coverage-report=html test\",\n    \"benchmarks\": \"ls benchmarks | while read l; do node benchmarks/$l; done\"\n  },\n  \"pre-commit\": [\n    \"test\",\n    \"lint\"\n  ],\n  \"keywords\": [\n    \"load shedding\",\n    \"overload protection\",\n    \"production monitoring\",\n    \"503\",\n    \"Service Unavailable\",\n    \"Server Unavailable\",\n    \"HTTP 503\",\n    \"heavy load\",\n    \"load\",\n    \"protection\",\n    \"shedding\",\n    \"express\",\n    \"fastify\",\n    \"http\",\n    \"koa\",\n    \"restify\"\n  ],\n  \"author\": \"David Mark Clements <huperekchuno@googlemail.com>\",\n  \"license\": \"MIT\",\n  \"devDependencies\": {\n    \"autocannon\": \"^4.4.1\",\n    \"express\": \"^4.17.1\",\n    \"koa\": \"^2.11.0\",\n    \"koa-router\": \"^7.4.0\",\n    \"pre-commit\": \"^1.2.2\",\n    \"restify\": \"^8.5.1\",\n    \"standard\": \"^10.0.3\",\n    \"tap\": \"^10.7.2\"\n  },\n  \"dependencies\": {\n    \"loopbench\": \"^1.2.0\"\n  },\n  \"directories\": {\n    \"lib\": \"lib\",\n    \"test\": \"test\"\n  },\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"git+https://github.com/davidmarkclements/overload-protection.git\"\n  },\n  \"bugs\": {\n    \"url\": \"https://github.com/davidmarkclements/overload-protection/issues\"\n  },\n  \"homepage\": \"https://github.com/davidmarkclements/overload-protection#readme\",\n  \"description\": \"Load detection and shedding capabilities for http, express, restify and koa\"\n}\n"
  },
  {
    "path": "readme.md",
    "content": "# overload-protection \n\nLoad detection and shedding capabilities for http, express, restify, and koa\n\n[![Build Status](https://travis-ci.org/davidmarkclements/overload-protection.svg?branch=master)](https://travis-ci.org/davidmarkclements/overload-protection)\n[![Coverage Status](https://coveralls.io/repos/github/davidmarkclements/overload-protection/badge.svg)](https://coveralls.io/github/davidmarkclements/overload-protection)\n[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\n\n## About\n\n`overload-protection` provides integration for your framework of choice.\n\nIf a threshold is crossed for a given metric, `overload-protection` \nwill send an HTTP 503 Service Unavailable response, with (by default) \na `Retry-After` header, instructing the client (e.g. a browser or load balancer) to \nretry after a given amount of seconds.\n\nCurrent supported metrics are:\n\n* event loop delay (is the JavaScript thread blocking too long)\n* Used Heap Memory \n* Total Resident Set Size\n\nFor a great explanation of Used Heap Memory vs Resident Set Size see \nDaniel Khans article at <https://www.dynatrace.com/blog/understanding-garbage-collection-and-hunting-memory-leaks-in-node-js>   \n\n## Usage\n\nCreate a config object for your thresholds (and other `overload-protection`)\noptions.\n\n```js\nconst protectCfg = {\n  production: process.env.NODE_ENV === 'production', // if production is false, detailed error messages are exposed to the client\n  clientRetrySecs: 1, // Retry-After header, in seconds (0 to disable) [default 1]\n  sampleInterval: 5, // sample rate, milliseconds [default 5]\n  maxEventLoopDelay: 42, // maximum detected delay between event loop ticks [default 42]\n  maxHeapUsedBytes: 0, // maximum heap used threshold (0 to disable) [default 0]\n  maxRssBytes: 0, // maximum rss size threshold (0 to disable) [default 0]\n  errorPropagationMode: false, // dictate behavior: take over the response \n                              // or propagate an error to the framework [default false]\n  logging: false, // set to string for log level or function to pass data to\n  logStatsOnReq: false // set to true to log stats on every requests\n}\n```\n\nThen pass the framework we're integrating with along with the configuration object.\n\nFor instance with Express we would do:\n\n```js\nconst app = require('express')()\nconst protect = require('overload-protection')('express', protectCfg)\napp.use(protect)\n```\n\nWith middleware based frameworks, always put the `overload-protection` middleware\nfirst. In default mode this means `overload-protection` will take over the response\nand prevent any other middleware from executing (thus taking further potential pressure off\nof the process).\n\nRestify, and Koa all work in much the same way, call the `overload-protection`\nmodule with the name of the framework, a config object and pass the resulting `protect`\ninstance to `app.use` – e.g. Koa would be:\n\n```js\nconst Koa = require('koa')\nconst protect = require('overload-protection')('koa', protectCfg)\nconst app = new Koa()\napp.use(protect)\n```\n\nFor pure core HTTP the `overload-protection` instance can be called\nat the top of the request handler function. With two arguments (just `req` and `res`)\nthe function will return `true` if protection/shedding has been provided, or `false`\nif not. If `overload-protection` *has* taken over (the `true` case), then we should\nexit the function and do no further work:\n\n```js\nconst http = require('http')\nconst protect = require('overload-protection')('http', protectCfg)\n\nhttp.createServer(function (req, res) {\n  if (protect(req, res) === true) return\n  res.end('content')\n})\n```\n\nWith three arguments (the third argument being a callback), the rest of the \nwork should be done within the supplied callback.\n\n```js\nconst http = require('http')\nconst protect = require('overload-protection')('http', protectCfg)\n\nhttp.createServer(function (req, res) {\n  protect(req, res, function () {\n    // when errorPropagationMode mode is false, will *only* \n    // be called if load shedding didn't occur\n    // (if it was true we'd need to check for an Error object as first arg)\n    res.end('content')\n  })\n})\n```\n\n## Installation\n\n```sh\nnpm install overload-protection --save\n```\n\n## Tests\n\n```sh\nnpm install\nnpm test\n```\n\n## Benchmark\n\nThe overhead of using `overload-protection` is minimal, run the benchmarks to conduct \ncomparative profiling of using `overload-protection` versus not using it for each supported framework.  \n\n```sh\nnpm run benchmarks\n```\n\n## API\n\n### require('overload-protection') => (framework, opts) => instance\n\nThe `framework` argument is non-optional. It's a string and may be one of:\n\n* express\n* koa\n* restify\n* http\n\nThe `opts` argument is optional, as are all properties.\n\nOptions (particularly thresholds) are quite sensitive and highly relevant on \na case by case basis. Possible options are as follows:\n\n#### production: process.env.NODE_ENV === 'production'\n\nThe `production` option determines whether the client receives an error message \ndetailing the surpassed threshold(s). (It may also be used in future for other such\ngood practices or performance trade-offs). \n\n#### clientRetrySecs: 1\n\nBy default, `overload-protection` will add a header to the 503 response\ncalled `Retry-After`. It's up to the client to honour this header, which\ninstructs the client on how many seconds to wait between retries. \nDefaults to 1 seconds.\n\n#### sampleInterval: 5\n\nIn order to establish whether a threshold has been crossed, the metrics \nare sampled at a regular interval. The interval defaults to 5 milliseconds.\n\n####  maxEventLoopDelay: 42\n\nSynchronous work causes the event loop to freeze, when this happens \nan interval timer (which is our sampler) will be delayed by the amount\nof time the event loop was stalled for while the thread processed synchronous \nwork. We can measure this with timestamp comparison. This option sets a threshold\nfor the maximum amount of stalling between intervals we'll accept before our\nservice begins responding with 503 codes to requests. Defaults to 42 milliseconds.\n\nWhen set to 0 this threshold will be disabled. \n\n#### maxHeapUsedBytes: 0\n\nDisabled by default (set to 0), this defines maximum V8 (Node's JavaScript engine) used heap size.\n\nIf the Used Heap size exceeds the threshold the server will begin return 503 error codes\nuntil it crosses back under the threshold. \n\nSee <https://www.dynatrace.com/blog/understanding-garbage-collection-and-hunting-memory-leaks-in-node-js>\nfor more info on Used Heap from a V8 context.\n\n#### maxRssBytes: 0\n\nDisabled by default (set to 0) maximum process Resident Set Size. If\nthe RSS exceeds the threshold the server will begin return 503 error codes\nuntil it crosses back under the threshold.\n\n#### errorPropagationMode: false\n\n**This is relevant to middleware integration only**\n\nBy default, `overload-protection` will handle and end the response, \nwithout calling any subsequent configured middleware. The point here \nis to avoid any further processing for an already (by definition) \nover loaded process.\n\nHowever, it could be argued, from a puritanical perspective, that middleware\nshould defer to the framework and that any HTTP code of 500 or above should \nbe generated by propagating an error through the framework. \n\nThis option prevents `overload-protection` from manually ended the response and\ninstead generates an `Error` object (with additional properties as per [`http-errors`](https://github.com/jshttp/http-errors) as used by Express and Koa)     \nand propagates it through the framework (either by throwing it in Koa, or passing through the `next` callback).\n\n#### logging: false\n\nThe `logging` option can be set to a string or a function. \n\nIf `logging` is set to a string, the string should indicate the desired log \nlevel for notifying that a 503 response was given. When `logging` is a string\na request bound Log4j-style logger is assumed. This means the `req` object (or the `ctx` object in the case of Koa) \nshould have a `log` object which contains methods corresponding to log levels. So if `logging`\nwas set to `warn` (`logging: 'warn'`) then `req.log.warn` is expected to be present\nand be a function. A number of logging libraries follow this pattern, such as \n[`bunyan-express`](http:/npm.im/bunyan-express) and all of the [`pino`](http://npm.im/pino) \nmiddleware loggers ([`express-pino-logger`](http://npm.im/express), [`koa-pino-logger`](http://npm.im/koa-pino-logger), \n[`restify-pino-logger`](http://npm.im/restify-pino-logger), [`pino-http`](http://npm.im/pino-http)).\n\nIf the application isn't using a request bound Log4j-style logger, the `logging` \noption can be set to a function which receives a log message. This function is \nthen responsible for writing the log. We could also simply set it to one of\nthe console methods, e.g. `logging: console.warn`. \n\nThis is primarily for usage when `errorPropagationMode` is `false`. If `errorPropagationMode` \nis set to `true`, we may want to instead log once the error has propagated to a handler.    \n\n#### logStatsOnReq: false\n\nSet `logStatsOnReq` to `true` log the profiled stats on every request. In order to use this option, the `logging` option must not be `false`. Bear in mind that using this option will\nadd extra pressure on the event loop in itself, so use with caution.\n\n### instance.overload\n\nThe returned instance (which in many cases is passed as middleware to `app.use`), \nhas an `overload` property. This begins as `false`. If any of the thresholds have \nbeen passed this will be set to `true`. Once all metrics are below their thresholds\nthis would become `false` again.\n\nThis allows for any heavy load detection required outside of a framework. \n\n### instance.eventLoopOverload\n\nThe returned instance (which in many cases is passed as middleware to `app.use`), \nhas an `eventLoopOverload` property. This begins as `false`. If the `maxEventLoopDelay`\nthreshold is passed this will be set to `true`. Once it's below the configured threshold\nthis would become `false` again.\n\nThis allows for any event loop delay detection necessary outside of a framework.\n\n### instance.heapUsedOverload\n\nThe returned instance (which in many cases is passed as middleware to `app.use`), \nhas a `heapUsedOverload` property. This begins as `false`. If the `maxHeapUsedBytes`\nthreshold is passed this will be set to `true`. Once it's below the configured threshold\nthis would become `false` again.\n\nThis allows for any heap used threshold detection necessary outside of a framework.\n\n### instance.rssOverload\n\nThe returned instance (which in many cases is passed as middleware to `app.use`), \nhas a `rssOverload` property. This begins as `false`. If the `maxRssBytes`\nthreshold is passed this will be set to `true`. Once it's below the configured threshold\nthis would become `false` again.\n\nThis allows for any heap used threshold detection necessary outside of a framework.\n\n### instance.eventLoopDelay\n\nThe delay in milliseconds (with additional decimal precision) since the last sample.\n\nIf `maxEventLoopDelay` is 0, the event loop is not measured, so `eventLoopDelay` will always\nbe 0 in that case.\n\n### instance.maxEventLoopDelay\n\nCorresponds to the `opts.maxEventLoopDelay` option.\n\n### instance.maxHeapUsedBytes\n\nCorresponds to the `opts.maxHeapUsedBytes` option.\n\n### instance.maxRssBytes\n\nCorresponds to the `opts.maxRssBytes` option.\n\n## Dependencies\n\n- [loopbench](https://github.com/mcollina/loopbench): Benchmark your event loop\n\n## Dev Dependencies\n\n- [autocannon](https://github.com/mcollina/autocannon): Fast HTTP benchmarking tool written in Node.js\n- [express](https://github.com/expressjs/express): Fast, unopinionated, minimalist web framework\n- [koa](https://github.com/koajs/koa): Koa web app framework\n- [koa-router](https://github.com/alexmingoia/koa-router): Router middleware for koa. Provides RESTful resource routing.\n- [pre-commit](https://github.com/observing/pre-commit): Automatically install pre-commit hooks for your npm modules.\n- [restify](https://github.com/restify/node-restify): REST framework\n- [standard](https://github.com/standard/standard): JavaScript Standard Style\n- [tap](https://github.com/tapjs/node-tap): A Test-Anything-Protocol library\n\n## License\n\nMIT\n\n## Acknowledgements\n\nKindly sponsored by [nearForm](http://nearform.com)\n"
  },
  {
    "path": "test/index.js",
    "content": "'use strict'\nvar test = require('tap').test\nvar protect = require('../')\n\ntest('throws if framework is unspecified', function (t) {\n  t.throws(function () {\n    protect()\n  })\n  t.end()\n})\n\ntest('throws if framework is not supported', function (t) {\n  t.throws(function () {\n    protect('not a thing')\n  })\n  t.end()\n})\n\ntest('throws if all thresholds are disabled (set to 0)', function (t) {\n  t.throws(function () {\n    protect('http', {\n      eventLoopDelay: 0,\n      maxRssBytes: 0,\n      maxHeapUsedBytes: 0\n    })\n  })\n  t.end()\n})\n\ntest('throws if logStatsOnReq is true but logging is false', function (t) {\n  t.throws(function () {\n    protect('http', {\n      logStatsOnReq: true\n    })\n  })\n  t.end()\n})\n\ntest('instance.stop ceases sampling', function (t) {\n  var sI = global.setInterval\n  var cI = global.clearInterval\n  var mock = {unref: function () { return mock }}\n  global.setInterval = function () { return mock }\n  global.clearInterval = function (ref) {\n    t.is(ref, mock)\n    global.setInterval = sI\n    global.clearInterval = cI\n    t.end()\n  }\n  var instance = protect('http')\n  instance.stop()\n})\n\ntest('sampleInterval option sets the sample rate', function (t) {\n  var sI = global.setInterval\n  var cI = global.clearInterval\n  var sampleRate = 88\n  var mock = {unref: function () { return mock }}\n  global.setInterval = function (fn, n) {\n    t.is(n, sampleRate)\n    return mock\n  }\n  global.clearInterval = function (ref) {\n    t.is(ref, mock)\n    global.setInterval = sI\n    global.clearInterval = cI\n    t.end()\n  }\n  var instance = protect('http', {\n    sampleInterval: sampleRate\n  })\n  instance.stop()\n})\n\ntest('exposes maxEventLoopDelay option on instance', function (t) {\n  var value = 9999\n  var instance = protect('http', {\n    maxEventLoopDelay: value\n  })\n  t.is(instance.maxEventLoopDelay, value)\n  instance.stop()\n  t.end()\n})\n\ntest('exposes maxHeapUsedBytes option on instance', function (t) {\n  var value = 9999\n  var instance = protect('http', {\n    maxHeapUsedBytes: value\n  })\n  t.is(instance.maxHeapUsedBytes, value)\n  instance.stop()\n  t.end()\n})\n\ntest('exposes maxRssBytes option on instance', function (t) {\n  var value = 9999\n  var instance = protect('http', {\n    maxRssBytes: value\n  })\n  t.is(instance.maxRssBytes, value)\n  instance.stop()\n  t.end()\n})\n\ntest('instance.eventLoopDelay indicates the delay between samples', function (t) {\n  var delay = 50\n  var instance = protect('http')\n  var start = Date.now()\n  // \"sleep\" with while blocking is imprecise, particularly in turbofan,\n  // throwing in a Buffer.alloc to compensate\n  while (Date.now() - start <= delay) { Buffer.alloc(1e9) }\n  setImmediate(function () {\n    t.is(instance.eventLoopDelay > delay, true)\n    instance.stop()\n    t.end()\n  })\n})\n\ntest('instance.eventLoopOverload is true when maxEventLoopDelay threshold is breached', function (t) {\n  var delay = 50\n  var instance = protect('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 10\n  })\n  var start = Date.now()\n  while (Date.now() - start < delay) {}\n  setImmediate(function () {\n    t.is(instance.eventLoopOverload, true)\n    instance.stop()\n    t.end()\n  })\n})\n\ntest('instance.eventLoopOverload is false when returning under maxEventLoopDelay threshold', function (t) {\n  var delay = 50\n  var instance = protect('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 10\n  })\n  var start = Date.now()\n  while (Date.now() - start < delay) {}\n  setImmediate(function () {\n    setTimeout(function () {\n      t.is(instance.eventLoopOverload, false)\n      instance.stop()\n      t.end()\n    }, 10)\n  })\n})\n\ntest('instance.eventLoopOverload is always false when maxEventLoopDelay is 0 (maxHeapUsedBytes enabled)', function (t) {\n  var delay = 50\n  var instance = protect('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxHeapUsedBytes: 10\n  })\n  var start = Date.now()\n  while (Date.now() - start < delay) {}\n  setImmediate(function () {\n    t.is(instance.eventLoopOverload, false)\n    instance.stop()\n    t.end()\n  })\n})\n\ntest('instance.eventLoopOverload is always false when maxEventLoopDelay is 0 (maxRssBytes enabled)', function (t) {\n  var delay = 50\n  var instance = protect('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 10\n  })\n  var start = Date.now()\n  while (Date.now() - start < delay) {}\n  setImmediate(function () {\n    t.is(instance.eventLoopOverload, false)\n    instance.stop()\n    t.end()\n  })\n})\n\ntest('instance.overload is true if instance.eventLoopOverload is true', function (t) {\n  var delay = 50\n  var instance = protect('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 1\n  })\n  var start = Date.now()\n  while (Date.now() - start < delay) {}\n  setImmediate(function () {\n    t.is(instance.eventLoopOverload, true)\n    t.is(instance.overload, instance.eventLoopOverload)\n    instance.stop()\n    t.end()\n  })\n})\n\ntest('instance.heapUsedOverload is true when maxHeapUsedBytes threshold is breached', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var instance = protect('http', {\n    sampleInterval: 5,\n    maxHeapUsedBytes: 10\n  })\n  setTimeout(function () {\n    t.is(instance.heapUsedOverload, true)\n    process.memoryUsage = memoryUsage\n    instance.stop()\n    t.end()\n  }, 6)\n})\n\ntest('instance.heapUsedOverload is false when returning under maxHeapUsedBytes threshold', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var instance = protect('http', {\n    sampleInterval: 5,\n    maxHeapUsedBytes: 10\n  })\n  setTimeout(function () {\n    process.memoryUsage = function () {\n      return {\n        rss: 99999,\n        heapTotal: 9999,\n        heapUsed: 2,\n        external: 99\n      }\n    }\n    setTimeout(function () {\n      t.is(instance.heapUsedOverload, false)\n      process.memoryUsage = memoryUsage\n      instance.stop()\n      t.end()\n    }, 6)\n  }, 6)\n})\n\ntest('instance.heapUsedOverload is always false when maxHeapUsedBytes is 0', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var instance = protect('http', {\n    sampleInterval: 5,\n    maxHeapUsedBytes: 0\n  })\n  setTimeout(function () {\n    t.is(instance.heapUsedOverload, false)\n    process.memoryUsage = memoryUsage\n    instance.stop()\n    t.end()\n  }, 6)\n})\n\ntest('instance.overload is true if instance.heapUsedOverload is true', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var instance = protect('http', {\n    sampleInterval: 5,\n    maxHeapUsedBytes: 10\n  })\n  setTimeout(function () {\n    t.is(instance.heapUsedOverload, true)\n    t.is(instance.overload, instance.heapUsedOverload)\n    instance.stop()\n    process.memoryUsage = memoryUsage\n    t.end()\n  }, 6)\n})\n\ntest('instance.rssOverload is true when maxRssBytes threshold is breached', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var instance = protect('http', {\n    sampleInterval: 5,\n    maxRssBytes: 10\n  })\n  setTimeout(function () {\n    t.is(instance.rssOverload, true)\n    process.memoryUsage = memoryUsage\n    instance.stop()\n    t.end()\n  }, 6)\n})\n\ntest('instance.rssOverload is false when returning under maxRssBytes threshold', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var instance = protect('http', {\n    sampleInterval: 5,\n    maxHeapUsedBytes: 10\n  })\n  setTimeout(function () {\n    process.memoryUsage = function () {\n      return {\n        rss: 2,\n        heapTotal: 9999,\n        heapUsed: 2,\n        external: 99\n      }\n    }\n    setTimeout(function () {\n      t.is(instance.rssOverload, false)\n      instance.stop()\n      process.memoryUsage = memoryUsage\n      t.end()\n    }, 6)\n  }, 6)\n})\n\ntest('instance.rssOverload is always false when maxRssBytes is 0', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var instance = protect('http', {\n    sampleInterval: 5,\n    maxRssBytes: 0\n  })\n  setTimeout(function () {\n    t.is(instance.rssOverload, false)\n    instance.stop()\n    process.memoryUsage = memoryUsage\n    t.end()\n  }, 6)\n})\n\ntest('instance.overload is true if instance.rssOverload is true', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var instance = protect('http', {\n    sampleInterval: 5,\n    maxRssBytes: 10\n  })\n  setTimeout(function () {\n    t.is(instance.rssOverload, true)\n    t.is(instance.overload, instance.rssOverload)\n    instance.stop()\n    process.memoryUsage = memoryUsage\n    t.end()\n  }, 6)\n})\n\nif (Object.setPrototypeOf) {\n  test('Supports legacy JS (__proto__)', function (t) {\n    var setPrototypeOf = Object.setPrototypeOf\n    delete Object.setPrototypeOf\n    var instance = protect('http')\n    // overload wouldn't be in instance if __proto__ wasn't set\n    t.is('overload' in instance, true)\n    t.end()\n    Object.setPrototypeOf = setPrototypeOf\n  })\n}\n\nif (!Object.setPrototypeOf) {\n  test('Supports modern/future JS (Object.setPrototypeOf)', function (t) {\n    Object.setPrototypeOf = function (o, proto) {\n      o.__proto__ = proto // eslint-disable-line\n    }\n    var instance = protect('http')\n    // overload wouldn't be in instance if __proto__ wasn't set\n    t.is('overload' in instance, true)\n    t.end()\n    delete Object.setPrototypeOf\n  })\n}\n"
  },
  {
    "path": "test/integration/express/index.js",
    "content": "'use strict'\n\nvar http = require('http')\nvar express = require('express')\n\nvar protection = require('../../..')\nvar test = require('tap').test\n\nfunction sleep (msec) {\n  var start = Date.now()\n  while (Date.now() - start < msec) {}\n}\n\ntest('sends 503 when event loop is overloaded, per maxEventLoopDelay', function (t) {\n  var protect = protection('express', {\n    maxEventLoopDelay: 1\n  })\n\n  var app = express()\n  app.use(protect)\n  var server = http.createServer(function (req, res) {\n    sleep(500)\n    app(req, res)\n  })\n\n  server.listen(3000, function () {\n    var req = http.get('http://localhost:3000')\n    req.on('response', function (res) {\n      t.is(res.statusCode, 503)\n      protect.stop()\n      server.close()\n      t.end()\n    }).end()\n  })\n})\n\ntest('sends 503 when heap used threshold is passed, as per maxHeapUsedBytes', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('express', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxHeapUsedBytes: 40\n  })\n\n  var app = express()\n  app.use(protect)\n  var server = http.createServer(app)\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('sends 503 when heap used threshold is passed, as per maxRssBytes', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('express', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40\n  })\n\n  var app = express()\n  app.use(protect)\n  var server = http.createServer(app)\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('sends Retry-After header as per clientRetrySecs', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('express', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    clientRetrySecs: 22\n  })\n\n  var app = express()\n  app.use(protect)\n  var server = http.createServer(app)\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        t.is(res.headers['retry-after'], '22')\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('does not set Retry-After header when clientRetrySecs is 0', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('express', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    clientRetrySecs: 0\n  })\n\n  var app = express()\n  app.use(protect)\n  var server = http.createServer(app)\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        t.is('retry-after' in res.headers, false)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('errorPropagationMode:false (default)', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('express', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: false\n  })\n\n  var app = express()\n  app.use(protect)\n  app.use(function () {\n    t.fail()\n  })\n  var server = http.createServer(app)\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('errorPropagationMode:true', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('express', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: true\n  })\n\n  var app = express()\n  app.use(protect)\n  app.use(function (err, req, res, next) {\n    t.ok(err)\n    t.is(err.statusCode, 503)\n    res.end('err message')\n  })\n  var server = http.createServer(app)\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in default mode, production:false leads to high detail client response message', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('express', {\n    production: false,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: false\n  })\n\n  var app = express()\n  app.use(protect)\n  var server = http.createServer(app)\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        res.once('data', function (msg) {\n          msg = msg.toString()\n          t.is(msg, 'Server experiencing heavy load: (rss)')\n          server.close()\n          protect.stop()\n          process.memoryUsage = memoryUsage\n          t.end()\n        })\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in default mode, production:true leads to standard 503 client response message', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('express', {\n    production: true,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: false\n  })\n\n  var app = express()\n  app.use(protect)\n  var server = http.createServer(app)\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        res.once('data', function (msg) {\n          msg = msg.toString()\n          t.is(msg, 'Service Unavailable')\n          server.close()\n          protect.stop()\n          process.memoryUsage = memoryUsage\n          t.end()\n        })\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in errorPropagationMode production:false sets expose:true on error object', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('express', {\n    production: false,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: true\n  })\n\n  var app = express()\n  app.use(protect)\n  app.use(function (err, req, res, next) {\n    t.ok(err)\n    t.is(err.expose, true)\n    res.end('err message')\n  })\n  var server = http.createServer(app)\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in errorPropagationMode production:true sets expose:false on error object', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('express', {\n    production: true,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: true\n  })\n\n  var app = express()\n  app.use(protect)\n  app.use(function (err, req, res, next) {\n    t.ok(err)\n    t.is(err.expose, false)\n    res.end('err message')\n  })\n  var server = http.createServer(app)\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('resumes usual operation once load pressure is reduced under threshold', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('express', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40\n  })\n\n  var app = express()\n  app.use(protect)\n  app.get('/', function (req, res) { res.end('content') })\n  var server = http.createServer(app)\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        process.memoryUsage = function () {\n          return {\n            rss: 10,\n            heapTotal: 9999,\n            heapUsed: 999,\n            external: 99\n          }\n        }\n        setTimeout(function () {\n          http.get('http://localhost:3000').on('response', function (res) {\n            t.is(res.statusCode, 200)\n            server.close()\n            protect.stop()\n            process.memoryUsage = memoryUsage\n            t.end()\n          })\n        }, 6)\n      }).end()\n    }, 6)\n  })\n})\n\ntest('if logging option is a string, when overloaded, writes log message using req.log as per level in string', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('express', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    logging: 'warn'\n  })\n\n  var app = express()\n  app.use(function (req, res, next) {\n    req.log = {\n      warn: function (msg) {\n        t.is(msg, 'Server experiencing heavy load: (rss)')\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }\n    }\n    next()\n  })\n  app.use(protect)\n  app.get('/', function (req, res) { res.end('content') })\n  var server = http.createServer(app)\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3000').end()\n    }, 6)\n  })\n})\n\ntest('if logging option is a function, when overloaded calls the function with heavy load message', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('express', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    logging: function (msg) {\n      t.is(msg, 'Server experiencing heavy load: (rss)')\n      server.close()\n      protect.stop()\n      process.memoryUsage = memoryUsage\n      t.end()\n    }\n  })\n\n  var app = express()\n  app.use(protect)\n  app.get('/', function (req, res) { res.end('content') })\n  var server = http.createServer(app)\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3000').end()\n    }, 6)\n  })\n})\n\ntest('if logStatsOnReq is true and if logging option is a string, writes log message using req.log as per level in string for every request', function (t) {\n  var protect = protection('express', {\n    logging: 'info',\n    logStatsOnReq: true\n  })\n  t.plan(1)\n  var app = express()\n  app.use(function (req, res, next) {\n    req.log = {\n      info: function (msg) {\n        t.same(Object.keys(msg), [\n          'overload',\n          'eventLoopOverload',\n          'heapUsedOverload',\n          'rssOverload',\n          'eventLoopDelay',\n          'maxEventLoopDelay',\n          'maxHeapUsedBytes',\n          'maxRssBytes'\n        ])\n        server.close()\n        protect.stop()\n        t.end()\n      }\n    }\n    next()\n  })\n  app.use(protect)\n  app.get('/', function (req, res) { res.end('content') })\n  var server = http.createServer(app)\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3000').end()\n    }, 6)\n  })\n})\n\ntest('if logStatsOnReq is true and logging option is a function, calls the function with stats on every request', function (t) {\n  var protect = protection('express', {\n    logStatsOnReq: true,\n    logging: function (msg) {\n      t.same(Object.keys(msg), [\n        'overload',\n        'eventLoopOverload',\n        'heapUsedOverload',\n        'rssOverload',\n        'eventLoopDelay',\n        'maxEventLoopDelay',\n        'maxHeapUsedBytes',\n        'maxRssBytes'\n      ])\n      server.close()\n      protect.stop()\n      t.end()\n    }\n  })\n\n  var app = express()\n  app.use(protect)\n  app.get('/', function (req, res) { res.end('content') })\n  var server = http.createServer(app)\n\n  server.listen(3001, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3001').end()\n    }, 6)\n  })\n})\n"
  },
  {
    "path": "test/integration/http/index.js",
    "content": "'use strict'\n\nvar http = require('http')\n\nvar protection = require('../../..')\nvar test = require('tap').test\n\nfunction sleep (msec) {\n  var start = Date.now()\n  while (Date.now() - start < msec) {}\n}\n\ntest('sends 503 when event loop is overloaded, per maxEventLoopDelay', function (t) {\n  var protect = protection('http', {\n    maxEventLoopDelay: 1\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    sleep(500)\n    if (protect(req, res) === true) return\n    res.end('content')\n  })\n\n  server.listen(3000, function () {\n    var req = http.get('http://localhost:3000')\n    req.on('response', function (res) {\n      t.is(res.statusCode, 503)\n      protect.stop()\n      server.close()\n      t.end()\n    }).end()\n  })\n})\n\ntest('sends 503 when heap used threshold is passed, as per maxHeapUsedBytes', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxHeapUsedBytes: 40\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    if (protect(req, res) === true) return\n    res.end('content')\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('sends 503 when heap used threshold is passed, as per maxRssBytes', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    if (protect(req, res) === true) return\n    res.end('content')\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('sends Retry-After header as per clientRetrySecs', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    clientRetrySecs: 22\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    if (protect(req, res) === true) return\n    res.end('content')\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        t.is(res.headers['retry-after'], '22')\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('does not set Retry-After header when clientRetrySecs is 0', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    clientRetrySecs: 0\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    if (protect(req, res) === true) return\n    res.end('content')\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        t.is('retry-after' in res.headers, false)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('callback api with errorPropagationMode false (default)', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: false\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    protect(req, res, function () {\n      t.fail() // should never be called\n      res.end('content')\n    })\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('callback api with errorPropagationMode true', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: true\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    protect(req, res, function (err) {\n      t.ok(err)\n      t.is(err.statusCode, 503)\n      res.end('err message')\n    })\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in default mode, production:false leads to high detail client response message', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('http', {\n    production: false,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: false\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    if (protect(req, res) === true) return\n    res.end('content')\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        res.once('data', function (msg) {\n          msg = msg.toString()\n          t.is(msg, 'Server experiencing heavy load: (rss)')\n          server.close()\n          protect.stop()\n          process.memoryUsage = memoryUsage\n          t.end()\n        })\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in default mode, production:true leads to standard 503 client response message', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('http', {\n    production: true,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: false\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    if (protect(req, res) === true) return\n    res.end('content')\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        res.once('data', function (msg) {\n          msg = msg.toString()\n          t.is(msg, 'Service Unavailable')\n          server.close()\n          protect.stop()\n          process.memoryUsage = memoryUsage\n          t.end()\n        })\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in errorPropagationMode production:false sets expose:true on error object', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('http', {\n    production: false,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: true\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    protect(req, res, function (err) {\n      t.ok(err)\n      t.is(err.expose, true)\n      res.end('err message')\n    })\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in errorPropagationMode production:true sets expose:false on error object', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('http', {\n    production: true,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: true\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    protect(req, res, function (err) {\n      t.ok(err)\n      t.is(err.expose, false)\n      res.end('err message')\n    })\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('resumes usual operation once load pressure is reduced under threshold', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    if (protect(req, res) === true) return\n    res.end('content')\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        process.memoryUsage = function () {\n          return {\n            rss: 10,\n            heapTotal: 9999,\n            heapUsed: 999,\n            external: 99\n          }\n        }\n        setTimeout(function () {\n          http.get('http://localhost:3000').on('response', function (res) {\n            t.is(res.statusCode, 200)\n            server.close()\n            protect.stop()\n            process.memoryUsage = memoryUsage\n            t.end()\n          })\n        }, 6)\n      }).end()\n    }, 6)\n  })\n})\n\ntest('(callback api) resumes usual operation once load pressure is reduced under threshold', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    protect(req, res, function () {\n      res.end('content')\n    })\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        process.memoryUsage = function () {\n          return {\n            rss: 10,\n            heapTotal: 9999,\n            heapUsed: 999,\n            external: 99\n          }\n        }\n        setTimeout(function () {\n          http.get('http://localhost:3000').on('response', function (res) {\n            t.is(res.statusCode, 200)\n            server.close()\n            protect.stop()\n            process.memoryUsage = memoryUsage\n            t.end()\n          })\n        }, 6)\n      }).end()\n    }, 6)\n  })\n})\n\ntest('if logging option is a string, when overloaded, writes log message using req.log as per level in string', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 1,\n    maxRssBytes: 40,\n    maxHeapUsedBytes: 40,\n    logging: 'warn'\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    req = {\n      log: {\n        warn: function (msg) {\n          t.is(msg, 'Server experiencing heavy load: (event loop, heap, rss)')\n          server.close()\n          protect.stop()\n          process.memoryUsage = memoryUsage\n          t.end()\n        }\n      }\n    }\n    if (protect(req, res) === true) return\n    res.end('content')\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      sleep(500)\n      http.get('http://localhost:3000').end()\n    }, 6)\n  })\n})\n\ntest('if logging option is a function, when overloaded calls the function with heavy load message', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('http', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    logging: function (msg) {\n      t.is(msg, 'Server experiencing heavy load: (rss)')\n      server.close()\n      protect.stop()\n      process.memoryUsage = memoryUsage\n      t.end()\n    }\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    if (protect(req, res) === true) return\n    res.end('content')\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3000').end()\n    }, 6)\n  })\n})\n\ntest('if logStatsOnReq is true and if logging option is a string, writes log message using req.log as per level in string for every request', function (t) {\n  var protect = protection('http', {\n    logging: 'info',\n    logStatsOnReq: true\n  })\n  t.plan(1)\n  var server = http.createServer(function serve (req, res) {\n    req = {\n      log: {\n        info: function (msg) {\n          t.same(Object.keys(msg), [\n            'overload',\n            'eventLoopOverload',\n            'heapUsedOverload',\n            'rssOverload',\n            'eventLoopDelay',\n            'maxEventLoopDelay',\n            'maxHeapUsedBytes',\n            'maxRssBytes'\n          ])\n          server.close()\n          protect.stop()\n          t.end()\n        }\n      }\n    }\n    if (protect(req, res) === true) return\n    res.end('content')\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3000').end()\n    }, 6)\n  })\n})\n\ntest('if logStatsOnReq is true and logging option is a function, calls the function with stats on every request', function (t) {\n  var protect = protection('http', {\n    logStatsOnReq: true,\n    logging: function (msg) {\n      t.same(Object.keys(msg), [\n        'overload',\n        'eventLoopOverload',\n        'heapUsedOverload',\n        'rssOverload',\n        'eventLoopDelay',\n        'maxEventLoopDelay',\n        'maxHeapUsedBytes',\n        'maxRssBytes'\n      ])\n      server.close()\n      protect.stop()\n      t.end()\n    }\n  })\n\n  var server = http.createServer(function serve (req, res) {\n    if (protect(req, res) === true) return\n    res.end('content')\n  })\n\n  server.listen(3002, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3002').end()\n    }, 6)\n  })\n})\n"
  },
  {
    "path": "test/integration/koa/index.js",
    "content": "'use strict'\nvar http = require('http')\nvar Koa = require('koa')\nvar Router = require('koa-router')\nvar protection = require('../../..')\nvar test = require('tap').test\n\nfunction block (n) {\n  while (n--) { JSON.parse(JSON.stringify(require('../../../package.json'))) }\n}\n\ntest('sends 503 when event loop is overloaded, per maxEventLoopDelay', function (t) {\n  var protect = protection('koa', {\n    maxEventLoopDelay: 1\n  })\n\n  var app = new Koa()\n\n  app.use(protect)\n\n  var server = app.listen(3000, function () {\n    var req = http.get('http://localhost:3000')\n    block(50000)\n    req.on('response', function (res) {\n      t.is(res.statusCode, 503)\n      protect.stop()\n      server.close()\n      t.end()\n    }).end()\n  })\n})\n\ntest('sends 503 when heap used threshold is passed, as per maxHeapUsedBytes', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('koa', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxHeapUsedBytes: 40\n  })\n\n  var app = new Koa()\n  app.use(protect)\n\n  var server = app.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('sends 503 when rss threshold is passed, as per maxRssBytes', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('koa', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40\n  })\n\n  var app = new Koa()\n  app.use(protect)\n\n  var server = app.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('sends Retry-After header as per clientRetrySecs', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('koa', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    clientRetrySecs: 22\n  })\n\n  var app = new Koa()\n  app.use(protect)\n\n  var server = app.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        t.is(res.headers['retry-after'], '22')\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('does not set Retry-After header when clientRetrySecs is 0', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('koa', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    clientRetrySecs: 0\n  })\n\n  var app = new Koa()\n  app.use(protect)\n\n  var server = app.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        t.is('retry-after' in res.headers, false)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('errorPropagationMode:false (default)', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('koa', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: false\n  })\n\n  var app = new Koa()\n  app.use(protect)\n  app.use(function (ctx, next) {\n    t.fail()\n    return next()\n  })\n\n  var server = app.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('errorPropagationMode:true', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('koa', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: true\n  })\n\n  var app = new Koa()\n  app.on('error', function () {}) // silence error log output\n  app.use(function (ctx, next) {\n    return next().catch(function (err) {\n      t.ok(err)\n      t.is(err.status, 503)\n      throw err\n    })\n  })\n\n  app.use(protect)\n\n  var server = app.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in default mode, production:false leads to high detail client response message', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('koa', {\n    production: false,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: false\n  })\n\n  var app = new Koa()\n  app.use(protect)\n\n  var server = app.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        res.once('data', function (msg) {\n          msg = msg.toString()\n          t.is(msg, 'Server experiencing heavy load: (rss)')\n          server.close()\n          protect.stop()\n          process.memoryUsage = memoryUsage\n          t.end()\n        })\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in default mode, production:true leads to standard 503 client response message', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('koa', {\n    production: true,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: false\n  })\n\n  var app = new Koa()\n  app.use(protect)\n\n  var server = app.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        res.once('data', function (msg) {\n          msg = msg.toString()\n          t.is(msg, 'Service Unavailable')\n          server.close()\n          protect.stop()\n          process.memoryUsage = memoryUsage\n          t.end()\n        })\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in errorPropagationMode production:false sets expose:true on error object', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('koa', {\n    production: false,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: true\n  })\n\n  var app = new Koa()\n  app.on('error', function () {}) // silence error log output\n  app.use(function (ctx, next) {\n    return next().catch(function (err) {\n      t.ok(err)\n      t.is(err.expose, true)\n      throw err\n    })\n  })\n\n  app.use(protect)\n\n  var server = app.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in errorPropagationMode production:true sets expose:false on error object', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('koa', {\n    production: true,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: true\n  })\n\n  var app = new Koa()\n  app.on('error', function () {}) // silence error log output\n  app.use(function (ctx, next) {\n    return next().catch(function (err) {\n      t.ok(err)\n      t.is(err.expose, false)\n      throw err\n    })\n  })\n  app.use(protect)\n\n  var server = app.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('resumes usual operation once load pressure is reduced under threshold', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('koa', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40\n  })\n\n  var app = new Koa()\n  var router = new Router()\n  app.use(protect)\n  router.get('/', function (ctx, next) {\n    ctx.body = 'content'\n    return next()\n  })\n\n  app.use(router.routes())\n\n  var server = app.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        process.memoryUsage = function () {\n          return {\n            rss: 10,\n            heapTotal: 9999,\n            heapUsed: 999,\n            external: 99\n          }\n        }\n        setTimeout(function () {\n          http.get('http://localhost:3000').on('response', function (res) {\n            t.is(res.statusCode, 200)\n            server.close()\n            protect.stop()\n            process.memoryUsage = memoryUsage\n            t.end()\n          })\n        }, 6)\n      }).end()\n    }, 6)\n  })\n})\n\ntest('if logging option is a string, when overloaded, writes log message using req.log as per level in string', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('koa', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    logging: 'warn'\n  })\n\n  var app = new Koa()\n  app.use(function (ctx, next) {\n    ctx.log = ctx.req.log = {\n      warn: function (msg) {\n        t.is(msg, 'Server experiencing heavy load: (rss)')\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }\n    }\n    return next()\n  })\n  app.use(protect)\n\n  var server = app.listen(3000, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3000').end()\n    }, 6)\n  })\n})\n\ntest('if logging option is a function, when overloaded calls the function with heavy load message', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('koa', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    logging: function (msg) {\n      t.is(msg, 'Server experiencing heavy load: (rss)')\n      server.close()\n      protect.stop()\n      process.memoryUsage = memoryUsage\n      t.end()\n    }\n  })\n\n  var app = new Koa()\n  app.use(protect)\n\n  var server = app.listen(3000, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3000').end()\n    }, 6)\n  })\n})\n\ntest('if logStatsOnReq is true and if logging option is a string, writes log message using req.log as per level in string for every request', function (t) {\n  var protect = protection('koa', {\n    logging: 'info',\n    logStatsOnReq: true\n  })\n  t.plan(1)\n  var app = new Koa()\n  app.use(function (ctx, next) {\n    ctx.log = ctx.req.log = {\n      info: function (msg) {\n        t.same(Object.keys(msg), [\n          'overload',\n          'eventLoopOverload',\n          'heapUsedOverload',\n          'rssOverload',\n          'eventLoopDelay',\n          'maxEventLoopDelay',\n          'maxHeapUsedBytes',\n          'maxRssBytes'\n        ])\n        server.close()\n        protect.stop()\n        t.end()\n      }\n    }\n    return next()\n  })\n  app.use(protect)\n\n  var server = app.listen(3000, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3000').end()\n    }, 6)\n  })\n})\n\ntest('if logStatsOnReq is true and logging option is a function, calls the function with stats on every request', function (t) {\n  var protect = protection('koa', {\n    logStatsOnReq: true,\n    logging: function (msg) {\n      t.same(Object.keys(msg), [\n        'overload',\n        'eventLoopOverload',\n        'heapUsedOverload',\n        'rssOverload',\n        'eventLoopDelay',\n        'maxEventLoopDelay',\n        'maxHeapUsedBytes',\n        'maxRssBytes'\n      ])\n      server.close()\n      protect.stop()\n      t.end()\n    }\n  })\n\n  var app = new Koa()\n  app.use(protect)\n\n  var server = app.listen(3001, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3001').end()\n    }, 6)\n  })\n})\n"
  },
  {
    "path": "test/integration/restify/index.js",
    "content": "'use strict'\n\nvar http = require('http')\nvar restify = require('restify')\n\nvar protection = require('../../..')\nvar test = require('tap').test\n\nfunction block (n) {\n  while (n--) { JSON.parse(JSON.stringify(require('../../../package.json'))) }\n}\n\ntest('sends 503 when event loop is overloaded, per maxEventLoopDelay', function (t) {\n  var protect = protection('restify', {\n    maxEventLoopDelay: 1\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n\n  server.listen(3000, function () {\n    var req = http.get('http://localhost:3000')\n    block(50000)\n    req.on('response', function (res) {\n      t.is(res.statusCode, 503)\n      protect.stop()\n      server.close()\n      t.end()\n    }).end()\n  })\n})\n\ntest('sends 503 when heap used threshold is passed, as per maxHeapUsedBytes', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('restify', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxHeapUsedBytes: 40\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('sends 503 when heap used threshold is passed, as per maxRssBytes', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('restify', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('sends Retry-After header as per clientRetrySecs', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('restify', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    clientRetrySecs: 22\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        t.is(res.headers['retry-after'], '22')\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('does not set Retry-After header when clientRetrySecs is 0', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('restify', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    clientRetrySecs: 0\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        t.is('retry-after' in res.headers, false)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('errorPropagationMode:false (default)', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('restify', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: false\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n  server.use(function () {\n    t.fail()\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('errorPropagationMode:true', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('restify', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: true\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n  server.use(function (err, req, res, next) {\n    t.ok(err)\n    t.is(err.statusCode, 503)\n    res.end('err message')\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in default mode, production:false leads to high detail client response message', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('restify', {\n    production: false,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: false\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        res.once('data', function (msg) {\n          msg = msg.toString()\n          t.is(msg, 'Server experiencing heavy load: (rss)')\n          server.close()\n          protect.stop()\n          process.memoryUsage = memoryUsage\n          t.end()\n        })\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in default mode, production:true leads to standard 503 client response message', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('restify', {\n    production: true,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: false\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        res.once('data', function (msg) {\n          msg = msg.toString()\n          t.is(msg, 'Service Unavailable')\n          server.close()\n          protect.stop()\n          process.memoryUsage = memoryUsage\n          t.end()\n        })\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in errorPropagationMode production:false sets expose:true on error object', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('restify', {\n    production: false,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: true\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n  server.use(function (err, req, res, next) {\n    t.ok(err)\n    t.is(err.expose, true)\n    res.end('err message')\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('in errorPropagationMode production:true sets expose:false on error object', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('restify', {\n    production: true,\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    errorPropagationMode: true\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n  server.use(function (err, req, res, next) {\n    t.ok(err)\n    t.is(err.expose, false)\n    res.end('err message')\n  })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }).end()\n    }, 6)\n  })\n})\n\ntest('resumes usual operation once load pressure is reduced under threshold', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('restify', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      var req = http.get('http://localhost:3000')\n      req.on('response', function (res) {\n        t.is(res.statusCode, 503)\n        process.memoryUsage = function () {\n          return {\n            rss: 10,\n            heapTotal: 9999,\n            heapUsed: 999,\n            external: 99\n          }\n        }\n        setTimeout(function () {\n          http.get('http://localhost:3000').on('response', function (res) {\n            t.is(res.statusCode, 200)\n            server.close()\n            protect.stop()\n            process.memoryUsage = memoryUsage\n            t.end()\n          })\n        }, 6)\n      }).end()\n    }, 6)\n  })\n})\n\ntest('if logging option is a string, when overloaded, writes log message using req.log as per level in string', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('restify', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    logging: 'warn'\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(function (req, res, next) {\n    req.log = {\n      warn: function (msg) {\n        t.is(msg, 'Server experiencing heavy load: (rss)')\n        server.close()\n        protect.stop()\n        process.memoryUsage = memoryUsage\n        t.end()\n      }\n    }\n    next()\n  })\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3000').end()\n    }, 6)\n  })\n})\n\ntest('if logging option is a function, when overloaded calls the function with heavy load message', function (t) {\n  var memoryUsage = process.memoryUsage\n  process.memoryUsage = function () {\n    return {\n      rss: 99999,\n      heapTotal: 9999,\n      heapUsed: 999,\n      external: 99\n    }\n  }\n  var protect = protection('restify', {\n    sampleInterval: 5,\n    maxEventLoopDelay: 0,\n    maxRssBytes: 40,\n    logging: function (msg) {\n      t.is(msg, 'Server experiencing heavy load: (rss)')\n      server.close()\n      protect.stop()\n      process.memoryUsage = memoryUsage\n      t.end()\n    }\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n\n  server.listen(3000, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3000').end()\n    }, 6)\n  })\n})\n\ntest('if logStatsOnReq is true and if logging option is a string, writes log message using req.log as per level in string for every request', function (t) {\n  var protect = protection('restify', {\n    logging: 'info',\n    logStatsOnReq: true\n  })\n  t.plan(1)\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(function (req, res, next) {\n    req.log = {\n      info: function (msg) {\n        t.same(Object.keys(msg), [\n          'overload',\n          'eventLoopOverload',\n          'heapUsedOverload',\n          'rssOverload',\n          'eventLoopDelay',\n          'maxEventLoopDelay',\n          'maxHeapUsedBytes',\n          'maxRssBytes'\n        ])\n        server.close()\n        protect.stop()\n        t.end()\n      }\n    }\n    next()\n  })\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n  server.listen(3000, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3000').end()\n    }, 6)\n  })\n})\n\ntest('if logStatsOnReq is true and logging option is a function, calls the function with stats on every request', function (t) {\n  var protect = protection('restify', {\n    logStatsOnReq: true,\n    logging: function (msg) {\n      t.same(Object.keys(msg), [\n        'overload',\n        'eventLoopOverload',\n        'heapUsedOverload',\n        'rssOverload',\n        'eventLoopDelay',\n        'maxEventLoopDelay',\n        'maxHeapUsedBytes',\n        'maxRssBytes'\n      ])\n      server.close()\n      protect.stop()\n      t.end()\n    }\n  })\n\n  var server = restify.createServer({name: 'myapp', version: '1.0.0'})\n  server.use(protect)\n  server.get('/', function (req, res) { res.end('content') })\n\n  server.listen(3001, function () {\n    setTimeout(function () {\n      http.get('http://localhost:3001').end()\n    }, 6)\n  })\n})\n"
  }
]