ncr / rack-proxy

A request/response rewriting HTTP proxy. A Rack app.
MIT License
269 stars 94 forks source link

ActiveRecord::ConnectionTimeoutError #93

Closed tleish closed 4 years ago

tleish commented 4 years ago

I have the following proxy for api requests. I want to forward all requests where the past starts with /api to a separate server.

class ApiProxy < Rack::Proxy
  def rewrite_env(env)
    if Rack::Request.new(env).path =~ %r{^/api}
      env['HTTP_HOST'] = ENV['API_PROXIED_HOST']
    end
    env
  end
end

and then in the Rails application.rb

config.middleware.use ApiProxy, streaming: false

This works for http://example.com/api/ping, but I get the following error when trying to access any other url (e.g. http://example.com/test

ActiveRecord::ConnectionTimeoutError (could not obtain a connection from the pool within 5.000 seconds (waited 5.001 seconds); all pooled connections were in use):

activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:222:in `block in wait_poll'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:211:in `loop'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:211:in `wait_poll'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:172:in `internal_poll'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:298:in `internal_poll'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:166:in `block in poll'
~/ruby/2.6.5/lib/ruby/2.6.0/monitor.rb:235:in `mon_synchronize'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:176:in `synchronize'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:166:in `poll'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:877:in `acquire_connection'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:595:in `checkout'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:439:in `connection'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:1121:in `retrieve_connection'
activerecord (6.0.2.2) lib/active_record/connection_handling.rb:238:in `retrieve_connection'
activerecord (6.0.2.2) lib/active_record/connection_handling.rb:206:in `connection'
activerecord (6.0.2.2) lib/active_record/migration.rb:562:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.0.2.2) lib/active_support/callbacks.rb:101:in `run_callbacks'
actionpack (6.0.2.2) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.2.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (6.0.2.2) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.2.2) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.2.2) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.2.2) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.2.2) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
request_store (1.5.0) lib/request_store/middleware.rb:19:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.2.2) lib/rack/method_override.rb:24:in `call'
rack (2.2.2) lib/rack/runtime.rb:22:in `call'
activesupport (6.0.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.2.2) lib/rack/sendfile.rb:110:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/host_authorization.rb:83:in `call'
webpacker (4.0.7) lib/webpacker/dev_server_proxy.rb:29:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (6.0.2.2) lib/rails/engine.rb:526:in `call'
rack (2.2.2) lib/rack/handler/webrick.rb:95:in `service'
~/ruby/2.6.5/lib/ruby/2.6.0/webrick/httpserver.rb:140:in `service'
~/ruby/2.6.5/lib/ruby/2.6.0/webrick/httpserver.rb:96:in `run'
~/ruby/2.6.5/lib/ruby/2.6.0/webrick/server.rb:307:in `block in start_thread'
tleish commented 4 years ago

Found an alternative solution