Unicorns吃掉了不考虑池大小的MySQL连接–Rails

我为我的Rails应用程序运行了4个Unicorn进程,它们占用了所有可用的MySQL连接,导致它因“连接数太多”错误而崩溃。 今天我不得不重启我的数据库实例4次。 =(

流程

 $ ps ax |  grep [u] ni
 21618?  Sl 0:15独角兽大师-D -c /home/deployer/apps/XXX/shared/config/unicorn.rb -E production                                                           
 21632?  Sl 0:20独角兽工[0] -D -c /home/deployer/apps/XXX/shared/config/unicorn.rb -E production                                                        
 21636?  Sl 0:14独角兽工[1] -D -c /home/deployer/apps/XXX/shared/config/unicorn.rb -E production                                                        
 21640?  Sl 0:20独角兽工[2] -D -c /home/deployer/apps/XXX/shared/config/unicorn.rb -E production                                                        
 21645?  Sl 0:12独角兽工[3] -D -c /home/deployer/apps/XXX/shared/config/unicorn.rb -E production  

我的database.yml为ActiveRecord池设置了22个连接……

 ...
生产:
  适配器:mysql2
  编码:utf8
  数据库:xxx
  用户名:xxx
  密码:xxx
  主持人:xxx
  港口:3306
  游泳池:22
 ...

Unicorn配置文件如下所示:

 working_directory“/ home / deployer / apps / XXX / current”
 pid“/home/deployer/apps/XXX/shared/pids/unicorn.pid”
 stderr_path“/home/deployer/apps/XXX/shared/log/unicorn.log”
 stdout_path“/home/deployer/apps/XXX/shared/log/unicorn.log”

听“/tmp/unicorn.XXX.sock”
 worker_processes 4
超时100

 preload_app是的

 before_fork do | server,worker |
   #断开连接,因为数据库连接不会继续
  如果定义? 的ActiveRecord :: Base的
     ActiveRecord的:: Base.connection.disconnect!
  结束

   #退出旧的独角兽进程
   old_pid =“#{server.config [:pid]}。oldbin”
  如果File.exists?(old_pid)&& server.pid!= old_pid
    开始
       Process.kill(“QUIT”,File.read(old_pid).to_i)
    救援Errno :: ENOENT,Errno :: ESRCH
       #其他人为我们做了我们的工作
    结束
  结束
结束

 after_fork do | server,worker |
   #再次在worker中启动数据库连接
  如果定义?(ActiveRecord :: Base)
    的ActiveRecord :: Base.establish_connection
  结束
   child_pid = server.config [:pid] .sub(“。pid”,“。#{worker.nr} .pid”)
   system(“echo#{Process.pid}>#{child_pid}”)
结束

如果我们查看数据库控制台,我们会看到类似这样的内容。 他们吃掉了大部分的连接。 (除了独角兽之外,我什么都没跑)在我看来应该有1个连接* 4个独角兽= 4个连接。

 mysql> show完整进程列表;
 + ----- + ---------- + -------------------------------- ------------------ + ------------------------ + ------ --- + ------ + ------- + ------------ +
 |  Id | 用户| 主持人|  db | 命令| 时间| 国家| 信息|
 + ----- + ---------- + -------------------------------- ------------------ + ------------------------ + ------ --- + ------ + ------- + ------------ +
 |  2 |  rdsadmin |  localhost:31383 |  NULL | 睡觉|  9 |  |  NULL |
 |  52 | 级别|  212.100.140.42:50683 |  leveltravel_production | 查询|  0 |  NULL | 显示完整的进程列表|
 |  74 | 级别|  ip-10-55-10-151.eu-west-1.compute.internal:38197 |  leveltravel_production | 睡觉|  5 |  |  NULL |
 |  75 | 级别|  ip-10-55-10-151.eu-west-1.compute.internal:38199 |  leveltravel_production | 睡觉|  8 |  |  NULL |
 |  76 | 级别|  ip-10-55-10-151.eu-west-1.compute.internal:38201 |  leveltravel_production | 睡觉|  8 |  |  NULL |

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~ CUT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

 |  157 | 级别|  ip-10-55-10-151.eu-west-1.compute.internal:38321 |  leveltravel_production | 睡觉|  154 |  |  NULL |
 |  158 | 级别|  ip-10-55-10-151.eu-west-1.compute.internal:38322 |  leveltravel_production | 睡觉|  17 |  |  NULL |
 |  159 | 级别|  ip-10-55-10-151.eu-west-1.compute.internal:38325 |  leveltravel_production | 睡觉|  54 |  |  NULL |
 |  160 | 级别|  ip-10-55-10-151.eu-west-1.compute.internal:38326 |  leveltravel_production | 睡觉|  54 |  |  NULL |
 |  161 | 级别|  ip-10-55-10-151.eu-west-1.compute.internal:38327 |  leveltravel_production | 睡觉|  54 |  |  NULL |
 |  162 | 级别|  ip-10-55-10-151.eu-west-1.compute.internal:38329 |  leveltravel_production | 睡觉|  42 |  |  NULL |
 + ----- + ---------- + -------------------------------- ------------------ + ------------------------ + ------ --- + ------ + ------- + ------------ +
 90行(0.15秒)

您还可以查看sidekiq存储库中的问题#503以了解此问题的背景https://github.com/mperham/sidekiq/issues/503

你已经运行了4个独角兽进程。 这是过程,而不是线程。
每个进程在池中有22个连接。 他们总共有22 * 4 = 88个连接。
如果要为4个工作进程提供4个连接,可以在database.yml中设置pool:1