1
0
Fork 0
forked from OpenNeo/impress

add timeout to pet load

This commit is contained in:
Emi Matchu 2012-08-11 18:47:25 -04:00
parent 412c401c5f
commit 7dfc6d81a2
3 changed files with 30 additions and 28 deletions

View file

@ -19,7 +19,8 @@ class Pet < ActiveRecord::Base
def load!
require 'ostruct'
begin
envelope = Pet.amf_service.fetch(PET_VIEWER_METHOD, name, nil)
envelope = Pet.amf_service.request(PET_VIEWER_METHOD, name, nil).
fetch(:timeout => 2)
rescue RocketAMF::RemoteGateway::AMFError => e
if e.message == PET_NOT_FOUND_REMOTE_ERROR
raise PetNotFound, "Pet #{name.inspect} does not exist"

View file

@ -9,23 +9,22 @@ module RocketAMF
@params = params
end
def fetch
def fetch(options={})
uri = @service.gateway.uri
data = envelope.serialize
# TODO: re-enable if we go back to using threads
if false && defined?(EventMachine) && EventMachine.respond_to?(:reactor_running?) && EventMachine.reactor_running?
req = EM::HttpRequest.new(uri).post :body => data
response_body = req.response
else
req = Net::HTTP::Post.new(uri.path)
req.body = data
begin
res = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) }
http = Net::HTTP.new(uri.host, uri.port)
http.read_timeout = options[:timeout] if options[:timeout]
res = http.request(req)
rescue Exception => e
raise ConnectionError, e.message
end
case res
when Net::HTTPSuccess
if res.is_a?(Net::HTTPSuccess)
response_body = res.body
else
error = nil
@ -36,16 +35,18 @@ module RocketAMF
end
raise ConnectionError, error.message
end
end
begin
result = RocketAMF::Envelope.new.populate_from_stream(response_body)
rescue Exception => e
raise ConnectionError, e.message
end
first_message_data = result.messages[0].data
if first_message_data.respond_to?(:[]) && first_message_data[:code] == ERROR_CODE
raise AMFError.new(first_message_data)
end
result
end

View file

@ -8,8 +8,8 @@ module RocketAMF
@name = name
end
def fetch(method, *params)
Request.new(self, method, *params).fetch
def request(method, *params)
Request.new(self, method, *params)
end
end
end