title: Heroku + Sidekiq: ActiveRecord::StatementInvalid: PG::UnableToSend: SSL SYSCALL error: EOF detected
body: <p>Hi we are running on Heroku's Cedar stack with Unicorn and Sidekiq. We intermittently get the following errors</p>
  
  <pre><code>BurnThis ActiveRecord::StatementInvalid: PG::UnableToSend: SSL SYSCALL error: EOF detected
  
  ActiveRecord::StatementInvalid: PG::ConnectionBad: PQconsumeInput() SSL SYSCALL error: Connection timed out
  </code></pre>
  
  <p>Does anyone have any insight what the direct cause of these errors? Is it too many connections to our database? We have our forking set up already in the following way:</p>
  
  <p>unicorn.rb</p>
  
  <pre><code>worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
  timeout 30
  preload_app true
  
  before_fork do |server, worker|
    Signal.trap 'TERM' do
      puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
      Process.kill 'QUIT', Process.pid
    end
  
    defined?(ActiveRecord::Base) and
      ActiveRecord::
  
  Base.connection.disconnect!
  end
  
  after_fork do |server, worker|
    Signal.trap 'TERM' do
      puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
    end
  
    # other setup
    if defined?(ActiveRecord::Base)
      config = Rails.application.config.database_configuration[Rails.env]
      config['adapter'] = 'postgis'
      config['pool']              = ENV['DB_POOL'] || 5
      config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10 # seconds
      ActiveRecord::Base.establish_connection(config)
    end
  end
  </code></pre>
  
  <p>And sidekiq.rb</p>
  
  <pre><code>Sidekiq.configure_server do |config|
    config.redis = { :url =&gt; ENV['REDIS_URL'], :namespace =&gt; 'btsidekiq' }
  
    if defined?(ActiveRecord::Base)
      config = Rails.application.config.database_configuration[Rails.env]
      config['adapter'] = 'postgis'
      config['pool']              = ENV['DB_POOL'] || 5
      config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10 # seconds
      ActiveRecord::Base.establish_connection(config)
    end
  end
  
  Sidekiq.configure_client do |config|
    config.redis = { :url =&gt; ENV['REDIS_URL'], :namespace =&gt; 'btsidekiq' }
  end
  </code></pre>
  
  <p>Our database pool size is pretty large DB_POOL=100 and we are on a PG database that apparently supports 500 connections concurrently. </p>