- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
更新 4:通过在函数外部实例化 restify 客户端(参见 controllers/messages.js)并在每次请求后调用 global.gc() ,内存增长率似乎已降低很多(每 10 秒约 500KB)。然而,内存使用量仍在不断增长。
更新 3:遇到这篇文章:https://journal.paul.querna.org/articles/2011/04/05/openssl-memory-use/
可能值得注意的是,我将 HTTPS 与 Restify 结合使用。
更新 2:将下面的代码更新为当前状态。我试过用 Express 替换 Restify。遗憾的是,这没有任何区别。似乎链末端的 api 调用(restify -> mongodb -> 外部 api)导致所有内容都保留在内存中。
更新 1:我已将 Mongoose 替换为标准的 MongoDB 驱动程序。内存使用量似乎增长得不那么快,但泄漏仍然存在。
这几天我一直在努力寻找这个漏洞。
我正在使用 Restify 运行 API和 Mongoose对于每个 API 调用,我至少进行一次 MongoDB 查找。我有大约 1-2k 用户在一天内多次访问 API。
我尝试过的
那么,可能出了什么问题?
我只使用一个 API 用户完成了上述测试。这意味着 Mongoose 只会一遍又一遍地抓取相同的文档。与生产环境的不同之处在于,许多不同的用户访问了 API,这意味着 mongoose 获得了很多不同的文档。
当我启动 nodejs 服务器时,内存迅速增长到 100MB-200MB。它最终稳定在 500MB 左右。这是否意味着它会泄漏每个用户的内存?一旦每个用户都访问过 API,它就会稳定下来吗?
我在下面包含了我的代码,其中概述了我的 API 的一般结构。我很想知道我的代码中是否存在严重错误,或者是否有任何其他方法可以找出导致高内存使用率的原因。
代码
app.js
var restify = require('restify');
var MongoClient = require('mongodb').MongoClient;
// ... setup restify server and mongodb
require('./api/message')(server, db);
api/message.js
module.exports = function(server, db) {
// Controllers used for retrieving accounts via MongoDB and communication with an external api
var accountController = require('../controllers/accounts')(db);
var messageController = require('../controllers/messages')();
// Restify bind to put
server.put('/api/message', function(req, res, next) {
// Token from body
var token = req.body.token;
// Get account by token
accountController.getAccount(token, function(error, account) {
// Send a message using external API
messageController.sendMessage(token, account.email, function() {
res.send(201, {});
return next();
});
});
});
};
controllers/accounts.js
module.exports = function(db) {
// Gets account by a token
function getAccount(token, callback) {
var ObjectID = require('mongodb').ObjectID;
var collection = db.collection('accounts');
collection.findOne({
token: token
}, function(error, account) {
if (error) {
return callback(error);
}
if (account) {
return callback('', account);
}
return callback('Account not found');
});
}
};
controllers/messages.js
module.exports = function() {
function sendMessage(token, email, callback) {
// Get a token used for external API
getAccessToken(function() {}
// ... Setup client
// Do POST
client.post('/external_api', values, function(err, req, res, obj) {
return callback();
});
});
}
return {
sendMessage: sendMessage
};
};
疑似泄漏的堆快照
最佳答案
可能是 getter 中的错误,我在为 mongoose 模式使用虚拟或 getter 时得到它 https://github.com/LearnBoost/mongoose/issues/1565
关于JavaScript 内存泄漏 (Node.js/Restify/MongoDB),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22576402/
我在一个节点 js 服务器上工作,并使用 bluebird 来实现 promise 。我了解如何使用 promise ,但我的问题是如何处理 promise 返回的错误。我尝试了简单的解决方案 - 只
我是开发 Web 服务的初学者,我有一个带有 node js 和模块 restify 的服务器,我想使用模块 OAuth2-restify 添加身份验证。但是当我努力准备获取 token 的请求时。我
我正在使用 REST 架构来创建在 Restifyjs 中创建的 Web 应用程序,其前端是在 Angular JS 中,两者都在不同的域上运行。例如,我的本地 Restify 正在 locahost
也在官方 Restify repo 中询问:#1224 你好, 是否有可能有一个默认格式化程序可以处理任何未定义的接受类型。 例如: restify.createServer({ format
我正在为 Node 使用 restify 来创建一个简单的 API。我想要一个目录 /public,人们可以在其中简单地浏览到该目录并下载他们浏览到的文件。 为了完成这个,我在/routes/publ
我正在尝试创建一个 REST API 服务器,它可以使用 node.js restify 接受图像上传。我在 http://restify.com/ 查看了文档但无法确定 restify 是否支持作为
我是 javascript 世界的新手,我正在尝试创建一个“基本”RESTful 应用程序。到目前为止,我发现 restify 框架似乎是完成此任务的最佳框架之一,因为 Express 也更倾向于处理
我想安装 restify在我的 Windows 机器上使用 npm。 出现错误,无法安装。 查看日志文件时出错 info dtrace-provider@0.2.8 Failed to exec in
我对 Restify 中客户端的目的感到困惑: http://mcavage.me/node-restify/#JsonClient 它们本身是基于 Node.js 构建的,就像服务器部分一样。我是否
来自 express ,我有这样的路线 server.get('/some/route/*', function(req, res) { // accessing wildcard c
我正在尝试使用 node.js restify 编写 Restful 应用程序。这是我的应用程序代码: var restify = require('restify'); var server = r
目前我使用 Node 模块 jwtRestify 进行身份验证。我有一个问题,我想从身份验证中排除一些路径,这没有问题: server.use(jwtRestify({ secret: config.
我熟悉 gulp 以及在单个配置文件中为每个环境进行不同配置的能力。这样运行 gulp dev 将允许您使用开发环境的配置启动服务器,gulp staging 用于暂存,gulp prod 来回生产。
我正在使用 restify .在 POST 上,它获取一个 JSON 对象并将值存储到数据库中。 现在我还需要上传图片。 场景是:上传图片,将其调整为指定尺寸(3 种类型的缩略图)并将它们全部保存在服
我有一个基于 Node 和 Restify 插件的用 JavaScript 编写的 REST 应用程序。 我设置了一些 REST 函数: var server = restify.createServ
我正在使用这里的restify-oauth2存储库: https://github.com/domenic/restify-oauth2 在自述文件中提到了 tokenExpirationTime 但
这就是当我尝试让 Facebook 为我的机器人验证我的 URL 时得到的结果。我知道这是一个数据类型问题。我发送回一个字符串而不是一个整数。我的代码如下所示: server.get("/webhoo
我懂了 TypeError: Cannot read property 'status' of undefined 当尝试使用 supertest 将文件上传到简单的 Restify 服务器时,并且打
我的以下代码不起作用: var server = restify.createServer( ... ); server .get('/', function(req,res,next){ res
我正在尝试使用以下代码通过 Restify 提供静态文件。 app.get(/.*/, restify.serveStatic({ directory: __dirname + '/publi
我是一名优秀的程序员,十分优秀!