Module: Protobuf::Rpc::Connectors::Common
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
Instance Method Summary collapse
- #any_callbacks? ⇒ Boolean
- #complete ⇒ Object
- #data_callback(data) ⇒ Object
-
#fail(code, message) ⇒ Object
All failures should be routed through this method.
- #initialize_stats ⇒ Object
- #log_signature ⇒ Object
- #parse_response ⇒ Object
- #post_init ⇒ Object
- #request_bytes ⇒ Object
- #request_caller ⇒ Object
- #setup_connection ⇒ Object
- #succeed(response) ⇒ Object
-
#timeout_wrap(&block) ⇒ Object
Wrap the given block in a timeout of the configured number of seconds.
- #validate_request_type! ⇒ Object
- #verify_callbacks ⇒ Object
- #verify_options! ⇒ Object
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
8 9 10 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 8 def error @error end |
Instance Method Details
#any_callbacks? ⇒ Boolean
10 11 12 13 14 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 10 def any_callbacks? return [@complete_cb, @failure_cb, @success_cb].inject(false) do |reduction, cb| reduction = (reduction || !cb.nil?) end end |
#complete ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 20 def complete @stats.stop log_info { @stats.to_s } log_debug { ('Response proceessing complete') } @complete_cb.call(self) unless @complete_cb.nil? rescue => e log_error { ('Complete callback error encountered') } log_exception(e) raise end |
#data_callback(data) ⇒ Object
31 32 33 34 35 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 31 def data_callback(data) log_debug { ('Using data_callback') } @used_data_callback = true @data = data end |
#fail(code, message) ⇒ Object
All failures should be routed through this method.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 41 def fail(code, ) @error = ClientError.new @error.code = Protobuf::Socketrpc::ErrorReason.fetch(code) @error. = log_debug { ("Server failed request (invoking on_failure): #{@error.inspect}") } @stats.failure(code) @failure_cb.call(@error) unless @failure_cb.nil? rescue => e log_error { ("Failure callback error encountered") } log_exception(e) raise ensure complete end |
#initialize_stats ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 57 def initialize_stats @stats = Protobuf::Rpc::Stat.new(:CLIENT) @stats.server = [@options[:port], @options[:host]] @stats.service = @options[:service].name @stats.method_name = @options[:method].to_s rescue => ex log_exception(ex) fail(:RPC_ERROR, "Invalid stats configuration. #{ex.}") end |
#log_signature ⇒ Object
67 68 69 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 67 def log_signature @_log_signature ||= "[client-#{self.class}]" end |
#parse_response ⇒ Object
71 72 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 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 71 def parse_response # Close up the connection as we no longer need it close_connection log_debug { ("Parsing response from server (connection closed)") } # Parse out the raw response @stats.response_size = @response_data.size unless @response_data.nil? response_wrapper = Protobuf::Socketrpc::Response.decode(@response_data) # Determine success or failure based on parsed data if response_wrapper.has_field?(:error_reason) log_debug { ("Error response parsed") } # fail the call if we already know the client is failed # (don't try to parse out the response payload) fail(response_wrapper.error_reason, response_wrapper.error) else log_debug { ("Successful response parsed") } # Ensure client_response is an instance parsed = @options[:response_type].decode(response_wrapper.response_proto.to_s) if parsed.nil? and not response_wrapper.has_field?(:error_reason) fail(:BAD_RESPONSE_PROTO, 'Unable to parse response from server') else verify_callbacks succeed(parsed) return @data if @used_data_callback end end end |
#post_init ⇒ Object
104 105 106 107 108 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 104 def post_init send_data unless error? rescue => e fail(:RPC_ERROR, "Connection error: #{e.}") end |
#request_bytes ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 110 def request_bytes validate_request_type! fields = { :service_name => @options[:service].name, :method_name => @options[:method].to_s, :request_proto => @options[:request], :caller => request_caller } return ::Protobuf::Socketrpc::Request.encode(fields) rescue => e fail(:INVALID_REQUEST_PROTO, "Could not set request proto: #{e.}") end |
#request_caller ⇒ Object
16 17 18 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 16 def request_caller @options[:client_host] || ::Protobuf.client_host end |
#setup_connection ⇒ Object
122 123 124 125 126 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 122 def setup_connection initialize_stats @request_data = request_bytes @stats.request_size = request_bytes.size end |
#succeed(response) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 128 def succeed(response) log_debug { ("Server succeeded request (invoking on_success)") } @stats.success @success_cb.call(response) unless @success_cb.nil? rescue => e log_error { ("Success callback error encountered") } log_exception(e) fail(:RPC_ERROR, "An exception occurred while calling on_success: #{e.}") ensure complete end |
#timeout_wrap(&block) ⇒ Object
Wrap the given block in a timeout of the configured number of seconds.
142 143 144 145 146 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 142 def timeout_wrap(&block) ::Timeout.timeout([:timeout], &block) rescue ::Timeout::Error fail(:RPC_FAILED, "The server took longer than #{[:timeout]} seconds to respond") end |
#validate_request_type! ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 148 def validate_request_type! unless @options[:request].class == @options[:request_type] expected = @options[:request_type].name actual = @options[:request].class.name fail(:INVALID_REQUEST_PROTO, "Expected request type to be type of #{expected}, got #{actual} instead") end end |
#verify_callbacks ⇒ Object
156 157 158 159 160 161 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 156 def verify_callbacks unless any_callbacks? log_debug { ("No callbacks set, using data_callback") } @success_cb = @failure_cb = self.method(:data_callback) end end |
#verify_options! ⇒ Object
163 164 165 166 167 168 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 163 def # Verify the options that are necessary and merge them in [:service, :method, :host, :port].each do |opt| fail(:RPC_ERROR, "Invalid client connection configuration. #{opt} must be a defined option.") if @options[opt].nil? end end |