I already wrote extensively of the new transport system in [email protected]
that we are developing at https://github.com/pinojs/pino/pull/1003
: it will allow you to create in-process transport similar to Winston or Bunyan, with no overhead on the main thread, as they will run inside a worker thread.
One of the biggest challenges of this approach is handling the shutdown of the process correctly. Two cases cause Node.js to exit: it can exit naturally if there are no more asynchronous things to do or manually by calling `process.exit(code)`. For the first case, we are calling unref() on the Worker thread
to let the process exit even if the transport thread is running. In the second case, we thought of adding a handler to the `‘exit’` event
to gracefully shut down the worker and ensure that all log messages are delivered independently of the status of the main thread. However, this approach had a significant obstacle to overcome: the `‘exit’` handler must be synchronous.
To achieve this, I updated the thread-stream `.end()` method to close the stream synchronously - this relies on work my friend mafintosh
did a long time ago on Node.js stream: the autoDestroy mode (https://github.com/nodejs/node/pull/22795
). Thanks to autoDestroy, we can guarantee that all streams emit a `‘close’` event when the underlining resource is fully closed. This setting became `true` by default in Node v12 onwards, while in readable-stream
, it is still false by default.