简体   繁体   English

Rails 3.2 + RESTful API速度慢

[英]Rails 3.2 + RESTful API slow

I'm working on a collection of RoR applications and I implemented an API system to let them exchange data. 我正在研究RoR应用程序的集合,并且实现了一个API系统以允许它们交换数据。

Implementation details 实施细节

Libraries 图书馆

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

Authorization 授权书

An access token is required in order to get access to the API 需要访问令牌才能访问API

Security 安全

Self-signed SSL certicates in development environment. 开发环境中的自签名SSL证书。 Use of SSL for API calls in order to prevent the access token to be stolen (httparty automatically ignore SSL warnings). 使用SSL进行API调用可防止访问令牌被盗(httparty自动忽略SSL警告)。

Scenario 情境

APP1 exposes data providing an API APP1公开提供API的数据

APP2 exposes data providing an API APP2公开提供API的数据

APP3 exposes data providing an API APP3公开提供API的数据

APP4 needs APP1, APP2, APP3 data and use the APIs in order to get it. APP4需要APP1,APP2,APP3数据并使用API​​来获取它。

Issue 问题

The first call to the API is slow (2 - 3 seconds of delay for each APP, subsequent calls are fast ~50 ms). 对API的第一次调用很慢(每个APP的延迟为2-3秒,随后的调用约为50毫秒)。 I think that is delay manifests, because APP4 needs to connect to APP*, then the connection is mantained, is this right? 我认为这是延迟的表现,因为APP4需要连接到APP *,然后才能进行连接,对吗?

Any suggestion to debug/solve the problem? 有任何调试/解决问题的建议吗?

Many thanks, Mauro 非常感谢,毛罗

UPDATE (2012-10-25) 更新(2012-10-25)

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

I found the problem and I want to share the solution with you. 我发现了问题,并想与您分享解决方案。

By default Passenger brings up application when needed (in order to save memory, cpu, ...). 默认情况下,Passenger在需要时会启动应用程序(以节省内存,CPU等)。 For this reason the first call to API of each application (APP1, APP2 & APP3) took up to 2 - 3 seconds. 因此,对每个应用程序(APP1,APP2和APP3)的API的首次调用最多需要2-3秒。

To solve the issue I preload applications using the correct Passenger directive found at http://www.modrails.com/documentation/Users%20guide%20Apache.html#PassengerPreStart 为了解决此问题,我使用http://www.modrails.com/documentation/Users%20guide%20Apache.html#PassengerPreStart中找到的正确的Passenger指令预加载了应用程序

Follows my rails configuration: 遵循我的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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM