简体   繁体   English

重新启动实时的node.js服务器而不关闭套接字?

[英]Restarting a realtime node.js server without closing sockets?

We have a small set of multiplayer servers using node.js that are currently serving roughly 1 million messages a minute during peak usage. 我们有一小组使用node.js的多人服务器,目前在高峰使用期间每分钟提供大约一百万条消息。 Is there a way to 'gracefully' restart the server without causing sockets to drop? 有没有一种方法可以“优雅地”重新启动服务器而不会导致套接字丢失? Basically, I'm wondering what is the best way to handle restarts were it would normally be very disruptive to players? 基本上,我想知道处理重启的最佳方法是什么,因为这通常会对玩家造成极大的破坏吗?

use redis or some in-memory database for storing connection so that you can easily reconnect even after server restart without loosing any sessions or connection. 使用redis或一些内存数据库来存储连接,以便即使服务器重新启动后也可以轻松地重新连接,而不会丢失任何会话或连接。 Try this if it suits your need. 如果适合您,请尝试此。 Also please note during restart connection may drop but due to having persistence you will be connected again very easily. 另外请注意,重新启动期间连接可能会断开,但是由于具有持久性,您将非常容易地再次连接。 socket.io-redis 套接字

When a process exits, the OS cleans up any sockets that belong to it by closing them. 当进程退出时,操作系统会通过关闭它们来清理属于它的所有套接字。 So, there's no way to just do a simple server restart and preserve your socket connections. 因此,无法简单地重新启动服务器并保留套接字连接。

In some operating systems, you can pass ownership of a socket from one process to another so it might be technically feasible for you to create a temporary process or perhaps a previously existing parent process), pass ownership of the sockets to that other process, restart your server, then transfer ownership back to the newly started process. 在某些操作系统中,您可以将套接字的所有权从一个进程传递给另一个进程,因此从技术上讲,创建一个临时进程或以前存在的父进程可能是可行的,将套接字的所有权传递给另一个进程,然后重新启动您的服务器,然后将所有权转移回新启动的进程。 I've never tried this (or heard about it being done), but it sounds like something that might be feasible. 我从未尝试过(或听说它已经完成),但听起来可能是可行的。

Here's some information on transferring a socket to a child process using child.send() in node.js. 以下是有关使用node.js中的child.send()将套接字传输到子进程的一些信息 It appears this can only be done for a node.js socket created by the net module and there are some caveats about doing it, but it is possible. 似乎只能对net模块创建的node.js套接字执行此操作,并且对此有一些注意事项,但有可能。

If not, the usual work-around is have the clients automatically reconnect when their connection is closed. 如果不是,通常的解决方法是让客户端在关闭连接时自动重新连接。 Done properly, this can be fairly transparent to the client (except for the momentary time when the server is not running). 正确完成后,这对客户端可能是相当透明的(服务器未运行时的瞬时时间除外)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM