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! def load!
require 'ostruct' require 'ostruct'
begin 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 rescue RocketAMF::RemoteGateway::AMFError => e
if e.message == PET_NOT_FOUND_REMOTE_ERROR if e.message == PET_NOT_FOUND_REMOTE_ERROR
raise PetNotFound, "Pet #{name.inspect} does not exist" raise PetNotFound, "Pet #{name.inspect} does not exist"

View file

@ -9,23 +9,22 @@ module RocketAMF
@params = params @params = params
end end
def fetch def fetch(options={})
uri = @service.gateway.uri uri = @service.gateway.uri
data = envelope.serialize 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 = Net::HTTP::Post.new(uri.path)
req.body = data req.body = data
begin 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 rescue Exception => e
raise ConnectionError, e.message raise ConnectionError, e.message
end end
case res
when Net::HTTPSuccess if res.is_a?(Net::HTTPSuccess)
response_body = res.body response_body = res.body
else else
error = nil error = nil
@ -36,16 +35,18 @@ module RocketAMF
end end
raise ConnectionError, error.message raise ConnectionError, error.message
end end
end
begin begin
result = RocketAMF::Envelope.new.populate_from_stream(response_body) result = RocketAMF::Envelope.new.populate_from_stream(response_body)
rescue Exception => e rescue Exception => e
raise ConnectionError, e.message raise ConnectionError, e.message
end end
first_message_data = result.messages[0].data first_message_data = result.messages[0].data
if first_message_data.respond_to?(:[]) && first_message_data[:code] == ERROR_CODE if first_message_data.respond_to?(:[]) && first_message_data[:code] == ERROR_CODE
raise AMFError.new(first_message_data) raise AMFError.new(first_message_data)
end end
result result
end end

View file

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