更改端口而不丢失数据

发布于 2024-10-27 09:15:48 字数 1264 浏览 1 评论 0原文

我正在为我的 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);

资源:

http://nodejs.org/docs/v0.4.4/api/http.html#server.close

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:

http://nodejs.org/docs/v0.4.4/api/http.html#server.close

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

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

发布评论

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

评论(3

屌丝范 2024-11-03 09:15:48

我测试了 close() 函数。它似乎什么也没做。服务器仍然接受其端口上的连接。重新启动该过程是我的唯一方法。

我使用了以下代码:

var http = require("http");

var server = false;
function OnRequest(req,res){    
    res.end("server now listens on port "+8889);
    CreateServer(8889);
}
function CreateServer(port){
    if(server){
        server.close();
        server = false;
    }
    server = http.createServer(OnRequest);
    server.listen(port);
}
CreateServer(8888);

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:

var http = require("http");

var server = false;
function OnRequest(req,res){    
    res.end("server now listens on port "+8889);
    CreateServer(8889);
}
function CreateServer(port){
    if(server){
        server.close();
        server = false;
    }
    server = http.createServer(OnRequest);
    server.listen(port);
}
CreateServer(8888);
許願樹丅啲祈禱 2024-11-03 09:15:48

当我决定彻底测试我的代码以查看它是否确实是一个错误(我讨厌在用户错误时提交错误报告)时,我正准备在节点 github 页面上提交一个问题。我意识到这个问题只在浏览器中表现出来,因为显然浏览器会执行一些奇怪的 HTTP 请求保持活动状态,它仍然可以访问死端口,因为仍然与服务器有连接。

我了解到的是:

  • 浏览器缓存使端口保持活动状态,除非服务器上的进程被
  • 终止默认情况下不保留缓存的实用程序(curl、wget 等)按
  • 节点中预期的 HTTP 请求工作,也不会保持相同的状态浏览器执行的缓存类型

例如,我使用此代码来证明节点 http 客户端无法访问旧端口:

客户端代码:

var http = require('http'),
    client,
    request;

function createClient (port) {
  client = http.createClient(port, 'localhost');

  request = client.request('GET', '/create');
  request.end();

  request.on('response', function (response) {
      response.on('end', function () {
          console.log("Request ended on port " + port);
          setTimeout(function () {
              createClient(port);
          }, 5000);
      });
  });
}

createClient(8888);

和服务器端代码:

var http = require("http");

var server,
    curPort = 8888;

function CreateServer(port){
    if(server){
        server.close();
        server = undefined;
    }

    curPort = port;

    server = http.createServer(function (req, res) {
        res.end("You are on port " + curPort);
        if (req.url === "/create") {
            CreateServer(curPort);
        }
    });
    server.listen(curPort);
    console.log("Server listening on port " + curPort);
}
CreateServer(curPort);

感谢大家的响应。

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:

  • Browser caches keep ports alive unless the process on the server is killed
  • Utilities that do not keep caches by default (curl, wget, etc) work as expected
  • HTTP requests in node also don't keep the same type of cache that browsers do

For example, I used this code to prove that node http clients don't have access to old ports:

Client-side code:

var http = require('http'),
    client,
    request;

function createClient (port) {
  client = http.createClient(port, 'localhost');

  request = client.request('GET', '/create');
  request.end();

  request.on('response', function (response) {
      response.on('end', function () {
          console.log("Request ended on port " + port);
          setTimeout(function () {
              createClient(port);
          }, 5000);
      });
  });
}

createClient(8888);

And server-side code:

var http = require("http");

var server,
    curPort = 8888;

function CreateServer(port){
    if(server){
        server.close();
        server = undefined;
    }

    curPort = port;

    server = http.createServer(function (req, res) {
        res.end("You are on port " + curPort);
        if (req.url === "/create") {
            CreateServer(curPort);
        }
    });
    server.listen(curPort);
    console.log("Server listening on port " + curPort);
}
CreateServer(curPort);

Thanks everyone for the responses.

爱,才寂寞 2024-11-03 09:15:48

使用集群怎么样?

http://learnboost.github.com/cluster/docs/reload.html

看起来很有趣!

What about using cluster?

http://learnboost.github.com/cluster/docs/reload.html

It looks interesting!

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