带有 Restify 的 Nodejs 如何处理并发请求?
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
正在包装几个不同的包之一:spdy
、http
或 https
。
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
中作为事件处理。 The EventListener calls all listeners synchronously in the order in which they were registered.。在这种情况下,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
});
使用以下代码:
var counter = 0;
server.get(
'/test,
function(request, response, next)
{
console.log('Counter', ++counter);
next();
}
);
多个并发连接如何影响计数器变量? Restify(或 Node)有某种连接隔离或传入请求队列?
非常感谢。
实际上,restify
正在包装几个不同的包之一:spdy
、http
或 https
。
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
中作为事件处理。 The EventListener calls all listeners synchronously in the order in which they were registered.。在这种情况下,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
});