开发者

Express.js webserver lasts 5 minutes on production before running out of memory

开发者 https://www.devze.com 2023-03-18 12:16 出处:网络
So I\'ve got a Express.js server setup, but after about 5 minutes on production - http://balupton.com - it crashes and restarts.

So I've got a Express.js server setup, but after about 5 minutes on production - http://balupton.com - it crashes and restarts.

The source code of my website is here: https://github.com/balupton/balupton.docpad/blob/master/server.coffee

The docpad reference it uses to create the server is here: https://github.com/bevry/docpad/blob/master/lib/docpad.coffee#L589

It's hosted on http://no.de and here is the out of memory exception that keeps appearing in the logs every 5 minutes or so:

FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory
[ Jul  7 14:02:13 Stopping because all processes in service exited. ]
[ Jul  7 14:02:13 Executing stop method (:kill). ]
[ Jul  7 14:02:13 Executing start method ("env `cat开发者_运维百科 /home/node/node-service/profile` /opt/nodejs/latest/bin/node /home/node/node-service/releases/20110707135409/server.js &"). ]
[ Jul  7 14:02:13 Method "start" exited with status 0. ]
Express server listening on port 80 and directory /home/node/node-service/releases/20110707135409/out

I've looked everywhere on this, but I've given up. Can anyone help me?


The problem turned out to be with the middlewares. Having the static middleware last meant that when a static file was not found, the static middleware would attempt to send the request to the next middleware - which didn't exist, so it would just keep the request open forever - so very quickly open connections started pilling up (due to spiders crawling non-existant urls) and would crash the server every time it ran out of resources.

The solution was to add a 404 middleware as the last middleware to run like so:

# 404 Middleware
myServerInstance.use (req,res,next) ->
    res.send(404)

This way, when the static middleware doesn't find a file, it sends it on to the next middleware, eventually the 404 middleware will be hit, and send off the 404 rather than keeping the request open forever.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号