40 points · by mifydev · 2y ago · 22 comments
They accidentally wrote synchronous O(n^2) code that hogged the CPU, blocking the event loop, then fixed it. But that doesn’t sound as adventurous…
Otherwise a solid example of using observability tools to debug a live issue.
It’s about how to systematically detect and debug the problem. In Node that’s not a trivial thing to do. That has value
Depends on your code best practices, I've found it way easier than other platforms I've used (C++, Python). Even without explicit interrupts and such.
I'm a pretty sophisticated node dev and I haven't heard of the technique of logging spans whenever the event loop blocks for 100 ms (similar to how you get warnings by default in frameworks/chrome for going over 100 ms on event handlers).
Obviously simple to run a setInterval and compare wall clock, but I would have no idea how to detect the actual issue.
As an aside, it’s a bit weird to create a span to mark that something happened, OTel events are made for that
if (n % 1000 === 0) await require('node:timers/promises').setImmediste()
If you sleep in your async functions, other work can flow. Just unblock the other work.Node calls this partitioning your work. https://nodejs.org/en/learn/asynchronous-work/dont-block-the...
Maybe don't use this specific library but it's pretty easy to rebuild everyday array iterators (forEach, reduce, map) to automatically yield every n iterations. https://www.npmjs.com/package/nice-loops