【问题标题】:Rails 3.2 + RESTful API slowRails 3.2 + RESTful API 慢
【发布时间】:2012-10-06 05:09:10
【问题描述】:

我正在开发一组 RoR 应用程序,并且我实现了一个 API 系统来让它们交换数据。

实现细节

  • 导轨 3.2.8
  • 红宝石 1.9.2 p320
  • jbuilder 0.8.2 (API srv)
  • httparty 0.9.0(API cli)

授权

需要访问令牌才能访问 API

安全

开发环境中的自签名 SSL 证书。 为 API 调用使用 SSL 以防止访问令牌被盗(httparty 自动忽略 SSL 警告)。

场景

APP1 公开提供 API 的数据

APP2 公开提供 API 的数据

APP3 公开提供 API 的数据

APP4 需要 APP1、APP2、APP3 数据并使用 API 来获取。

问题

第一次调用 API 很慢(每个 APP 延迟 2 - 3 秒,后续调用很快 ~ 50 毫秒)。我认为这是延迟清单,因为APP4需要连接到APP*,然后连接是mantained,是吗?

对调试/解决问题有什么建议吗?

非常感谢, 毛罗

更新(2012-10-25)

在 API SRV APP 上添加了输出 (ruby-prof): https://gist.github.com/3950920

【问题讨论】:

  • 改用 ruby​​ 1.9.3-p286(最近发布)能解决问题吗?
  • 这里真的没有足够的信息可以说。我们必须查看日志、代码、请求等。但是您可以尝试使用 ruby​​-prof 之类的东西来分析自己。

标签: ruby web-services api rest ruby-on-rails-3.2


【解决方案1】:

我找到了问题,我想和你分享解决方案。

默认情况下,Passenger 会在需要时启动应用程序(以节省内存、CPU 等)。因此,对每个应用程序(APP1、APP2 和 APP3)的 API 的第一次调用最多需要 2 到 3 秒。

为了解决这个问题,我使用在 http://www.modrails.com/documentation/Users%20guide%20Apache.html#PassengerPreStart 找到的正确乘客指令预加载应用程序

遵循我的 rails 配置:

# Load passenger module and set paths
# The following three lines MUST be updated whenever the 'passenger-install-apache2-module' is executed
LoadModule passenger_module /usr/local/rvm/gems/ruby-1.9.3-p286/gems/passenger-3.0.17/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/rvm/gems/ruby-1.9.3-p286/gems/passenger-3.0.17
PassengerRuby /usr/local/rvm/wrappers/ruby-1.9.3-p286/ruby

# Speeds up spawn time tremendously -- if your app is compatible. 
# RMagick seems to be incompatible with smart spawning
# Older versions of Passenger called this RailsSpawnMethod
PassengerSpawnMethod smart

# Keep the application instances alive longer. Default is 300 (seconds)
PassengerPoolIdleTime 1000

# Keep the spawners alive, which speeds up spawning a new Application
# listener after a period of inactivity at the expense of memory.
RailsAppSpawnerIdleTime 0

# Just in case you're leaking memory, restart a listener 
# after processing 5000 requests
PassengerMaxRequests 5000

# Automatically hit your site when apache starts, so that you don't have to wait
# for the first request for passenger to "spin up" your application. This even
# helps when you have smart spawning enabled. 
PassengerPreStart http://app1.mydomain.com/
PassengerPreStart http://app2.mydomain.com/
PassengerPreStart http://app3.mydomain.com/
PassengerPreStart http://app4.mydomain.com/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多