{"id":15723315,"url":"https://github.com/rhinobase/hono-under-pressure","last_synced_at":"2026-03-04T15:31:08.664Z","repository":{"id":246712170,"uuid":"812770640","full_name":"rhinobase/hono-under-pressure","owner":"rhinobase","description":"Measure process load with automatic handling of \"Service Unavailable\" plugin for Hono Nodejs runtime. ","archived":false,"fork":false,"pushed_at":"2024-07-05T06:43:30.000Z","size":538,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-24T17:55:11.848Z","etag":null,"topics":["api","hono","honojs","middleware","nodejs","optimization","security","server","web"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rhinobase.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-09T20:36:34.000Z","updated_at":"2025-10-24T11:59:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"e45b1d31-01e2-463e-a6df-c42ccdccfbb0","html_url":"https://github.com/rhinobase/hono-under-pressure","commit_stats":null,"previous_names":["rhinobase/hono-under-pressure"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/rhinobase/hono-under-pressure","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhinobase%2Fhono-under-pressure","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhinobase%2Fhono-under-pressure/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhinobase%2Fhono-under-pressure/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhinobase%2Fhono-under-pressure/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rhinobase","download_url":"https://codeload.github.com/rhinobase/hono-under-pressure/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhinobase%2Fhono-under-pressure/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30084960,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T13:22:36.021Z","status":"ssl_error","status_checked_at":"2026-03-04T13:20:45.750Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["api","hono","honojs","middleware","nodejs","optimization","security","server","web"],"created_at":"2024-10-03T22:11:09.326Z","updated_at":"2026-03-04T15:31:08.618Z","avatar_url":"https://github.com/rhinobase.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\u003ccode\u003e🌀hono-under-pressure🌀\u003c/code\u003e\u003c/h1\u003e\n\n\u003cdiv align=\"center\"\u003e\n\n[![tests](https://img.shields.io/github/actions/workflow/status/rhinobase/hono-under-pressure/test.yml)](https://github.com/rhinobase/hono-under-pressure/actions/workflows/test.yml)\n[![npm version](https://img.shields.io/npm/v/hono-under-pressure.svg)](https://npmjs.org/package/hono-under-pressure \"View this project on NPM\")\n[![npm downloads](https://img.shields.io/npm/dm/hono-under-pressure)](https://www.npmjs.com/package/hono-under-pressure)\n[![license](https://img.shields.io/npm/l/hono-under-pressure)](LICENSE)\n\n\u003c/div\u003e\n\nMeasure process load with automatic handling of _\"Service Unavailable\"_ plugin for Hono.\nIt can check `maxEventLoopDelay`, `maxHeapUsedBytes`, `maxRssBytes` and `maxEventLoopUtilization` values.\nYou can also specify a custom health check, to verify the status of\nexternal resources.\n\n\u003ca name=\"install\"\u003e\u003c/a\u003e\n\n## Install\n\n```sh\n# Using npm/yarn/pnpm/bun\nnpm add hono-under-pressure\n```\n\n\u003ca name=\"usage\"\u003e\u003c/a\u003e\n\n## Usage\n\nWrap the function around your server instance and the provided middlewares can be added to Hono instance to apply them.\n\n```js\nimport { underPressure } from \"hono-under-pressure\";\nimport { serve } from \"@hono/node-server\";\nimport { Hono } from \"hono\";\n\nconst app = new Hono();\napp.get(\"/\", (c) =\u003e {\n  const isUnderPressure = c.get(\"isUnderPressure\");\n  if (isUnderPressure()) {\n    // skip complex computation\n  }\n  return c.text(\"Hello Node.js!\");\n});\n\nunderPressure(\n  (handlers) =\u003e {\n    const newApp = new Hono().use(...handlers);\n    newApp.route(\"/\", app);\n    return serve(newApp);\n  },\n  {\n    maxEventLoopDelay: 1000,\n    maxHeapUsedBytes: 100000000,\n    maxRssBytes: 100000000,\n    maxEventLoopUtilization: 0.98,\n  }\n);\n```\n\n`hono-under-pressure` will automatically handle for you the `Service Unavailable` error once one of the thresholds has been reached.\nYou can configure the error handler by passing `pressureHandler` function.\n\n```js\nunderPressure(createServer, {\n  pressureHandler: () =\u003e {\n    throw new HTTPException(SERVICE_UNAVAILABLE, {\n      message: \"Under pressure!\",\n    });\n  },\n});\n```\n\nThe default value for `maxEventLoopDelay`, `maxHeapUsedBytes`, `maxRssBytes` and `maxEventLoopUtilization` is `0`.\nIf the value is `0` the check will not be performed.\n\nSince [`eventLoopUtilization`](https://nodejs.org/api/perf_hooks.html#perf_hooks_performance_eventlooputilization_utilization1_utilization2) is only available in Node version 14.0.0 and 12.19.0 the check will be disabled in other versions.\n\n#### `memoryUsage`\n\nThis plugin also exposes a function that will tell you the current values of `heapUsed`, `rssBytes`, `eventLoopDelay` and `eventLoopUtilized`.\n\n```js\nconst memoryUsage = c.get(\"memoryUsage\");\nconsole.log(memoryUsage());\n```\n\n#### Pressure Handler\n\nYou can provide a pressure handler in the options to handle the pressure errors. The advantage is that you know why the error occurred. Moreover, the request can be handled as if nothing happened.\n\n```js\nunderPressure(createServer, {\n  maxHeapUsedBytes: 100000000,\n  maxRssBytes: 100000000,\n  pressureHandler: (c, type, value) =\u003e {\n    if (type === underPressure.TYPE_HEAP_USED_BYTES) {\n      console.warn(`too many heap bytes used: ${value}`);\n    } else if (type === underPressure.TYPE_RSS_BYTES) {\n      console.warn(`too many rss bytes used: ${value}`);\n    }\n\n    throw new HTTPException(503, { message: \"out of memory\" }); // if you omit this line, the request will be handled normally\n  },\n});\n```\n\nIt is possible as well to return a Promise that will call `c.text` (or something else).\n\n```js\nunderPressure(createServer, {\n  maxHeapUsedBytes: 100000000,\n  pressureHandler: (c, type, value) =\u003e {\n    return getPromise().then(() =\u003e {\n      throw new HTTPException(503, { message: \"out of memory\" });\n    });\n  },\n});\n```\n\nIf you don't throw a HTTPException, the request will be handled normally.\n\nIt's also possible to specify the `pressureHandler` on the route:\n\n```js\nimport { underPressure } from \"hono-under-pressure\";\nimport { serve } from \"@hono/node-server\";\nimport { Hono } from \"hono\";\n\nconst app = new Hono();\n\napp.use((c, next) =\u003e {\n  c.set(\"pressureHandler\", (c, type, value) =\u003e {\n    if (type === underPressure.TYPE_HEAP_USED_BYTES) {\n      console.warn(`too many heap bytes used: ${value}`);\n    } else if (type === underPressure.TYPE_RSS_BYTES) {\n      console.warn(`too many rss bytes used: ${value}`);\n    }\n\n    throw new HTTPException(503, { message: \"out of memory\" }); // if you omit this line, the request will be handled normally\n  });\n});\n\napp.get(\"/\", (c) =\u003e {\n  return c.text(\"A\");\n});\n\nunderPressure(\n  (handlers) =\u003e {\n    const newApp = new Hono().use(...handlers);\n    newApp.route(\"/\", app);\n    return serve(newApp);\n  },\n  {\n    maxHeapUsedBytes: 100000000,\n    maxRssBytes: 100000000,\n  }\n);\n```\n\n#### Custom health checks\n\nIf needed you can pass a custom `healthCheck` property, which is an async function, and `hono-under-pressure` will allow you to check the status of other components of your service.\n\nThis function should return a promise that resolves to a boolean value or to an object. The `healthCheck` function can be called every X milliseconds, the time can be configured with the `healthCheckInterval` option.\n\nBy default when this function is supplied your service health is considered unhealthy, until it has started to return true.\n\n```js\nunderPressure(createServer, {\n  healthCheck: async function () {\n    // do some magic to check if your db connection is healthy, etc...\n    return true;\n  },\n  healthCheckInterval: 500,\n});\n```\n\n\u003ca name=\"sample-interval\"\u003e\u003c/a\u003e\n\n#### Sample interval\n\nYou can set a custom value for sampling the metrics returned by `memoryUsage` using the `sampleInterval` option, which accepts a number that represents the interval in milliseconds.\n\nThe default value is different depending on which Node version is used. In version 8 and 10 it is `5`, while on version 11.10.0 and up it is `1000`. This difference is because from version 11.10.0 the event loop delay can be sampled with [`monitorEventLoopDelay`](https://nodejs.org/docs/latest-v12.x/api/perf_hooks.html#perf_hooks_perf_hooks_monitoreventloopdelay_options) and this allows to increase the interval value.\n\n```js\nunderPressure(\n  createServer,\n  {\n    sampleInterval: \u003cyour custom sample interval in ms\u003e\n  }\n);\n```\n\n\u003ca name=\"additional-information\"\u003e\u003c/a\u003e\n\n## Additional information\n\n\u003ca name=\"set-timeout-vs-set-interval\"\u003e\u003c/a\u003e\n\n#### `setTimeout` vs `setInterval`\n\nUnder the hood the `hono-under-pressure` uses the `setTimeout` method to perform its polling checks. The choice is based on the fact that we do not want to add additional pressure to the system.\n\nIn fact, it is known that `setInterval` will call repeatedly at the scheduled time regardless of whether the previous call ended or not, and if the server is already under load, this will likely increase the problem, because those `setInterval` calls will start piling up. `setTimeout`, on the other hand, is called only once and does not cause the mentioned problem.\n\nOne note to consider is that because the two methods are not identical, the timer function is not guaranteed to run at exactly the same rate when the system is under pressure or running a long-running process.\n\n\u003ca name=\"articles\"\u003e\u003c/a\u003e\n\n#### Articles\n\nThese can help you understand how it can improve your Hono Nodejs server performance -\n\n- \u003chttps://nodesource.com/blog/event-loop-utilization-nodejs/\u003e\n- \u003chttps://blog.platformatic.dev/the-nodejs-event-loop\u003e\n\n## Contributing\n\nWe would love to have more contributors involved!\n\nTo get started, please read our [Contributing Guide](https://github.com/rhinobase/hono-under-pressure/blob/main/CONTRIBUTING.md).\n\n## Credits\n\nThe `hono-under-pressure` project is heavily inspired by [@fastify/under-pressure](https://github.com/fastify/under-pressure)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frhinobase%2Fhono-under-pressure","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frhinobase%2Fhono-under-pressure","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frhinobase%2Fhono-under-pressure/lists"}