Node Server crashes after few hours

2020-02-09 18:03发布

问题:

I am using Nodemon with Forever Module on Ubuntu Server.

I use this command to start my Node Server:

forever start -c nodemon app.js  --exitcrash

It works fine for few hours (approx 48 Hours), but after that my Server stops working with these errors:

Error: getaddrinfo EMFILE   
TypeError: Cannot call method 'indexOf' of undefined
Error: Handshake inactivity timeout

These errors are caused due to Exceeding Limit of Open Files/Sockets.

Now my question is:

Can I use -m (Which sets to unlimited in my Operating System):

max memnory size   (kbytes, -m) unlimited

Should I use the above command with -m? Are there any drawbacks?

Or is there any other efficient solution to fix Server crashing?

回答1:

If you have large number of users most probably you are hitting systems maximum number of requests queued to listen socket. If you are sure your server can handle the load you can increase from default 128 to something 1024.

And yes, increase the ulimit, so system can handle more load, but don't set to unlimited, just check what is enough to handle current load.

Also go through this Increasing the maximum number of tcp/ip connections in linux will get some helpful info too



回答2:

This is probably not the ideal answer but using forever-service with nodemon will ensure your server restarts after it crashes.

This is the command that worked for me. I'm including it because getting forever-service and nodemon to play well can be tricky.

It does the following: everytime a json or raml file in the applications dist/assets folder is modified, wait 10 seconds and then restart the node app (server.js script):

$ forever-service install raml --script server.js -f " -c nodemon" -o " --delay 10 --watch dist/assets -e json,raml --exitcrash" -e "PATH=/usr/local/bin:$PATH"

Then I can run:

$ service raml start|stop|restart|status

I can also have the service start on server reboot with the chkconfig utility:

$ chkconfig --add raml
$ chkconfig raml on