Class: Protobuf::Rpc::Connectors::Http

Inherits:
Base
  • Object
show all
Includes:
Logger::LogMethods, Common
Defined in:
lib/protobuf/rpc/connectors/http.rb

Instance Attribute Summary

Attributes included from Common

#error

Attributes inherited from Base

#complete_cb, #failure_cb, #options, #success_cb

Instance Method Summary collapse

Methods included from Logger::LogMethods

included, #log_exception, #sign_message

Methods included from Common

#any_callbacks?, #complete, #data_callback, #fail, #initialize_stats, #parse_response, #post_init, #request_bytes, #request_caller, #setup_connection, #succeed, #timeout_wrap, #validate_request_type!, #verify_callbacks, #verify_options!

Methods inherited from Base

#first_alive_load_balance?, #initialize, #ping_port, #ping_port_enabled?

Constructor Details

This class inherits a constructor from Protobuf::Rpc::Connectors::Base

Instance Method Details

#baseObject



43
44
45
# File 'lib/protobuf/rpc/connectors/http.rb', line 43

def base
  options[:base] or ''
end

#clientObject



47
48
49
# File 'lib/protobuf/rpc/connectors/http.rb', line 47

def client
  @_client ||= Faraday.new(:url => host)
end

#close_connectionObject

private



25
26
27
# File 'lib/protobuf/rpc/connectors/http.rb', line 25

def close_connection
  log_debug { sign_message('Connector closed')  }
end

#error?Boolean

Method to determine error state, must be used with Connector api

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
# File 'lib/protobuf/rpc/connectors/http.rb', line 30

def error?
  log_debug { sign_message("Error state : #{@error}")  }
  if @error
    true
  else
    false
  end
end

#hostObject



39
40
41
# File 'lib/protobuf/rpc/connectors/http.rb', line 39

def host
  'http://' + options[:host] + ':' + options[:port].to_s
end

#log_signatureObject



19
20
21
# File 'lib/protobuf/rpc/connectors/http.rb', line 19

def log_signature
  @_log_signature ||= "[http-client-#{self.class.name}]"
end

#send_dataObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/protobuf/rpc/connectors/http.rb', line 51

def send_data
  rpc_request = ::Protobuf::Socketrpc::Request.decode(@request_data)

  http_response = client.post do |http_request|
    path_components = [''] + rpc_request[:service_name].split('::') + [rpc_request[:method_name]]
    http_request.url base + path_components.map{ |x| CGI::escape(x) }.join('/')
    http_request.headers['Content-Type'] = 'application/x-protobuf'
    http_request.headers['X-Protobuf-Caller'] = rpc_request[:caller] || ''
    http_request.body = rpc_request[:request_proto]
  end

  # Server returns protobuf response with no error
  if http_response.status == 200 and http_response.headers['x-protobuf-error'].nil?
    rpc_response = Protobuf::Socketrpc::Response.new(
      :response_proto => http_response.body
    )
  # Server returns protobuf error
  elsif http_response.status != 200 and not http_response.headers['x-protobuf-error'].nil?
    rpc_response = Protobuf::Socketrpc::Response.new(
      :response_proto => http_response.body,
      :error => http_response.headers['x-protobuf-error'],
      :error_reason => http_response.headers['x-protobuf-error-reason'].to_i
    )
  # Server didn't return a response or error
  else
    rpc_response = Protobuf::Socketrpc::Response.new(
      :response_proto => http_response.body,
      :error => "Bad response from the server.",
      :error_reason => Protobuf::Socketrpc::ErrorReason::BAD_RESPONSE_PROTO
    )
  end

  @response_data = rpc_response.encode()

  parse_response
end

#send_requestObject



12
13
14
15
16
17
# File 'lib/protobuf/rpc/connectors/http.rb', line 12

def send_request
  timeout_wrap do
    setup_connection
    post_init
  end
end