Module: LVS::JsonService::Request::ClassMethods

Defined in:
lib/lvs/json_service/request.rb

Instance Method Summary collapse

Instance Method Details

#http_request_with_timeout(service, args, options) ⇒ Object



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
# File 'lib/lvs/json_service/request.rb', line 14

def http_request_with_timeout(service, args, options)
  uri = URI.parse(service)

  req = Net::HTTP::Post.new(uri.path)
  req.form_data = { "object_request" => args.to_json }

  options[:encrypted] ||= require_ssl?
  retries = options[:retries] || 0
  hard_retries = 1 # For persistent connection failures

  begin
    retries -= 1
            
    http = LVS::JsonService::ConnectionManager.get_connection(uri.host, uri.port, options)
    response = http.request(req)
  
  rescue Errno::EPIPE, EOFError, Errno::ECONNRESET
    hard_retries -= 1
    if hard_retries >= 0
      sleep(1)
      LVS::JsonService::ConnectionManager.reset_connection(uri.host, uri.port, options)
      retry
    end
  
  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

  if response.is_a?(Net::HTTPNotModified)
    raise LVS::JsonService::NotModified.new("304 Data hasn't changed", 304, service, args)
  end

  response
end

#run_remote_request(service, args, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/lvs/json_service/request.rb', line 73

def run_remote_request(service, args, options = {})
  LVS::JsonService::Logger.debug "Requesting '#{service}' with #{args.to_json}"
  
  if options[:cached_for]
    timing = "CACHED"
    response, result = Rails.cache.fetch([service, args].cache_key, :expires_in => options[:cached_for]) do
      start = Time.now
      response = http_request_with_timeout(service, args, options)
      net_timing = ("%.1f" % ((Time.now - start) * 1000)) + "ms"
      start = Time.now
      result = JSON.parse(response.body)
      parse_timing = ("%.1f" % ((Time.now - start) * 1000)) + "ms"
      timing = "Net: #{net_timing}, Parse: #{parse_timing}"
      [response, result]
    end
  else
    start = Time.now
    response = http_request_with_timeout(service, args, options)
    net_timing = ("%.1f" % ((Time.now - start) * 1000)) + "ms"
    start = Time.now
    result = JSON.parse(response.body)
    parse_timing = ("%.1f" % ((Time.now - start) * 1000)) + "ms"
    timing = "Net: #{net_timing}, Parse: #{parse_timing}"
  end

  if response.body.size < 1024 || options[:debug]
    LVS::JsonService::Logger.debug "Response (#{timing}): #{response.body.gsub(/\n/, '')}"
  else
    LVS::JsonService::Logger.debug "Response Snippet (#{timing} / #{"%.1f" % (response.body.size/1024)}kB): #{response.body.gsub(/\n/, '')[0..1024]}"
  end
  if result.is_a?(Hash) && result.has_key?("PCode")
    raise LVS::JsonService::Error.new(result["message"], result["PCode"], service, args, result)
  end
  result
end