我对Node.js Core的首次贡献中学到了什么

by Yael Hermon

通过Yael Hermon

我对Node.js Core的首次贡献中学到了什么 (What I Learned from My First Contribution To Node.js Core)

A couple of weeks ago my very first PR for Node.js core was merged! A few days later, I decided to tweet about it and share how positive this experience was, hoping to encourage others to contribute as well.

几周前,我对Node.js核心的第一个PR被合并了! 几天后,我决定发布推文并分享这种体验的积极程度,希望能鼓励其他人也做出贡献。

Later, Uri Shaked suggested I share my experience in a short blog post. Uri always has great ideas. Thanks, Uri!

后来, Uri Shaked建议我在一篇简短的博客文章中分享我的经验。 乌里总是有好主意。 谢谢友利

我如何最终获得此PR? (How did I end up with this PR?)

I’m glad you asked. Let me start with a little background. I love Node.js, and contributing to it was actually on my bucket list for a really long time. I never got around to doing it because I always told myself that I didn’t have the time for it, or that I might not be qualified enough, or other lame excuses.

我很高兴你问。 让我从一些背景开始。 我喜欢Node.js,并且对它的贡献实际上已经存在了很长时间。 我从来没有绕过它,因为我总是告诉自己,我没有时间去做,或者我可能没有足够的资格,或者其他la脚的借口。

The plot twist happened when I was working on a talk for a JavaScript-Israel meetup about the V8 Garbage Collector. Benjamin Gruenbaum, a Node.js core collaborator, asked me if I would like him to connect me with V8 engineers to review my slides. Umm…obviously I would.

当我正在为关于V8垃圾收集器JavaScript-以色列聚会进行演讲时,发生了剧情扭曲。 Node.js核心协作者Benjamin Gruenbaum问我是否希望他让我与V8工程师联系以查看我的幻灯片。 嗯…… 显然我会的。

So he did, which turned out pretty awesome. Benji also asked me if I was interested in contributing to Node.js core. Again, I said yes. No more excuses. Stuff just got real.

因此,他做到了,结果真棒。 Benji还问我是否有兴趣为Node.js核心做出贡献。 我再说一次。 别再找借口。 东西变得真实。

搭建环境 (Setting up the environment)

I first had to build Node.js on my machine. It was surprisingly easy, thanks to the great docs Node.js has. Next, I decided to play around and start debugging it.

我首先必须在计算机上构建Node.js。 得益于Node.js 出色的文档 ,这非常容易。 接下来,我决定尝试一下并开始调试它。

I’d be lying if I said it was smooth sailing from the start. The last time I worked with C++ was back in 2012. I was rusty. Moreover, back then I had a completely different environment than I have now. I had a Windows PC with Visual Studio on it, while now I am running VSCode on a Mac.

如果我说从一开始就航行顺利,我会撒谎。 我上一次使用C ++的时间是在2012年。我很生锈。 而且,那时我的环境与现在完全不同。 我有一台装有Visual Studio的Windows PC,而现在我在Mac上运行VSCode。

I love VSCode so I wanted to setup VSCode for this project too. I soon found an extension and configured things to work. For my debugging configuration, I ended up setting up a node debugger and a lldb debugger to attach to the Node process. That worked great.

我喜欢VSCode,所以我也想为此项目设置VSCode。 我很快找到了一个扩展,并配置了某些功能。 对于我的调试配置,我最终设置了一个节点调试器和一个lldb调试器以附加到Node进程。 效果很好。

正在处理实际问题! (Working on an actual issue!)

So Benji connected me with Anna, who’s the Node.js core collaborator who implemented ‘worker_threads’. Benji also pointed me at this issue. I looked at the issue and tried reproducing it with as little code as possible, just to get rid of the noise.

因此,本吉将我与实现了worker_threads的Node.js核心协作者Anna联系 起来 。 本吉也指出我这个问题 。 我研究了这个问题,并尝试使用尽可能少的代码来重现它,以消除噪音。

I struggled with creating a test case that reproduced the issue, since it was caused by a race condition. The code that failed when running inside Node.js wouldn’t fail in my testing environment. Eventually, I found something that failed on all of my runs. Although it might not fail on every machine, or every time, Anna confirmed it was good enough. Next, I started debugging it to see what was actually happening there.

我努力创建一个重现该问题的测试用例,因为它是由竞争状况引起的。 在Node.js中运行时失败的代码在我的测试环境中不会失败。 最终,我发现所有运行都失败了。 尽管它可能不会在每台机器上或每一次机器上都失败,但是Anna确认它已经足够好。 接下来,我开始对其进行调试,以查看实际发生的情况。

If you’ve never heard of ‘worker threads’, that’s probably because they are quite new and are currently in an experimental state. Workers let you create multiple environments running on independent threads. They are useful for performing CPU-intensive JavaScript operations, without blocking the main thread.

如果您从未听说过“工人线程”,那可能是因为它们很新并且目前处于试验状态。 使用工作程序,您可以创建在独立线程上运行的多个环境。 它们对于执行CPU密集型JavaScript操作非常有用,而不会阻塞主线程。

The main thread and the worker thread can communicate with each other through a message channel between them. In addition to this message channel, there is another message channel where internal messages are sent, such as stdout of the worker. When you console.log inside the worker, it arrives to the main thread through this internal message channel and the main thread handles it by pushing it to its stdout stream.

主线程和工作线程可以通过它们之间的消息通道相互通信。 除此消息通道外,还有另一个消息通道,在该消息通道中发送内部消息,例如worker的stdout。 当您在worker内部使用console.log ,它通过此内部消息通道到达主线程,并且主线程通过将其推送到其stdout流来对其进行处理。

The problem was that we were calling the kDispose function in the JS worker class before waiting for all messages from worker’s stdio to be processed by the main thread through the internal message port. So when the worker thread finished, we lost the references for the parent side stdio streams, and a message to the parent could possibly arrive after that.

问题在于,我们在等待工作线程的stdio的所有消息由主线程通过内部消息端口处理之前,在JS worker类中调用了kDispose函数。 因此,当工作线程完成时,我们将丢失父端stdio流的引用,并且在此之后可能会到达父消息。

At first, I tried lots of different approaches to getting this fixed, including setting a promise to be resolved when the message port was done, awaiting it before disposing, and passing JS callbacks to the C++ layer.

最初,我尝试了许多不同的方法来解决此问题,包括设置一个承诺,以便在完成消息端口时解决该承诺,在处理之前等待它,并将JS回调传递给C ++层。

Chatting with Anna about it revealed to me that a drain method existed for the MessagePort and it emitted all its incoming messages synchronously. So in the end, all the messages from it would be processed. In fact, drain was already called for the external MessagePort. How hadn’t I seen this function all this time? ? I added a call to drain also on the internal MessagePort. The fix was that simple.

与Anna聊天时向我透露,MessagePort存在一种耗用方法,并且它同步地发出所有传入的消息。 所以最后,所有来自它的消息都会被处理。 事实上, 排水已经呼吁外部MessagePort。 我怎么一直没有看到这个功能? ? 我还在内部MessagePort上添加了对d rain的调用。 解决方法就是这么简单。

An important thing to remember is — it’s totally fine trying out weird approaches along the way. That’s how you learn. And after debugging lots of worker_threads code, I can say that I know some of its codebase pretty well now :)

要记住的重要一点是–沿途尝试怪异的方法是完全可以的。 那就是你学习的方式。 在调试了很多worker_threads代码之后,我可以说我现在非常了解它的一些代码库:)

Benji and Anna were so welcoming right from the beginning. This was a great experience. I learned a lot from Anna and from the code, which was very challenging. It’s definitely not something I usually deal with in my day-to-day.

Benji和Anna从一开始就非常热情。 这是一次很棒的经历。 我从Anna和代码中学到了很多东西,这非常具有挑战性。 绝对不是我每天通常要处理的事情。

I can’t wait to work on my next issue!

我等不及要处理下一个问题了!

翻译自: https://www.freecodecamp.org/news/what-i-learned-from-my-first-contribution-to-node-js-core-9cdc0e0d5efc/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值