Search code examples
node.jstcpforever

Tcp server not accepting connection after certain time


I am implementing tcp server in nodejs which accepts connections from gps devices and code is being monitored by forever. The code is not crashing but after certain time server is not accepting any new connections. Once i restart the server it works fine again. I don't have any clue whats wrong. OS Ubuntu 14.04. Any help is appreciated.

Edit:

time after which the server is not accepting connections is around 6 days.

net.createServer(function(sock){
    sock.on('data',function(data){
         //converting data to ascii.
         //parsing data,doing calculation
         //fetching user_id from redis.(i will get device id from gps device, there are user_id associated to device ids). 
         //posting to api using rest-client

    });
});

Edit 2: Is there anything to with size of virtual memory. The virtual memory size is around 940mb. ?


Solution

  • I've worked depeloping same type of system, so this question have some nostalgia for me. You problem is the File descriptor:

    In Unix and related computer operating systems, a file descriptor (FD, less frequently fildes) is an abstract indicator (handle) used to access a file or other input/output resource, such as a pipe or network connection.

    Then, you need to increase this file descriptor limit in you linux server as follow:

    Edit the kernel parameter file /etc/sysctl.conf. Add line fs.file-max=[new value] to it.

     vim /etc/sysctl.conf
    
     fs.file-max = 500000
    

    Apply the changes:

    sysctl -p
    

    To change the ulimit setting, edit the file /etc/security/limits.conf and set the hard and soft limits.

    vi /etc/security/limits.conf
    * soft nofile 90000
    * hard nofile 90000
    

    Apply the changes:

    reboot
    

    Now test the new system settings using the following commands:

    #ulimit -a
    open files (-n) 90000
    

    Check the current open file descriptor limit:

    # more /proc/sys/fs/file-max
    500000
    

    tip1: To find out how many file descriptors are currently being used

    # more /proc/sys/fs/file-nr
    

    tip2: Add a conjob to restart you socket server (Node.js app) each week. Hope it helps.