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

Inherits:
Base
  • Object
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!

Constructor Details

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

Instance Method Details

#baseObject



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

def base
  options[:base] || ''
end

#clientObject



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

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

#close_connectionObject

private



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

Returns:

  • (Boolean)


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

#hostObject



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

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

#log_signatureObject



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

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

#post_initObject



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_dataObject



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.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 && 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 && http_response.headers['x-protobuf-error']
    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



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