Heroku、Puma、Redis、Sidekiq 和连接限制,达到 ERR 最大客户端数量

Nat*_*a B 5 ruby ruby-on-rails heroku

我正在使用 heroku、puma 和 redis 来搭配 sidekiq。当我试图执行一些时间正在抛出的后台工作时Your Redis connection pool is too small for Sidekiq to work. Your pool has 20 connections but really needs to have at least 22

请找到下面的配置文件,请帮我找到解决方案

在此处输入图片说明

我在用

Redis To Go 实例:Mini

进程文件

web: bundle exec puma -C config/puma.rb  
worker: bundle exec sidekiq -C config/sidekiq.yml 
Run Code Online (Sandbox Code Playgroud)

美洲狮

workers Integer(ENV['WEB_CONCURRENCY'] || 2)  
threads_count = Integer(ENV['MAX_THREADS'] || 1)  
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup  
port        ENV['PORT']     || 3000  
environment ENV['RACK_ENV'] || 'development'

# Because we are using preload_app, an instance of our app is created by master process (calling our initializers) and then memory space
# is forked. So we should close DB connection in the master process to avoid connection leaks.
# https://github.com/puma/puma/issues/303
# http://stackoverflow.com/questions/17903689/puma-cluster-configuration-on-heroku
# http://www.rubydoc.info/gems/puma/2.14.0/Puma%2FDSL%3Abefore_fork
# Dont have to worry about Sidekiq's connection to Redis because connections are only created when needed. As long as we are not
# queuing workers when rails is booting, there will be no redis connections to disconnect, so it should be fine.
before_fork do  
  puts "Puma master process about to fork. Closing existing Active record connections."
  ActiveRecord::Base.connection.disconnect!
end

on_worker_boot do  
  # Worker specific setup for Rails 4.1+
  # See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
  ActiveRecord::Base.establish_connection
end 
Run Code Online (Sandbox Code Playgroud)

在 sidekiq.yml

development:  
  :concurrency: 5
production:  
  :concurrency: 20
:queues:
  - default
Run Code Online (Sandbox Code Playgroud)

sidekiq.rb

Sidekiq.configure_client do |config|
  config.redis = { url: ENV['REDISTOGO_URL'], size: 2 }
end

Sidekiq.configure_server do |config|
  config.redis = { url: ENV['REDISTOGO_URL'], size: 20 }

  Rails.application.config.after_initialize do
    Rails.logger.info("DB Connection Pool size for Sidekiq Server before disconnect is: #{ActiveRecord::Base.connection.pool.instance_variable_get('@size')}")
    ActiveRecord::Base.connection_pool.disconnect!

    ActiveSupport.on_load(:active_record) do
      config = Rails.application.config.database_configuration[Rails.env]
      config['reaping_frequency'] = ENV['DATABASE_REAP_FREQ'] || 10 # seconds
      # config['pool'] = ENV['WORKER_DB_POOL_SIZE'] || `
      config['pool'] = 16
      ActiveRecord::Base.establish_connection(config)

      Rails.logger.info("DB Connection Pool size for Sidekiq Server is now: #{ActiveRecord::Base.connection.pool.instance_variable_get('@size')}")
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

部署此配置后,它会抛出以下错误,

您的 Redis 连接池太小,Sidekiq 无法工作。您的池有 20 个连接,但实际上至少需要有 22 个连接

后来我在sidkiq.rb中更新了以下详细信息,

Sidekiq.configure_client do |config|
  config.redis = { url: ENV['REDISTOGO_URL'], size: 2 }
end

Sidekiq.configure_server do |config|
  config.redis = { url: ENV['REDISTOGO_URL'], size: 22 }

  Rails.application.config.after_initialize do
    Rails.logger.info("DB Connection Pool size for Sidekiq Server before disconnect is: #{ActiveRecord::Base.connection.pool.instance_variable_get('@size')}")
    ActiveRecord::Base.connection_pool.disconnect!

    ActiveSupport.on_load(:active_record) do
      config = Rails.application.config.database_configuration[Rails.env]
      config['reaping_frequency'] = ENV['DATABASE_REAP_FREQ'] || 10 # seconds
      # config['pool'] = ENV['WORKER_DB_POOL_SIZE'] || `
      config['pool'] = 16
      ActiveRecord::Base.establish_connection(config)

      Rails.logger.info("DB Connection Pool size for Sidekiq Server is now: #{ActiveRecord::Base.connection.pool.instance_variable_get('@size')}")
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这一次它抛出了另一个问题

已达到 ERR 最大客户端数

我只在生产中遇到这个问题,请帮帮我