Error:
[2019-05-25 10:44:08] dial tcp 127.0.0.1:5432: socket: too many open files
Ulimits are Linux and Unix features that allows limiting how much resources a user uses, such as processes, CPU time, and various types of memory. You can view your shell’s current ulimits with the command ulimit -a.
“Too many open files” errors happen when a process needs to open more files than it is allowed by the operating system. This number is controlled by the maximum number of file descriptors the process has.
Temporal solution:
The number of file descriptors for the current process can be shown with the following commands:
[root@aurlen ~]# ulimit -a | grep open open files (-n) 1024
To temporal change, the number of file descriptors, execute the command ulimit -n
[root@aurlen-unix ~]# ulimit -n 614400
To know the limits for the current Process:
The current limits set for the process that reach that limits can be verified in the /proc file system. example the limits for Postgres:
ps aux | grep postgres postgres 1801 0.0 0.4 369732 38592 ? S 10:43 0:08 /usr/lib/postgresql/11/bin/postgres -D /var/lib/postgresql/11/main
cat /proc/1801/limits Limit Soft Limit Hard Limit Units Max cpu time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 31219 31219 processes Max open files 1024 4096 files Max locked memory 16777216 16777216 bytes Max address space unlimited unlimited bytes Max file locks unlimited unlimited locks Max pending signals 31219 31219 signals Max msgqueue size 819200 819200 bytes Max nice priority 0 0 Max realtime priority 0 0 Max realtime timeout unlimited unlimited
To change permanent.
Change or add these lines in /etc/security/limits.conf
* soft nofile 614400 * hard nofile 614400 gouser soft nofile 614400 gouser hard nofile 614400
* gouser is the user which run the golang server
Add or change this line to /etc/sysctl.conf
fs.file-max = 716800
And execute this command:
/sbin/sysctl -p
Add to the end of the file /etc/profile:
if [ $USER = "gouser" ]; then if [ $SHELL = "/bin/ksh" ]; then ulimit -p 16384 ulimit -n 65536 else ulimit -u 16384 -n 65536 fi fi
Open a new terminal and execute:
ulimit -n 614400 stop and start the golang process