{"id":13989712,"url":"https://github.com/STRML/node-toobusy","last_synced_at":"2025-07-22T11:31:09.123Z","repository":{"id":8686463,"uuid":"10347573","full_name":"STRML/node-toobusy","owner":"STRML","description":"Don't let your Node.JS server fall over when it's too busy.","archived":false,"fork":true,"pushed_at":"2018-07-19T07:32:42.000Z","size":76,"stargazers_count":367,"open_issues_count":7,"forks_count":33,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-06-29T11:01:49.554Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"lloyd/node-toobusy","license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/STRML.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-05-28T23:07:51.000Z","updated_at":"2025-05-02T18:18:35.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/STRML/node-toobusy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/STRML/node-toobusy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/STRML%2Fnode-toobusy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/STRML%2Fnode-toobusy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/STRML%2Fnode-toobusy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/STRML%2Fnode-toobusy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/STRML","download_url":"https://codeload.github.com/STRML/node-toobusy/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/STRML%2Fnode-toobusy/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266483559,"owners_count":23936366,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-09T13:01:59.376Z","updated_at":"2025-07-22T11:31:08.854Z","avatar_url":"https://github.com/STRML.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"[![Build Status](https://secure.travis-ci.org/STRML/node-toobusy.png)](http://travis-ci.org/STRML/node-toobusy)\n\n# Is Your Node Process Too Busy?\n\n`toobusy-js` is a fork of lloyd's [node-toobusy](http://github.com/lloyd/node-toobusy) that removes native dependencies\nin favor of using the `unref` introduced in [node 0.9.1](http://blog.nodejs.org/2012/08/28/node-v0-9-1-unstable/).\n\nThis package is a simpler install without native dependencies, but requires node \u003e= 0.9.1.\n\n## Node-Toobusy\n\nWhat happens when your service is overwhelmed with traffic?\nYour server can do one of two things:\n\n  * Stop working, or...\n  * Keep serving as many requests as possible\n\nThis library helps you do the latter.\n\n## How it works\n\n`toobusy` polls the node.js event loop and keeps track of \"lag\",\nwhich is long requests wait in node's event queue to be processed.\nWhen lag crosses a threshold, `toobusy` tells you that you're *too busy*.\nAt this point you can stop request processing early\n(before you spend too much time on them and compound the problem),\nand return a \"Server Too Busy\" response.\nThis allows your server to stay *responsive* under extreme load,\nand continue serving as many requests as possible.\n\n## installation\n\n```\nnpm install toobusy-js\n```\n\n\n## usage\n\n```javascript\nvar toobusy = require('toobusy-js'),\n    express = require('express');\n\nvar app = express();\n\n// middleware which blocks requests when we're too busy\napp.use(function(req, res, next) {\n  if (toobusy()) {\n    res.send(503, \"I'm busy right now, sorry.\");\n  } else {\n    next();\n  }\n});\n\napp.get('/', function(req, res) {\n  // processing the request requires some work!\n  var i = 0;\n  while (i \u003c 1e5) i++;\n  res.send(\"I counted to \" + i);\n});\n\nvar server = app.listen(3000);\n\nprocess.on('SIGINT', function() {\n  server.close();\n  // calling .shutdown allows your process to exit normally\n  toobusy.shutdown();\n  process.exit();\n});\n```\n\n## tunable parameters\n\nThe library exposes a few knobs:\n\n`maxLag` - This number represents the maximum amount of time in milliseconds that the event queue is behind,\nbefore we consider the process *too busy*.\n`interval` - The check interval for measuring event loop lag, in ms.\n\n```javascript\nvar toobusy = require('toobusy-js');\n\n// Set maximum lag to an aggressive value.\ntoobusy.maxLag(10);\n\n// Set check interval to a faster value. This will catch more latency spikes\n// but may cause the check to be too sensitive.\ntoobusy.interval(250);\n\n// Get current maxLag or interval setting by calling without parameters.\nvar currentMaxLag = toobusy.maxLag(), interval = toobusy.interval();\n\ntoobusy.onLag(function(currentLag) {\n  console.log(\"Event loop lag detected! Latency: \" + currentLag + \"ms\");\n});\n```\n\nThe default maxLag value is 70ms, and the default check interval is 500ms.\nThis allows an \"average\" server to run at 90-100% CPU\nand keeps request latency at around 200ms.\nFor comparison, a maxLag value of 10ms results in 60-70% CPU usage,\nwhile latency for \"average\" requests stays at about 40ms.\n\nThese numbers are only examples,\nand the specifics of your hardware and application can change them drastically,\nso experiment!\nThe default of 70 should get you started.\n\n## Events\n\nAs of `0.5.0`, `toobusy-js` exposes an `onLag` method. Pass it a callback to be notified when\na slow event loop tick has been detected.\n\n## references\n\n\u003e There is nothing new under the sun. (Ecclesiastes 1:9)\n\nThough applying \"event loop latency\" to node.js was not directly inspired by anyone else's work,\nthis concept is not new.  Here are references to others who apply the same technique:\n\n  * [Provos, Lever, and Tweedie 2000](http://www.kegel.com/c10k.html#tips) - \"notes that dropping incoming connections when the server is overloaded improved the shape of the performance curve.\"\n\n## license\n\n[WTFPL](http://wtfpl.org)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSTRML%2Fnode-toobusy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSTRML%2Fnode-toobusy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSTRML%2Fnode-toobusy/lists"}