简体   繁体   English

努力寻找 memory 泄漏。 NodeJS,快递,MongoDB

[英]Struggling to find memory leak. NodeJS, Express, MongoDB

so I have a NodeJS with Express configured for API calls to get data from my backend Database.所以我有一个带有 Express 配置的 NodeJS,用于 API 调用以从我的后端数据库获取数据。

Everything works great except there is a memory leak that I can't seem to solve no matter what I do.一切都很好,除了有一个 memory 泄漏,无论我做什么我似乎都无法解决。 I have narrowed it down to a MongoDB fetch call, that grabs data from MongoDB.我已将其缩小为 MongoDB 获取调用,它从 MongoDB 获取数据。

Since the Data being sent is the same, and to avoid multiple requests to MongoDB, I created a top level variable that fetches that data so on a request it sends that, vs doing a fetch constantly as it would be 1000's of requests every minute.由于发送的数据是相同的,并且为了避免对 MongoDB 的多个请求,我创建了一个顶级变量来获取该数据,因此在它发送的请求上,而不是不断地进行获取,因为每分钟有 1000 个请求。

I have also set the --max_old_space_size variable to 4096 and higher at 8192 and will eventually crash also.我还将 --max_old_space_size 变量设置为 4096 和更高的 8192,最终也会崩溃。

Below is the fetch code.下面是获取代码。

//router get apis.js //路由器获取apis.js

 import { Model } from '../../dbmodels/models.js';

 let data = null;

// Bot interval
 setInterval(async () => {
     try {
         data = await tokenModel.find({
             $or: [
                 { "currentRanks.minuteTokenRank": {$lt: 51} },
                 { "currentRanks.fiveMinuteTokenRank": {$lt: 51} },
                 { "currentRanks.fifteenMinuteTokenRank": {$lt: 51} },
                 { "currentRanks.thirtyMinuteTokenRank": {$lt: 51} },
                 { "currentRanks.hourlyTokenRank": {$lt: 51} },
                 { "currentRanks.dailyTokenRank": {$lt: 51} },
                 { "currentRanks.weeklyTokenRank": {$lt: 51} }
             ]
         }).lean();
     } catch (error) {
       console.error(error);
       return;
     }
   }, 45000);

export async function main(req, res) {
    let dataRes = data;
    try {
        res.status(200).json(dataRes);
        dataRes = null;
    } catch {(err) => {
        res.status(500).json({ message: err.message })
        console.log('err', err.message)
        }
    }
  //console.log('Get Top Data')
}

//main server.js file //主server.js文件

import dotenv  from "dotenv"
dotenv.config()

import express from 'express';
const app = express();

import { createServer } from 'https';
import { createServer as _createServer } from 'http';

import { readFileSync } from 'fs';

import compression from "compression";

import pkg from 'mongoose';
const { connect, connection } = pkg;
import cors from 'cors';
import auth from './utils/auth.js'

connect(process.env.DB_URL);
let mongoClient = connection;
mongoClient.on('error', (error) => console.error(error));
mongoClient.once('open', () => console.log(`Cncted to DB ${mongoClient.name}`));

app.use(compression());
app.use(cors({ origin: ['http://localhost:3000']}));

// Apis route

import apis from './apis/apis.js';
app.use('/api', auth, apis);

//listen both http & https
const httpServer = _createServer(app);
const httpsServer = createServer({
    key: readFileSync('redacted.pem'),
    cert: readFileSync('redacted.pem'),
}, app);

httpServer.listen(3000, () => console.log('Server Started port 3000'));

httpsServer.listen(3001, () => {
    console.log('HTTPS Server running port 3001')
})

So looks like I was able to find the leak.所以看起来我能够找到泄漏点。 It wasn't with any of the API's I posted.它不是我发布的任何 API。 But a hidden one that I use or Web3.但是我使用的隐藏的还是 Web3。 There is a known bug on the web3 package leaving connections open. web3 package 上存在一个已知错误,导致连接保持打开状态。

This is tied to the data I am retrieving above which is why it seemed to me like it was this API, but further troubleshooting found the real issue.这与我在上面检索的数据有关,这就是为什么在我看来它是这个 API,但进一步的故障排除发现了真正的问题。

Here is the issue in case anyone uses web3 packages and runs into similar.这是万一有人使用 web3 包并遇到类似问题的问题。

https://github.com/web3/web3.js/issues/3042#issuecomment-663622882 https://github.com/web3/web3.js/issues/3042#issuecomment-663622882

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

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