我正在研究一系列RoR应用程序,并实施了一个API系统让他们交换数据。
实施细节
库
授权
需要访问令牌才能访问API
安全的
开发环境中的自签名SSL证书。 使用SSL进行API调用以防止访问令牌被盗(httparty会自动忽略SSL警告)。
方案
APP1公开提供API的数据
APP2公开提供API的数据
APP3公开提供API的数据
APP4需要APP1,APP2,APP3数据并使用API才能获得它。
问题
对API的第一次调用很慢(每个APP延迟2-3秒,后续调用快~50 ms)。我认为这是延迟清单,因为APP4需要连接到APP *,然后连接被保留,这是对的吗?
有任何调试/解决问题的建议吗?
非常感谢, 莫罗
更新(2012-10-25)
在API SRV APP上添加了输出(ruby-prof): https://gist.github.com/3950920
答案 0 :(得分:0)
我发现了问题,我希望与您分享解决方案。
默认情况下,Passenger会在需要时启动应用程序(为了节省内存,CPU,...)。因此,每个应用程序(APP1,APP2和APP3)的API首次调用最多需要2到3秒。
要解决此问题,我使用http://www.modrails.com/documentation/Users%20guide%20Apache.html#PassengerPreStart
中的正确Passenger指令预加载应用程序按照我的导轨配置:
# 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/