This `http-close` package for Node.js provides a mechanism to gracefully shut down an HTTP server by managing open TCP sockets. Currently at version 1.0.0, it appears to be a stable utility, likely in maintenance mode given its focused scope and the absence of rapid version increments. It differentiates itself by intelligently handling various socket states during `server.close()`: it destroys keep-alive sockets without active responses, sends `Connection: close` headers for requests where headers haven't been sent, and applies a configurable timeout (default 5 seconds) to pending connections. If a socket times out after `server.close()` has been called, the module intervenes to respond with a 500 status code before ending the connection for requests with unsent headers, and forcefully destroys all other remaining sockets. This ensures a clean exit for the server process, mitigating the common issue of hanging connections during shutdown.
npm install http-closeVerified import paths — ran on the pinned version, not inferred.
This example sets up an HTTP server with `http-close` to manage graceful shutdown. It simulates asynchronous request handling and demonstrates how to hook `http-close` to the server and trigger shutdown via `server.close()` in response to OS signals (SIGTERM/SIGINT), ensuring open connections are handled gracefully with a specified timeout.
Always ensure you call `server.close()` when you intend to shut down your server. `httpClose({ timeout: X }, server)` should be called once after server creation but before `server.close()` is invoked.Consider setting an explicit `timeout` option when initializing `httpClose`. For example: `httpClose({ timeout: 10000 }, server)` for 10 seconds, or `httpClose({ timeout: 1000 }, server)` for 1 second.Evaluate if its functionality meets all requirements for modern Node.js applications. Consider alternatives if your project requires active development support, advanced features, or is highly sensitive to the latest platform changes.
Ensure client applications are designed to gracefully handle connection termination during server shutdown, especially for services relying heavily on HTTP keep-alive. This behavior is part of the graceful shutdown strategy.
Verify that `httpClose` is correctly applied to your `http.Server` instance. Ensure you've set an appropriate `timeout` option that allows pending requests to complete while also forcing closure within acceptable bounds. Long-running requests might still delay shutdown until their timeout is reached.
This is expected behavior for requests that cannot complete within the specified `httpClose` timeout during shutdown. Inform clients about scheduled maintenance periods, or increase the `timeout` if these requests are critical and must always attempt to complete gracefully.
For ESM, use `import httpClose from 'http-close';`. For CommonJS, `const httpClose = require('http-close');`. Always ensure a valid instance of `http.createServer()` result is passed as the second argument: `httpClose({ timeout: 5000 }, yourHttpServerInstance);`.No dependency data recorded yet.