繁体   English   中英

带有Restify的Node.js如何处理并发请求?

[英]How Nodejs with Restify Handle Concurrent Request?

使用以下代码:

var counter = 0;
server.get(
    '/test,
    function(request, response, next)
    {
        console.log('Counter', ++counter);
        next();
    }
);

几个并行连接如何影响计数器变量? Restify(或Node)是否有某种类型的连接异常或传入请求队列?

提前谢谢了。

实际上, restify封装了以下几种不同的软件包之一: spdyhttphttps

if (options.spdy) {
    this.spdy = true;
    this.server = spdy.createServer(options.spdy);
} else if ((options.cert || options.certificate) && options.key) {
    this.ca = options.ca;
    this.certificate = options.certificate || options.cert;
    this.key = options.key;
    this.passphrase = options.passphrase || null;
    this.secure = true;

    this.server = https.createServer({
        ca: self.ca,
        cert: self.certificate,
        key: self.key,
        passphrase: self.passphrase,
        rejectUnauthorized: options.rejectUnauthorized,
        requestCert: options.requestCert,
        ciphers: options.ciphers
    });
} else if (options.httpsServerOptions) {
    this.server = https.createServer(options.httpsServerOptions);
} else {
    this.server = http.createServer();
}

来源: https : //github.com/restify/node-restify/blob/5.x/lib/server.js

这些包管理请求的异步性质,这些请求在restify作为Events处理。 EventListener按注册顺序同步调用所有侦听器。 在这种情况下, restify是侦听器,并将按照接收到的顺序处理请求。

缩放比例

话虽如此,像restify这样的Web服务器通常通过在诸如nginx类的代理后面的多个进程上释放它们来扩大规模。 在这种情况下, nginx将在进程之间分配传入的请求,从而有效地允许Web服务器处理更大的并发负载。

Node.js的局限性

最后,请记住,这全部受Node.js行为的限制。 由于该应用程序在单个线程上运行,因此您可以在执行缓慢的同步请求时有效地阻止所有请求。

server.get('/test', function(req, res, next) {
    fs.readFileSync('something.txt', ...) // blocks the other requests until done
});

暂无
暂无

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

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