Search code examples
ruby-on-railsherokurails-geocoder

Is request.location in geocoder rails gem too slow for heroku?


Over the past week, I've noticed that on pages where I use the Geocoder gem to request.location, my heroku app often displays

Application Error
An error occurred in the application and your page could not be served. Please try again in a few moments.  
If you are the application owner, check your logs for details.  

The logs show

Geocoding API not responding fast enough (use Geocoder.configure(:timeout => ...)  

I've never noticed this problem before, but perhaps it was always there, it's just that I didn't access the pages with the problem very often until recently. The occurrence of timing out vs. accessing the page seems random.

So I added

Geocoder.configure(
  :timeout => 40
)

to my config/initializers/geocoder.rb. I think that resulted in accessing the page more often, but it often takes awhile to load.

I'm implementing a default zip code on those pages so that I don't get application errors, but that's not ideal in the long term.

I tried

if params[:search].present?
   @events = Event.near(params[:search], params[:dist], order: 'distance') 
elsif user_signed_in? && current_user.address
  @events = Event.near([current_user.latitude, current_user.longitude], 25, order: 'distance') 
elsif request.location 
  @events = Event.near([request.location.latitude, request.location.longitude], 25, order: 'distance') 
else
  @events = Event.near(20016, 100, order: 'distance')

expecting to get a default zipcode of 20016 when request.location times out, but I still get the application error.

Any other suggestions?


Solution

  • If you set a timeout for the Geocoder gem you will need to set it to less than 30 seconds, which is how long you have for each request to Heroku. When the timeout is set to 40 seconds, the Geocoder request to the IP geolocation service will timeout after 40 second -- but at the point Heroku has already timed out the request and you see the H12 application error. Set the timeout to something less than 30 seconds and you should at least see the default zip code.

    The Geocoder request.location method will attempt to geocode the request IP address using the default IP geocoding service, FreeGeoIP. It seems that they have been having some issues recently (Nov, 2015). You might try one of the other IP services (like Telize) to see if that helps the issue as well.