对于我的rails应用程序,我使用gem Grape设置了API。我添加了一个测试发布方法,该代码休眠10秒并返回{'status'=>'success'}。一切正常,除了API调用似乎阻止了所有其他发送到服务器的请求。在此睡眠10秒api完成之前,不会执行任何其他请求。来自前端接口的任何GET请求都会被延迟。如果我模拟了两个api调用,则第二个调用需要20秒(等待第一个完成需要10秒)才能完成。请对此提供建议。
api.rb文件如下所示:
module ProjectName
module MyErrorFormatter
def self.call message, backtrace, options, env
{ "status" => "Fail", "error_message" => message }.to_json
end
end
class API < Grape::API
format :json
default_format :json
prefix 'api'
cascade false
error_formatter :json, MyErrorFormatter
helpers APIHelpers
before do
authenticate!
end
post do
if params[:action].present?
p_url = format_url(params)
redirect "/api/#{params[:action]}?key=#{params[:key]}#{p_url}"
end
end
post 'test' do
sleep(10)
{'status'=>'success'}
end
end
end
我正在使用Rails 4.2.0
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句