在node.js上运行的HTTP服务器中如何仅用一小块空间来表示连接?

发布于 2024-12-07 11:49:36 字数 199 浏览 1 评论 0原文

我读到,在 node.js 中创建的 HTTP 服务器不会为每个传入连接(请求)创建新线程。相反,它执行一个已注册为与接收请求事件相对应的回调的函数。

据说每个连接都由堆中的一些小空间表示。我无法弄清楚这一点。连接不是用套接字表示的吗?是否不应为与 Node.js 服务器建立的每个连接打开套接字,这意味着每个连接不能仅由 javascript 堆中的空间分配来表示?

I have read that a HTTP server created in node.js does not create new threads for each incoming connection(request). Instead it executes a function that has been registered as a callback corresponding to the event of receiving a request.

It is said that each connection is represented by some small space in the heap. I cannot figure this out. Are connections not represented by sockets ? Should sockets not be opened for every connection made to the node.js server and this would mean each connection cannot be represented by just a space allocation in the javascript heap ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放肆 2024-12-14 11:49:36

nodejs.org 网站上描述,服务器不是为每个连接生成线程(每个线程 2mb 开销!),而是使用 select()、epoll、kqueue 或 /dev/poll 来等待套接字准备好读/写。正是这种方法允许节点避免每个连接生成线程,并且开销与连接的套接字描述符的堆分配相关。此实现细节在很大程度上对开发人员隐藏,并且运行时公开的 net.socket API 提供了利用该功能所需的一切,您甚至无需考虑它。

Node 还通过 events.EventEmitter 公开自己的事件 API。许多节点对象实现事件来提供异步(非阻塞)事件通知,这对于 I/O 操作来说是完美的,而在其他语言(例如 PHP)中,I/O 操作默认是同步(阻塞)的。对于节点 net.socket API,处理套接字 I/O 的多个 API 方法会触发事件,并且当事件发生时会触发通过参数传递给这些方法的回调。事件可以通过多种不同的方式将回调函数绑定到它们,接受回调函数作为参数只是为开发人员提供方便。

最后,不要将操作系统事件与 Nodejs 事件混淆。对于网络API,操作系统事件被传递到nodejs运行时,但nodejs事件是javascript。

我希望这有帮助。

It is described on the nodejs.org website that instead of spawning threads (2mb overhead per thread!) per connection, the server uses select(), epoll, kqueue or /dev/poll to wait until a socket is ready to read / write. It is this method that allows node to avoid thread spawning per connection, and the overhead is that associated with the heap allocation of the socket descriptor for the connection. This implementation detail is largely hidden from developers, and the net.socket API exposed by the runtime provides everything you need to take advantage of that feature without even thinking about it.

Node also exposes its own event API through events.EventEmitter. Many node objects implement events to provide asynchronous (non-blocking) event notification, which is perfect for I/O operations, which in other languages - such as PHP - are synchronous (blocking) by default. In the case of the node net.socket API, events are triggered for several API methods dealing with socket I/O, and the callbacks that are passed by parameter to these methods are triggered when an event occurs. Events can have callback functions bound to them in a variety of different ways, accepting a callback function as a parameter is only a convenience for the developer.

Finally, do not confuse OS events with nodejs events. In the case of the net API, OS events are passed to the nodejs runtime, but nodejs events are javascript.

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文