Class: Protobuf::Rpc::Connectors::Http
- Inherits:
-
Base
- Object
- Base
- Protobuf::Rpc::Connectors::Http
show all
- Includes:
- Logging
- Defined in:
- lib/protobuf/rpc/connectors/http.rb
Instance Attribute Summary
Attributes inherited from Base
#complete_cb, #error, #failure_cb, #options, #stats, #success_cb
Instance Method Summary
collapse
Methods included from Logging
initialize_logger, #log_exception, #logger, #sign_message
Methods inherited from Base
#any_callbacks?, #complete, #data_callback, #failure, #first_alive_load_balance?, #initialize, #initialize_stats, #parse_response, #ping_port, #ping_port_enabled?, #request_bytes, #request_caller, #setup_connection, #succeed, #timeout, #timeout_wrap, #validate_request_type!, #verify_callbacks, #verify_options!
Instance Method Details
#base ⇒ Object
42
43
44
|
# File 'lib/protobuf/rpc/connectors/http.rb', line 42
def base
options[:base] || ''
end
|
#client ⇒ Object
46
47
48
|
# File 'lib/protobuf/rpc/connectors/http.rb', line 46
def client
@_client ||= Faraday.new(:url => host)
end
|
#close_connection ⇒ Object
24
25
26
|
# File 'lib/protobuf/rpc/connectors/http.rb', line 24
def close_connection
logger.debug { sign_message('Connector closed') }
end
|
#error? ⇒ Boolean
Method to determine error state, must be used with Connector api
29
30
31
32
33
34
35
36
|
# File 'lib/protobuf/rpc/connectors/http.rb', line 29
def error?
logger.debug { sign_message("Error state : #{@error}") }
if @error
true
else
false
end
end
|
#host ⇒ Object
38
39
40
|
# File 'lib/protobuf/rpc/connectors/http.rb', line 38
def host
'http://' + options[:host] + ':' + options[:port].to_s
end
|
#log_signature ⇒ Object
18
19
20
|
# File 'lib/protobuf/rpc/connectors/http.rb', line 18
def log_signature
@_log_signature ||= "[http-client-#{self.class.name}]"
end
|
#post_init ⇒ Object
50
51
52
53
54
|
# File 'lib/protobuf/rpc/connectors/http.rb', line 50
def post_init
send_data unless error?
rescue => e
fail(:RPC_ERROR, "Connection error: #{e.message}")
end
|
#send_data ⇒ Object
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
87
88
89
90
91
|
# File 'lib/protobuf/rpc/connectors/http.rb', line 56
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.['Content-Type'] = 'application/x-protobuf'
http_request.['X-Protobuf-Caller'] = rpc_request[:caller] || ''
http_request.body = rpc_request[:request_proto]
end
if http_response.status == 200 && http_response.['x-protobuf-error'].nil?
rpc_response = Protobuf::Socketrpc::Response.new(
:response_proto => http_response.body
)
elsif http_response.status != 200 && http_response.['x-protobuf-error']
rpc_response = Protobuf::Socketrpc::Response.new(
:response_proto => http_response.body,
:error => http_response.['x-protobuf-error'],
:error_reason => http_response.['x-protobuf-error-reason'].to_i
)
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_request ⇒ Object
11
12
13
14
15
16
|
# File 'lib/protobuf/rpc/connectors/http.rb', line 11
def send_request
timeout_wrap do
setup_connection
post_init
end
end
|