14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/lvs/json_service/request.rb', line 14
def http_request_with_timeout(service, args, options)
uri = URI.parse(service)
http = Net::HTTP.new(uri.host, uri.port)
http = Net::HTTP.new(uri.host, uri.port)
if options[:encrypted] || require_ssl?
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
LVS::JsonService::Logger.debug "Using SSL"
if options[:auth_cert]
LVS::JsonService::Logger.debug "Using Auth"
http.cert = OpenSSL::X509::Certificate.new(File.read(options[:auth_cert]))
http.key = OpenSSL::PKey::RSA.new(File.read(options[:auth_key]), options[:auth_key_password])
end
end
http.open_timeout = options[:timeout] || 1
http.read_timeout = options[:timeout] || 1
req = Net::HTTP::Post.new(uri.path)
req.form_data = { "object_request" => args.to_json }
retries = options[:retries] || 0
begin
retries -= 1
response = http.start { |connection| connection.request(req) }
rescue Timeout::Error => e
if retries >= 0
LVS::JsonService::Logger.debug(
"Retrying #{service} due to TimeoutError"
)
retry
end
raise LVS::JsonService::TimeoutError.new("Backend failed to respond in time", 500, service, args)
rescue Errno::ECONNREFUSED => e
if retries >= 0
LVS::JsonService::Logger.debug(
"Retrying #{service} due to Errno::ECONNREFUSED"
)
sleep(1)
retry
end
raise LVS::JsonService::BackendUnavailableError.new("Backend unavailable", 500, service, args)
rescue OpenSSL::SSL::SSLError => e
raise LVS::JsonService::BackendUnavailableError.new("Backend unavailable #{e}", 500, service, args)
end
if response.is_a?(Net::HTTPNotFound)
raise LVS::JsonService::NotFoundError.new("404 Found for the service", 404, service, args)
end
response
end
|