Nodejs mkdirP module causes high CPU usage, nodejsmkdirp

Source: Internet
Author: User
Tags high cpu usage

Nodejs mkdirP module causes high CPU usage, nodejsmkdirp
Nodejs mkdirP module causes high CPU usage

When I recently deployed the node. js project on the server and started it, I found that the cpu usage of the node process was around 40%. At that time, I was very puzzled, the newly started service does not run any logic that requires a lot of cpu consumption. At this time, no requests have been sent to the server.

In view of this situation, we can only guess that an exception occurs when a program initializes something, which leads to this situation.

After checking the code, the code block that is finally locked out is as follows:

router.use(multer({    dest: config.uploadDir,    limits:{        fileSize : config.fileSizeLimit, //bytes , == 50M        files : 1    },    rename:function(fieldname, filename) {        return uuid.v1() + "_" + filename + "_" + Date.now();    },    onFileUploadStart: function (file) {        console.log(file.originalname + ' is starting ...');    },    onError: function (error, next) {        winston.error(error);        next(error);    }}));

I don't believe this code will occupy such a high CPU at first, so the biggest suspect is the initialization of the multer module, so I will go to the source code to see it.

Multer initialization source code:

  var dest;  if (options.dest) {    dest = options.dest;  } else {    dest = os.tmpdir();  }  mkdirp(dest, function(err) { if (err) throw err; });  var rename = options.rename || function(fieldname, filename) {    var random_string = fieldname + filename + Date.now() + Math.random();    return crypto.createHash('md5').update(random_string).digest('hex');  };

This code is most suspected of being mkdirP (creating the target folder) or crypto md5, so one by one exclusion. First, just comment out mkdirP, restart node, and find that the cpu is basically 0, so we can conclude that the problem lies in mkdirP.

Simply use this sentenceMkdirp (dest, function (err) {if (err) throw err ;});You can't see anything, so you have to continue to implement mkdirP.

The source code of the mkdirP folder is as follows:

Function mkdirP (p, mode, f, made) {// a few lines of fs. mkdir (p, mode, function (er) {if (! Er) {made = made | p; return cb (null, made);} switch (er. code) {case 'enoent': mkdirP (path. dirname (p), mode, function (er, made) {if (er) cb (er, made); else mkdirP (p, mode, cb, made );}); break; default: fs. stat (p, function (er2, stat) {if (er2 |! Stat. isDirectory () cb (er, made) else cb (null, made) ;}); break ;}});

When we see the case 'enoent', it is a little clue. After the folder fails to be created, we call mkdirP again. In this way, if there is a problem for the first time, we won't keep trying to create it. If so, it must be because of a problem with the input path. When I check the path in config, I find that the path is reversed by the forward slash when deploying the Configuration:

config.path = 'c:\aa\aa';

Which of the following statements is true:

config.path = 'c:\\aa\\aa';orconfig.path = 'c://aa//aa';

After several twists and turns, the problem was finally solved. nodejs still had a long way to go, go, eat ......

Author: forevercjl
Article link: http://blog.csdn.net/ForeverCjl/article/details/45895795
Indicate the source for reprinting.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.