Skip to content

Commit dc4e854

Browse files
TommyDew42jsumners
andauthoredSep 5, 2022
Document how error is received from worker transport (#1548)
* document how error is received from worker transport * add a link to nodejs worker thread doc * improve doc based on review * ops, missed a few places * Update docs/transports.md Co-authored-by: James Sumners <james@sumners.email> * add a line between sentences Co-authored-by: James Sumners <james@sumners.email>
1 parent 376a8c4 commit dc4e854

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
 

‎docs/transports.md

+29
Original file line numberDiff line numberDiff line change
@@ -887,3 +887,32 @@ $ node app.js | pino-websocket -a my-websocket-server.example.com -p 3004
887887
For full documentation of command line switches read the [README](https://github.com/abeai/pino-websocket#readme).
888888
889889
[pino-pretty]: https://github.com/pinojs/pino-pretty
890+
891+
<a id="communication-between-pino-and-transport"></a>
892+
## Communication between Pino and Transports
893+
Here we discuss some technical details of how Pino communicates with its [worker threads](https://nodejs.org/api/worker_threads.html).
894+
895+
Pino uses [`thread-stream`](https://github.com/pinojs/thread-stream) to create a stream for transports.
896+
When we create a stream with `thread-stream`, `thread-stream` spawns a [worker](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/index.js#L50-L60) (an independent JavaScript execution thread).
897+
898+
899+
### Error messages
900+
How are error messages propagated from a transport worker to Pino?
901+
902+
Let's assume we have a transport with an error listener:
903+
```js
904+
// index.js
905+
const transport = pino.transport({
906+
target: './transport.js'
907+
})
908+
909+
transport.on('error', err => {
910+
console.error('error caught', err)
911+
})
912+
913+
const log = pino(transport)
914+
```
915+
916+
When our worker emits an error event, the worker has listeners for it: [error](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/lib/worker.js#L59-L70) and [unhandledRejection](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/lib/worker.js#L135-L141). These listeners send the error message to the main thread where Pino is present.
917+
918+
When Pino receives the error message, it further [emits](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/index.js#L349) the error message. Finally, the error message arrives at our `index.js` and is caught by our error listener.

0 commit comments

Comments
 (0)
Please sign in to comment.