0

我正在为我的 http 服务器构建一个设置管理器。我希望能够更改设置而不必终止整个过程。我希望能够更改的设置之一是更改端口号,我想出了多种解决方案:

  • 杀死进程并重新启动它
  • 调用 server.close() 然后做第一种方法
  • 调用 server.close() 并在同一进程中初始化一个新服务器

问题是,我不确定每种方法的影响是什么。我知道第一个会起作用,但我真的很想完成这些事情:

  • 响应现有请求而不接受新请求
  • 在新服务器的内存中维护数据
  • 损失尽可能少的正常运行时间

有什么办法可以得到我想要的一切吗?server.close() 的 API 给了我希望: server.close(): Stops the server from accepting new connections.

我的服务器只能由我创建的客户端和通过浏览器连接的数量非常有限的客户端访问,因此我将能够通知他们端口更改。我知道更改端口通常是一个坏主意,但我想考虑方便或可能需要的边缘情况。

PS如果改变任何东西,我正在使用连接。

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

4

3 回答 3

3

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);
于 2011-03-29T15:58:42.067 回答
3

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.

于 2011-03-29T18:14:47.250 回答
0

What about using cluster?

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

It looks interesting!

于 2011-03-29T16:06:24.313 回答