【问题标题】:Is request.location in geocoder rails gem too slow for heroku?geocoder rails gem中的request.location对heroku来说太慢了吗?
【发布时间】:2014-11-22 06:05:24
【问题描述】:

过去一周,我注意到在我使用 Geocoder gem 请求位置的页面上,我的 heroku 应用经常显示

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.  

日志显示

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

我以前从未注意到这个问题,但也许它一直存在,只是我直到最近才经常访问有问题的页面。超时与访问页面的发生似乎是随机的。

所以我加了

Geocoder.configure(
  :timeout => 40
)

到我的 config/initializers/geocoder.rb。我认为这导致更频繁地访问页面,但加载通常需要一段时间。

我正在这些页面上实施默认邮政编码,这样我就不会收到应用程序错误,但从长远来看这并不理想。

我试过了

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')

希望在 request.location 超时时获得默认邮政编码 20016,但我仍然收到应用程序错误。

还有其他建议吗?

【问题讨论】:

    标签: ruby-on-rails heroku rails-geocoder


    【解决方案1】:

    如果您为 Geocoder gem 设置超时,您需要将其设置为小于 30 秒,这是您对 Heroku 的每个请求的时间。当超时设置为 40 秒时,对 IP 地理定位服务的地理编码器请求将在 40 秒后超时——但此时 Heroku 已经超时请求并且您会看到 H12 应用程序错误。将超时设置为小于 30 秒,您至少应该看到默认邮政编码。

    Geocoder request.location 方法将尝试使用默认 IP 地理编码服务 FreeGeoIP 对请求 IP 地址进行地理编码。他们最近似乎遇到了一些问题(2015 年 11 月)。您可以尝试其他 IP 服务之一(如 Telize),看看这是否也有助于解决问题。

    【讨论】:

    • 感谢您的回答,卢卡斯!在某些时候,我将超时设置为 30,但仍然有问题。我将超时设置为 20,并将位置替换为“华盛顿特区”。我认为 20016 年可能不是位置的有效值。至少现在它在使用 request.location 和“华盛顿特区”之间摇摆不定。
    • 是的,我应该更清楚。如果地理编码器超时设置为 30,您仍然会看到 H12 超时,因为请求的总时间最终会超过 30 秒。
    猜你喜欢
    • 1970-01-01
    • 2012-06-16
    • 2020-08-08
    • 2011-11-22
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    相关资源
    最近更新 更多