barelyknown
barelyknown

Reputation: 5560

How do I fix a "Connection timed out" error for HTTP read operations on Heroku?

I have an operation in my Heroku-hosted Rails app that runs in a background process and calls out to a government website that is slow. I am receiving a timeout error (at the bottom below) that I haven't been able to fix.

The read timeout appears happen at 20 seconds into the request.

The following line causes the problem:

Mechanize.new.get('http://ai.fmcsa.dot.gov/SMS/Data/Search.aspx')

Is there an environment variable that fixes this? Something else that I'm missing?

Errno::ETIMEDOUT: Connection timed out - connect(2)
    from /app/vendor/bundle/ruby/1.9.1/gems/net-http-persistent-2.7/lib/net/http/persistent/ssl_reuse.rb:29:in `initialize'
    from /app/vendor/bundle/ruby/1.9.1/gems/net-http-persistent-2.7/lib/net/http/persistent/ssl_reuse.rb:29:in `open'
    from /app/vendor/bundle/ruby/1.9.1/gems/net-http-persistent-2.7/lib/net/http/persistent/ssl_reuse.rb:29:in `block in connect'
    from /usr/local/lib/ruby/1.9.1/timeout.rb:44:in `timeout'
    from /usr/local/lib/ruby/1.9.1/timeout.rb:89:in `timeout'
    from /app/vendor/bundle/ruby/1.9.1/gems/net-http-persistent-2.7/lib/net/http/persistent/ssl_reuse.rb:29:in `connect'
    from /usr/local/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
    from /usr/local/lib/ruby/1.9.1/net/http.rb:632:in `start'
    from /app/vendor/bundle/ruby/1.9.1/gems/net-http-persistent-2.7/lib/net/http/persistent.rb:768:in `reset'
    from /app/vendor/bundle/ruby/1.9.1/gems/net-http-persistent-2.7/lib/net/http/persistent.rb:503:in `connection_for'
    from /app/vendor/bundle/ruby/1.9.1/gems/net-http-persistent-2.7/lib/net/http/persistent.rb:806:in `request'
    from /app/vendor/bundle/ruby/1.9.1/gems/mechanize-2.5.1/lib/mechanize/http/agent.rb:258:in `fetch'
    from /app/vendor/bundle/ruby/1.9.1/gems/mechanize-2.5.1/lib/mechanize.rb:407:in `get'
    from /app/app/models/inspection.rb:14:in `fmcsa_summary'
    from (irb):5
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'

Upvotes: 1

Views: 1920

Answers (1)

barelyknown
barelyknown

Reputation: 5560

The solution that worked as a temporary fix was to use a proxy server via Mechanize. There must be something going on between the server that I'm connecting to and the IP address range of the Heroku EC2 instances.

agent = Mechanize.new { |a| a.set_proxy(url,port,username,password) }

It would still be helpful if someone knew the root cause of the original problem.

Upvotes: 1

Related Questions