NodeJS堆内存不足

nukf8bse  于 2023-08-04  发布在  Node.js
关注(0)|答案(2)|浏览(136)

使用MailjetApi发送5K电子邮件时出错。

我的编码:

function arraySend(array, locals) {
    var mailjetConnect = mailjet.connect(locals.blocks.mailjetparam.username, locals.blocks.mailjetparam.password),
        chunkedArrayMessages = _.chunk(array, 50),
        delay = ms => new Promise(resolve => setTimeout(resolve, ms)),
        promises;

    if (process.env.NODE_ENV.trim() === 'dev') {
      return Promise.resolve();
    } else {
      promises = chunkedArrayMessages.map((messages) => {
        return Promise.all([
          delay(1000),
          mailjetConnect.post('send', { version: 'v3.1' }).request({ Messages: messages }).then(() => {
            return Promise.resolve();
          }).catch(() => {
            return Promise.resolve();
          })
        ]);
      });
      return Promise.all(promises).then(function() {
        return Promise.resolve();
      }).catch(function(err) {
        return Promise.reject(err);
      });
    }
  }

字符串

错误:

<--- Last few GCs --->
  [30337:0x51226d0]    59607 ms: Mark-sweep (reduce) 4085.3 (4103.8) -> 4085.1 (4107.6) MB, 71.7 / 0.1 ms  (+ 79.5 ms in 40 steps since start of marking, biggest step 78.8 ms, walltime since start of marking 255 ms) (average mu = 0.852, current mu = 0.779) [30337:0x51226d0]    59810 ms: Mark-sweep (reduce) 4087.0 (4108.0) -> 4086.9 (4109.0) MB, 119.3 / 0.1 ms  (average mu = 0.781, current mu = 0.411) allocation failure scavenge might not succeed
  <--- JS stacktrace --->
  FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memor


我的NodeJS应用程序使用PM2和process.json启动。

启动NodeJS应用的命令:sudo pm2 start process.json --node-args="--max-old-space-size=4096"
我的PM2的process.json:

{apps:[{name:"*****.fr:3006",cwd: "****/mywebsite/cms",script: "./index.js",watch:false, env: {"NODE_ENV": "prod"}}]}

Package.json:

"start": "set NODE_ENV=prod & node index.js",

我的服务器:


的数据

v09wglhw

v09wglhw1#

您可以将批处理大小从5 K减少到1 K,并在Apache Kafka / RabbitMQ中对批处理进行排队。

kokeuurv

kokeuurv2#

由于Mailjet的限制,您将无法绕过异步函数。试试这个方法。你可能需要调整一些部分。

// maybe better in global context (?)
const mailjetConnect = mailjet.connect(locals.blocks.mailjetparam.username, locals.blocks.mailjetparam.password);
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

async function arraySend(array, locals) {
  let chunkedArrayMessages = _.chunk(array, 50);
  let result;

  if (process.env.NODE_ENV.trim() === 'dev') {
    return true; // or false (?)
  }
  
  for (let chunkedArray of chunkedArrayMessages) {
    let messages = [];
    for (let chunk of chunkedArray) {
      messages.push(chunk.messages); // structure of chunkedArrayMessages ?
    }
    result = await mailjet
      .post('send', { version: 'v3.1' })
      .request(messages);
    // result validation !!! 
    await delay(1000);
  }

  return true; // or false (?)
}

字符串

相关问题