更改端口而不丢失数据
我正在为我的 http 服务器构建一个设置管理器。我希望能够更改设置而不必终止整个过程。我希望能够更改的设置之一是更改端口号,我想出了多种解决方案:
- 终止进程并重新启动它
- 调用 server.close() 然后执行第一种方法
- 调用服务器.close() 并在同一进程中初始化一个新服务器
问题是,我不确定每种方法的影响是什么。我知道第一个会起作用,但我真的很想完成这些事情:
- 响应现有请求而不接受新请求
- 在新服务器上的内存中维护数据
- 损失尽可能少的正常运行时间
有没有办法获得我想要的一切? server.close() 的 API 给了我希望: server.close():停止服务器接受新连接。
我的服务器只能由我创建的客户端以及通过浏览器连接的极少数客户端访问,因此我将能够通知他们端口更改。我知道更改端口通常是一个坏主意,但我想允许方便或可能需要的边缘情况。
PS如果这改变了什么,我正在使用 connect 。
PPS 相对不相关,但是如果我使用 UNIX 服务器套接字或更改主机名,会发生什么变化?这可能是一个更相关的用例。
PPPS 此代码说明了使用 server.close() 的问题。以前的服务器都没有被杀死,但更多的服务器被创建来访问相同的资源...
var http = require("http");
var server = false,
curPort = 8888;
function OnRequest(req,res){
res.end("You are on port " + curPort);
CreateServer(curPort + 1);
}
function CreateServer(port){
if(server){
server.close();
server = false;
}
curPort = port;
server = http.createServer(OnRequest);
server.listen(curPort);
}
CreateServer(curPort);
资源:
I'm building a settings manager for my http server. I want to be able to change settings without having to kill the whole process. One of the settings I would like to be able to change is change the port number, and I've come up with a variety of solutions:
- Kill the process and restart it
- Call server.close() and then do the first approach
- Call server.close() and initialize a new server in the same process
The problem is, I'm not sure what the repercussions of each approach is. I know that the first will work, but I'd really like to accomplish these things:
- Respond to existing requests without accepting new ones
- Maintain data in memory on the new server
- Lose as little uptime as possible
Is there any way to get everything I want? The API for server.close() gives me hope:server.close(): Stops the server from accepting new connections.
My server will only be accessible by clients I create and by a very limited number of clients connecting through a browser, so I will be able to notify them of a port change. I understand that changing ports is generally a bad idea, but I want to allow for the edge-case where it is convenient or possibly necessary.
P.S. I'm using connect if that changes anything.
P.P.S. Relatively unrelated, but what would change if I were to use UNIX server sockets or change the host name? This might be a more relevant use-case.
P.P.P.S. This code illustrates the problem of using server.close(). None of the previous servers are killed, but more are created with access to the same resources...
var http = require("http");
var server = false,
curPort = 8888;
function OnRequest(req,res){
res.end("You are on port " + curPort);
CreateServer(curPort + 1);
}
function CreateServer(port){
if(server){
server.close();
server = false;
}
curPort = port;
server = http.createServer(OnRequest);
server.listen(curPort);
}
CreateServer(curPort);
Resources:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我测试了 close() 函数。它似乎什么也没做。服务器仍然接受其端口上的连接。重新启动该过程是我的唯一方法。
我使用了以下代码:
I tested the close() function. It seems to do absolute nothing. The server still accepts connections on his port. restarting the process was the only way for me.
I used the following code:
当我决定彻底测试我的代码以查看它是否确实是一个错误(我讨厌在用户错误时提交错误报告)时,我正准备在节点 github 页面上提交一个问题。我意识到这个问题只在浏览器中表现出来,因为显然浏览器会执行一些奇怪的 HTTP 请求保持活动状态,它仍然可以访问死端口,因为仍然与服务器有连接。
我了解到的是:
例如,我使用此代码来证明节点 http 客户端无法访问旧端口:
客户端代码:
和服务器端代码:
感谢大家的响应。
I was about to file an issue on the node github page when I decided to test my code thoroughly to see if it really is a bug (I hate filing bug reports when it's user error). I realized that the problem only manifests itself in the browser, because apparently browsers do some weird kind of HTTP request keep alive thing where it can still access dead ports because there's still a connection with the server.
What I've learned is this:
For example, I used this code to prove that node http clients don't have access to old ports:
Client-side code:
And server-side code:
Thanks everyone for the responses.
使用集群怎么样?
http://learnboost.github.com/cluster/docs/reload.html
看起来很有趣!
What about using cluster?
http://learnboost.github.com/cluster/docs/reload.html
It looks interesting!