Nodejs MKDIRP module causes high CPU consumption

Source: Internet
Author: User

Nodejs MKDIRP module causes high CPU consumption

The recent deployment of the NODEJS project to the server and startup, found that the node process CPU utilization of about 40%, at that time expressed very puzzled, the service just started does not run what requires a lot of CPU consumption of logic, and at this time there is no request to send to the server side.

In this case, it can only be guessed that a certain program is initializing something, which is unusual, and that is why this is the case.

After troubleshooting the code, the code block that eventually locks out the title is as follows:

Router.use (Multer ({dest:config.uploadDir, limits:{fileSize:config.fileSizeLimit,//bytes, = = 50MFiles:1}, rename: function(fieldname, filename) {        returnUUID.V1 () +"_"+ filename +"_"+Date. Now (); }, Onfileuploadstart: function (file) {Console.log (File.originalname +' is starting ... '); }, OnError: function (Error, next) {Winston.error (Error);    Next (Error); }}));

At first look at this code how also do not believe that it will occupy such a high CPU, so the biggest suspicion is Multer module initialization, and then enter the source code to see.

Multer initializing part of source code:

  varDestif(options.dest) {dest = Options.dest; }Else{dest = Os.tmpdir (); } mkdirp (Dest, function(err) { if(ERR)ThrowErr });varRename = Options.rename | | function(fieldname, filename) {    varrandom_string = fieldname + filename +Date. Now () +Math. random ();returnCrypto.createhash (' MD5 '). Update (random_string). Digest (' Hex '); };

The most suspect of this code is not MKDIRP (create target folder) is crypto MD5, then one to exclude. First, just comment out Mkdirp, restart node, found that the CPU is basically 0, so it can be concluded that the problem is on the mkdirp.

This sentence mkdirp (dest, function (err) {if (err) throw err;}); Can not see anything, so still have to continue into the realization of MKDIRP.

Mkdirp in the source code for creating a folder:

 function mkdirp (p, mode, F, made) {    //Omit a few lines hereFs.mkdir (p, mode, function (er) {        if(!er) {made = made | | p;returncbNULL, made); }Switch(Er.code) { Case ' ENOENT ': Mkdirp (Path.dirname (p), mode, function (er, made) {                    if(er) CB (er, made);ElseMKDIRP (p, mode, CB, made); }); Break;default: Fs.stat (P, function (ER2, stat) {                    if(Er2 | |!stat.isdirectory ()) CB (ER, made)ElsecbNULL, made); }); Break; }    });

See case ' ENOENT ' when a bit of a clue, in the creation of a folder after the failure, and then tune the MKDIRP, so if the first problem, it is not kept trying to create. If so, then there must be a problem with the incoming path. When we look back at the path in config, we find that the path of the slash is reversed when the configuration is deployed:

‘c:\aa\aa‘;

The correct one should be:

‘c:\\aa\\aa‘‘c://aa//aa‘;

After several setbacks finally solved the problem, Nodejs Road is very long, go, eat to ...

Article FOREVERCJL
Article original link: http://blog.csdn.net/ForeverCjl/article/details/45895795
Reprint please indicate the source.

Nodejs MKDIRP module causes high CPU consumption

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.