protobuf-cucumber 3.10.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.rubocop.yml +70 -0
  4. data/.rubocop_todo.yml +145 -0
  5. data/.travis.yml +40 -0
  6. data/.yardopts +5 -0
  7. data/CHANGES.md +344 -0
  8. data/CONTRIBUTING.md +16 -0
  9. data/Gemfile +3 -0
  10. data/LICENSE.txt +22 -0
  11. data/README.md +33 -0
  12. data/Rakefile +64 -0
  13. data/bin/protoc-gen-ruby +22 -0
  14. data/bin/rpc_server +5 -0
  15. data/install-protobuf.sh +28 -0
  16. data/lib/protobuf.rb +129 -0
  17. data/lib/protobuf/cli.rb +257 -0
  18. data/lib/protobuf/code_generator.rb +120 -0
  19. data/lib/protobuf/decoder.rb +28 -0
  20. data/lib/protobuf/deprecation.rb +117 -0
  21. data/lib/protobuf/descriptors.rb +3 -0
  22. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +62 -0
  23. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +301 -0
  24. data/lib/protobuf/encoder.rb +11 -0
  25. data/lib/protobuf/enum.rb +365 -0
  26. data/lib/protobuf/exceptions.rb +9 -0
  27. data/lib/protobuf/field.rb +74 -0
  28. data/lib/protobuf/field/base_field.rb +380 -0
  29. data/lib/protobuf/field/base_field_object_definitions.rb +504 -0
  30. data/lib/protobuf/field/bool_field.rb +64 -0
  31. data/lib/protobuf/field/bytes_field.rb +78 -0
  32. data/lib/protobuf/field/double_field.rb +25 -0
  33. data/lib/protobuf/field/enum_field.rb +61 -0
  34. data/lib/protobuf/field/field_array.rb +104 -0
  35. data/lib/protobuf/field/field_hash.rb +122 -0
  36. data/lib/protobuf/field/fixed32_field.rb +25 -0
  37. data/lib/protobuf/field/fixed64_field.rb +28 -0
  38. data/lib/protobuf/field/float_field.rb +43 -0
  39. data/lib/protobuf/field/int32_field.rb +21 -0
  40. data/lib/protobuf/field/int64_field.rb +34 -0
  41. data/lib/protobuf/field/integer_field.rb +23 -0
  42. data/lib/protobuf/field/message_field.rb +51 -0
  43. data/lib/protobuf/field/sfixed32_field.rb +27 -0
  44. data/lib/protobuf/field/sfixed64_field.rb +28 -0
  45. data/lib/protobuf/field/signed_integer_field.rb +29 -0
  46. data/lib/protobuf/field/sint32_field.rb +21 -0
  47. data/lib/protobuf/field/sint64_field.rb +21 -0
  48. data/lib/protobuf/field/string_field.rb +51 -0
  49. data/lib/protobuf/field/uint32_field.rb +21 -0
  50. data/lib/protobuf/field/uint64_field.rb +21 -0
  51. data/lib/protobuf/field/varint_field.rb +77 -0
  52. data/lib/protobuf/generators/base.rb +85 -0
  53. data/lib/protobuf/generators/enum_generator.rb +39 -0
  54. data/lib/protobuf/generators/extension_generator.rb +27 -0
  55. data/lib/protobuf/generators/field_generator.rb +193 -0
  56. data/lib/protobuf/generators/file_generator.rb +262 -0
  57. data/lib/protobuf/generators/group_generator.rb +122 -0
  58. data/lib/protobuf/generators/message_generator.rb +104 -0
  59. data/lib/protobuf/generators/option_generator.rb +17 -0
  60. data/lib/protobuf/generators/printable.rb +160 -0
  61. data/lib/protobuf/generators/service_generator.rb +50 -0
  62. data/lib/protobuf/lifecycle.rb +33 -0
  63. data/lib/protobuf/logging.rb +39 -0
  64. data/lib/protobuf/message.rb +260 -0
  65. data/lib/protobuf/message/fields.rb +233 -0
  66. data/lib/protobuf/message/serialization.rb +85 -0
  67. data/lib/protobuf/optionable.rb +70 -0
  68. data/lib/protobuf/rpc/buffer.rb +78 -0
  69. data/lib/protobuf/rpc/client.rb +140 -0
  70. data/lib/protobuf/rpc/connectors/base.rb +221 -0
  71. data/lib/protobuf/rpc/connectors/ping.rb +89 -0
  72. data/lib/protobuf/rpc/connectors/socket.rb +78 -0
  73. data/lib/protobuf/rpc/connectors/zmq.rb +319 -0
  74. data/lib/protobuf/rpc/dynamic_discovery.pb.rb +50 -0
  75. data/lib/protobuf/rpc/env.rb +60 -0
  76. data/lib/protobuf/rpc/error.rb +28 -0
  77. data/lib/protobuf/rpc/error/client_error.rb +31 -0
  78. data/lib/protobuf/rpc/error/server_error.rb +43 -0
  79. data/lib/protobuf/rpc/middleware.rb +25 -0
  80. data/lib/protobuf/rpc/middleware/exception_handler.rb +40 -0
  81. data/lib/protobuf/rpc/middleware/logger.rb +95 -0
  82. data/lib/protobuf/rpc/middleware/request_decoder.rb +79 -0
  83. data/lib/protobuf/rpc/middleware/response_encoder.rb +83 -0
  84. data/lib/protobuf/rpc/middleware/runner.rb +18 -0
  85. data/lib/protobuf/rpc/rpc.pb.rb +64 -0
  86. data/lib/protobuf/rpc/rpc_method.rb +16 -0
  87. data/lib/protobuf/rpc/server.rb +39 -0
  88. data/lib/protobuf/rpc/servers/socket/server.rb +121 -0
  89. data/lib/protobuf/rpc/servers/socket/worker.rb +56 -0
  90. data/lib/protobuf/rpc/servers/socket_runner.rb +46 -0
  91. data/lib/protobuf/rpc/servers/zmq/broker.rb +194 -0
  92. data/lib/protobuf/rpc/servers/zmq/server.rb +321 -0
  93. data/lib/protobuf/rpc/servers/zmq/util.rb +48 -0
  94. data/lib/protobuf/rpc/servers/zmq/worker.rb +105 -0
  95. data/lib/protobuf/rpc/servers/zmq_runner.rb +70 -0
  96. data/lib/protobuf/rpc/service.rb +172 -0
  97. data/lib/protobuf/rpc/service_directory.rb +261 -0
  98. data/lib/protobuf/rpc/service_dispatcher.rb +45 -0
  99. data/lib/protobuf/rpc/service_filters.rb +250 -0
  100. data/lib/protobuf/rpc/stat.rb +119 -0
  101. data/lib/protobuf/socket.rb +21 -0
  102. data/lib/protobuf/tasks.rb +1 -0
  103. data/lib/protobuf/tasks/compile.rake +80 -0
  104. data/lib/protobuf/varint.rb +20 -0
  105. data/lib/protobuf/varint_pure.rb +31 -0
  106. data/lib/protobuf/version.rb +3 -0
  107. data/lib/protobuf/wire_type.rb +10 -0
  108. data/lib/protobuf/zmq.rb +21 -0
  109. data/profile.html +5103 -0
  110. data/proto/dynamic_discovery.proto +44 -0
  111. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  112. data/proto/google/protobuf/descriptor.proto +779 -0
  113. data/proto/rpc.proto +69 -0
  114. data/protobuf-cucumber.gemspec +57 -0
  115. data/spec/benchmark/tasks.rb +143 -0
  116. data/spec/bin/protoc-gen-ruby_spec.rb +23 -0
  117. data/spec/encoding/all_types_spec.rb +103 -0
  118. data/spec/encoding/extreme_values_spec.rb +0 -0
  119. data/spec/functional/class_inheritance_spec.rb +52 -0
  120. data/spec/functional/code_generator_spec.rb +58 -0
  121. data/spec/functional/socket_server_spec.rb +59 -0
  122. data/spec/functional/zmq_server_spec.rb +105 -0
  123. data/spec/lib/protobuf/cli_spec.rb +317 -0
  124. data/spec/lib/protobuf/code_generator_spec.rb +87 -0
  125. data/spec/lib/protobuf/enum_spec.rb +307 -0
  126. data/spec/lib/protobuf/field/bool_field_spec.rb +91 -0
  127. data/spec/lib/protobuf/field/double_field_spec.rb +9 -0
  128. data/spec/lib/protobuf/field/enum_field_spec.rb +44 -0
  129. data/spec/lib/protobuf/field/field_array_spec.rb +105 -0
  130. data/spec/lib/protobuf/field/field_hash_spec.rb +168 -0
  131. data/spec/lib/protobuf/field/fixed32_field_spec.rb +7 -0
  132. data/spec/lib/protobuf/field/fixed64_field_spec.rb +7 -0
  133. data/spec/lib/protobuf/field/float_field_spec.rb +90 -0
  134. data/spec/lib/protobuf/field/int32_field_spec.rb +120 -0
  135. data/spec/lib/protobuf/field/int64_field_spec.rb +7 -0
  136. data/spec/lib/protobuf/field/message_field_spec.rb +132 -0
  137. data/spec/lib/protobuf/field/sfixed32_field_spec.rb +9 -0
  138. data/spec/lib/protobuf/field/sfixed64_field_spec.rb +9 -0
  139. data/spec/lib/protobuf/field/sint32_field_spec.rb +9 -0
  140. data/spec/lib/protobuf/field/sint64_field_spec.rb +9 -0
  141. data/spec/lib/protobuf/field/string_field_spec.rb +79 -0
  142. data/spec/lib/protobuf/field/uint32_field_spec.rb +7 -0
  143. data/spec/lib/protobuf/field/uint64_field_spec.rb +7 -0
  144. data/spec/lib/protobuf/field_spec.rb +192 -0
  145. data/spec/lib/protobuf/generators/base_spec.rb +154 -0
  146. data/spec/lib/protobuf/generators/enum_generator_spec.rb +82 -0
  147. data/spec/lib/protobuf/generators/extension_generator_spec.rb +42 -0
  148. data/spec/lib/protobuf/generators/field_generator_spec.rb +197 -0
  149. data/spec/lib/protobuf/generators/file_generator_spec.rb +119 -0
  150. data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
  151. data/spec/lib/protobuf/generators/service_generator_spec.rb +99 -0
  152. data/spec/lib/protobuf/lifecycle_spec.rb +94 -0
  153. data/spec/lib/protobuf/message_spec.rb +944 -0
  154. data/spec/lib/protobuf/optionable_spec.rb +265 -0
  155. data/spec/lib/protobuf/rpc/client_spec.rb +66 -0
  156. data/spec/lib/protobuf/rpc/connectors/base_spec.rb +226 -0
  157. data/spec/lib/protobuf/rpc/connectors/ping_spec.rb +69 -0
  158. data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +34 -0
  159. data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +110 -0
  160. data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +62 -0
  161. data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +49 -0
  162. data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +115 -0
  163. data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +91 -0
  164. data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +38 -0
  165. data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +43 -0
  166. data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +55 -0
  167. data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +35 -0
  168. data/spec/lib/protobuf/rpc/service_directory_spec.rb +293 -0
  169. data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +35 -0
  170. data/spec/lib/protobuf/rpc/service_filters_spec.rb +517 -0
  171. data/spec/lib/protobuf/rpc/service_spec.rb +162 -0
  172. data/spec/lib/protobuf/rpc/stat_spec.rb +101 -0
  173. data/spec/lib/protobuf/varint_spec.rb +29 -0
  174. data/spec/lib/protobuf_spec.rb +105 -0
  175. data/spec/spec_helper.rb +42 -0
  176. data/spec/support/all.rb +6 -0
  177. data/spec/support/packed_field.rb +23 -0
  178. data/spec/support/protos/all_types.data.bin +0 -0
  179. data/spec/support/protos/all_types.data.txt +119 -0
  180. data/spec/support/protos/enum.pb.rb +63 -0
  181. data/spec/support/protos/enum.proto +37 -0
  182. data/spec/support/protos/extreme_values.data.bin +0 -0
  183. data/spec/support/protos/google_unittest.bin +0 -0
  184. data/spec/support/protos/google_unittest.pb.rb +798 -0
  185. data/spec/support/protos/google_unittest.proto +884 -0
  186. data/spec/support/protos/google_unittest_custom_options.bin +0 -0
  187. data/spec/support/protos/google_unittest_custom_options.pb.rb +361 -0
  188. data/spec/support/protos/google_unittest_custom_options.proto +424 -0
  189. data/spec/support/protos/google_unittest_import.pb.rb +55 -0
  190. data/spec/support/protos/google_unittest_import.proto +73 -0
  191. data/spec/support/protos/google_unittest_import_public.pb.rb +31 -0
  192. data/spec/support/protos/google_unittest_import_public.proto +41 -0
  193. data/spec/support/protos/map-test.bin +157 -0
  194. data/spec/support/protos/map-test.pb.rb +85 -0
  195. data/spec/support/protos/map-test.proto +68 -0
  196. data/spec/support/protos/multi_field_extensions.pb.rb +59 -0
  197. data/spec/support/protos/multi_field_extensions.proto +35 -0
  198. data/spec/support/protos/resource.pb.rb +172 -0
  199. data/spec/support/protos/resource.proto +137 -0
  200. data/spec/support/resource_service.rb +23 -0
  201. data/spec/support/server.rb +65 -0
  202. data/spec/support/test_app_file.rb +2 -0
  203. data/varint_prof.rb +82 -0
  204. metadata +579 -0
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ ##
4
+ # This file is auto-generated. DO NOT EDIT!
5
+ #
6
+ require 'protobuf'
7
+
8
+ module Protobuf
9
+ module Rpc
10
+ module DynamicDiscovery
11
+ ::Protobuf::Optionable.inject(self) { ::Google::Protobuf::FileOptions }
12
+
13
+ ##
14
+ # Enum Classes
15
+ #
16
+ class BeaconType < ::Protobuf::Enum
17
+ define :HEARTBEAT, 0
18
+ define :FLATLINE, 1
19
+ end
20
+
21
+
22
+ ##
23
+ # Message Classes
24
+ #
25
+ class Server < ::Protobuf::Message; end
26
+ class Beacon < ::Protobuf::Message; end
27
+
28
+
29
+ ##
30
+ # Message Fields
31
+ #
32
+ class Server
33
+ optional :string, :uuid, 1
34
+ optional :string, :address, 2
35
+ optional :string, :port, 3
36
+ optional :int32, :ttl, 4
37
+ repeated :string, :services, 5
38
+ end
39
+
40
+ class Beacon
41
+ optional ::Protobuf::Rpc::DynamicDiscovery::BeaconType, :beacon_type, 1
42
+ optional ::Protobuf::Rpc::DynamicDiscovery::Server, :server, 2
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
@@ -0,0 +1,60 @@
1
+ module Protobuf
2
+ module Rpc
3
+ class Env < Hash
4
+ # Creates an accessor that simply sets and reads a key in the hash:
5
+ #
6
+ # class Config < Hash
7
+ # hash_accessor :app
8
+ # end
9
+ #
10
+ # config = Config.new
11
+ # config.app = Foo
12
+ # config[:app] #=> Foo
13
+ #
14
+ # config[:app] = Bar
15
+ # config.app #=> Bar
16
+ #
17
+ def self.hash_accessor(*names) #:nodoc:
18
+ names.each do |name|
19
+ name_str = name.to_s.freeze
20
+
21
+ define_method name do
22
+ self[name_str]
23
+ end
24
+
25
+ define_method "#{name}=" do |value|
26
+ self[name_str] = value
27
+ end
28
+
29
+ define_method "#{name}?" do
30
+ !self[name_str].nil?
31
+ end
32
+ end
33
+ end
34
+
35
+ # TODO: Add extra info about the environment (i.e. variables) and other
36
+ # information that might be useful
37
+ hash_accessor :client_host,
38
+ :encoded_request,
39
+ :encoded_response,
40
+ :log_signature,
41
+ :method_name,
42
+ :request,
43
+ :request_type,
44
+ :request_wrapper,
45
+ :response,
46
+ :response_type,
47
+ :rpc_method,
48
+ :rpc_service,
49
+ :server,
50
+ :service_name,
51
+ :worker_id
52
+
53
+ def initialize(options = {})
54
+ merge!(options)
55
+
56
+ self['worker_id'] = ::Thread.current.object_id.to_s(16)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,28 @@
1
+ require 'protobuf/rpc/rpc.pb'
2
+
3
+ module Protobuf
4
+ module Rpc
5
+ ClientError = Struct.new("ClientError", :code, :message)
6
+
7
+ # Base PbError class for client and server errors
8
+ class PbError < StandardError
9
+ attr_reader :error_type
10
+
11
+ def initialize(message = 'An unknown RpcError occurred', error_type = 'RPC_ERROR')
12
+ @error_type = error_type.is_a?(String) ? ::Protobuf::Socketrpc::ErrorReason.const_get(error_type) : error_type
13
+ super message
14
+ end
15
+
16
+ def encode
17
+ to_response.encode
18
+ end
19
+
20
+ def to_response
21
+ ::Protobuf::Socketrpc::Response.new(:error => message, :error_reason => error_type)
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ require 'protobuf/rpc/error/server_error'
28
+ require 'protobuf/rpc/error/client_error'
@@ -0,0 +1,31 @@
1
+ require 'protobuf/rpc/error'
2
+
3
+ module Protobuf
4
+ module Rpc
5
+
6
+ class InvalidRequestProto < PbError
7
+ def initialize(message = 'Invalid request type given')
8
+ super message, 'INVALID_REQUEST_PROTO'
9
+ end
10
+ end
11
+
12
+ class BadResponseProto < PbError
13
+ def initialize(message = 'Bad response type from server')
14
+ super message, 'BAD_RESPONSE_PROTO'
15
+ end
16
+ end
17
+
18
+ class UnkownHost < PbError
19
+ def initialize(message = 'Unknown host or port')
20
+ super message, 'UNKNOWN_HOST'
21
+ end
22
+ end
23
+
24
+ class IOError < PbError
25
+ def initialize(message = 'IO Error occurred')
26
+ super message, 'IO_ERROR'
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,43 @@
1
+ require 'protobuf/rpc/rpc.pb'
2
+
3
+ module Protobuf
4
+ module Rpc
5
+
6
+ class BadRequestData < PbError
7
+ def initialize(message = 'Unable to parse request')
8
+ super message, 'BAD_REQUEST_DATA'
9
+ end
10
+ end
11
+
12
+ class BadRequestProto < PbError
13
+ def initialize(message = 'Request is of wrong type')
14
+ super message, 'BAD_REQUEST_PROTO'
15
+ end
16
+ end
17
+
18
+ class ServiceNotFound < PbError
19
+ def initialize(message = 'Service class not found')
20
+ super message, 'SERVICE_NOT_FOUND'
21
+ end
22
+ end
23
+
24
+ class MethodNotFound < PbError
25
+ def initialize(message = 'Service method not found')
26
+ super message, 'METHOD_NOT_FOUND'
27
+ end
28
+ end
29
+
30
+ class RpcError < PbError
31
+ def initialize(message = 'RPC exception occurred')
32
+ super message, 'RPC_ERROR'
33
+ end
34
+ end
35
+
36
+ class RpcFailed < PbError
37
+ def initialize(message = 'RPC failed')
38
+ super message, 'RPC_FAILED'
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,25 @@
1
+ require 'middleware'
2
+
3
+ require 'protobuf/rpc/middleware/exception_handler'
4
+ require 'protobuf/rpc/middleware/logger'
5
+ require 'protobuf/rpc/middleware/request_decoder'
6
+ require 'protobuf/rpc/middleware/response_encoder'
7
+ require 'protobuf/rpc/middleware/runner'
8
+
9
+ module Protobuf
10
+ module Rpc
11
+ def self.middleware
12
+ @middleware ||= ::Middleware::Builder.new(:runner_class => ::Protobuf::Rpc::Middleware::Runner)
13
+ end
14
+
15
+ # Ensure the middleware stack is initialized
16
+ middleware
17
+ end
18
+
19
+ Rpc.middleware.use(Rpc::Middleware::ExceptionHandler)
20
+ Rpc.middleware.use(Rpc::Middleware::RequestDecoder)
21
+ Rpc.middleware.use(Rpc::Middleware::Logger)
22
+ Rpc.middleware.use(Rpc::Middleware::ResponseEncoder)
23
+
24
+ ActiveSupport.run_load_hooks(:protobuf_rpc_middleware, Rpc)
25
+ end
@@ -0,0 +1,40 @@
1
+ module Protobuf
2
+ module Rpc
3
+ module Middleware
4
+ class ExceptionHandler
5
+ include ::Protobuf::Logging
6
+
7
+ attr_reader :app
8
+
9
+ def initialize(app)
10
+ @app = app
11
+ end
12
+
13
+ def call(env)
14
+ dup._call(env)
15
+ end
16
+
17
+ def _call(env)
18
+ app.call(env)
19
+ rescue => exception
20
+ log_exception(exception)
21
+
22
+ # Rescue exceptions, re-wrap them as generic Protobuf errors,
23
+ # and encode them
24
+ env.response = wrap_exception(exception)
25
+ env.encoded_response = env.response.encode
26
+ env
27
+ end
28
+
29
+ private
30
+
31
+ # Wrap exceptions in a generic Protobuf errors unless they already are
32
+ #
33
+ def wrap_exception(exception)
34
+ exception = RpcFailed.new(exception.message) unless exception.is_a?(PbError)
35
+ exception
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,95 @@
1
+ module Protobuf
2
+ module Rpc
3
+ module Middleware
4
+ class Logger
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ # TODO: Figure out how to control when logs are flushed
10
+ def call(env)
11
+ dup._call(env)
12
+ end
13
+
14
+ def _call(env)
15
+ instrumenter.start
16
+ instrumenter.flush(env) # Log request stats
17
+
18
+ env = @app.call(env)
19
+
20
+ instrumenter.stop
21
+ instrumenter.flush(env) # Log response stats
22
+
23
+ env
24
+ end
25
+
26
+ private
27
+
28
+ def instrumenter
29
+ @instrumenter ||= Instrumenter.new
30
+ end
31
+
32
+ # TODO: Replace this with ActiveSupport::Notifications and log subscribers
33
+ # TODO: Consider adopting Rails-style logging so we can track serialization
34
+ # time as well as ActiveRecord time, etc.:
35
+ #
36
+ # Started GET "/" for 127.0.0.1 at 2014-02-12 09:40:29 -0700
37
+ # Processing by ReleasesController#index as HTML
38
+ # Rendered releases/_release.html.erb (0.0ms)
39
+ # Rendered releases/_release.html.erb (0.0ms)
40
+ # Rendered releases/_release.html.erb (0.0ms)
41
+ # Rendered releases/_release.html.erb (0.0ms)
42
+ # Rendered releases/index.html.erb within layouts/application (11.0ms)
43
+ # Completed 200 OK in 142ms (Views: 117.6ms | ActiveRecord: 1.7ms)
44
+ #
45
+ class Instrumenter
46
+ attr_reader :env
47
+
48
+ def flush(env)
49
+ ::Protobuf::Logging.logger.info { to_s(env) }
50
+ end
51
+
52
+ def start
53
+ @start_time = ::Time.now.utc
54
+ end
55
+
56
+ def stop
57
+ @end_time = ::Time.now.utc
58
+ end
59
+
60
+ def to_s(env)
61
+ @env = env
62
+
63
+ [
64
+ "[SRV]",
65
+ env.client_host,
66
+ env.worker_id,
67
+ rpc,
68
+ sizes,
69
+ elapsed_time,
70
+ @end_time.try(:iso8601),
71
+ ].compact.join(' - ')
72
+ end
73
+
74
+ private
75
+
76
+ def elapsed_time
77
+ (@start_time && @end_time ? "#{(@end_time - @start_time).round(4)}s" : nil)
78
+ end
79
+
80
+ def rpc
81
+ env.service_name && env.method_name ? "#{env.service_name}##{env.method_name}" : nil
82
+ end
83
+
84
+ def sizes
85
+ if env.encoded_response?
86
+ "#{env.encoded_request.size}B/#{env.encoded_response.size}B"
87
+ else
88
+ "#{env.encoded_request.size}B/-"
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,79 @@
1
+ module Protobuf
2
+ module Rpc
3
+ module Middleware
4
+ class RequestDecoder
5
+ include ::Protobuf::Logging
6
+
7
+ attr_reader :app, :env
8
+
9
+ def initialize(app)
10
+ @app = app
11
+ end
12
+
13
+ def call(env)
14
+ dup._call(env)
15
+ end
16
+
17
+ def _call(env)
18
+ @env = env
19
+
20
+ logger.debug { sign_message("Decoding request: #{env.encoded_request}") }
21
+ env.service_name = service_name
22
+ env.method_name = method_name
23
+ env.request = request
24
+ env.request_wrapper = request_wrapper
25
+ env.client_host = request_wrapper.caller
26
+
27
+ env.rpc_service = service
28
+ env.rpc_method = rpc_method
29
+ env.request_type = rpc_method.request_type
30
+ env.response_type = rpc_method.response_type
31
+
32
+ app.call(env)
33
+ end
34
+
35
+ def log_signature
36
+ env.log_signature || super
37
+ end
38
+
39
+ private
40
+
41
+ def method_name
42
+ return @method_name unless @method_name.nil?
43
+
44
+ @method_name = request_wrapper.method_name.underscore.to_sym
45
+ fail MethodNotFound, "#{service.name}##{@method_name} is not a defined RPC method." unless service.rpc_method?(@method_name)
46
+ @method_name
47
+ end
48
+
49
+ def request
50
+ @request ||= rpc_method.request_type.decode(request_wrapper.request_proto)
51
+ rescue => exception
52
+ raise BadRequestData, "Unable to decode request: #{exception.message}"
53
+ end
54
+
55
+ # Decode the incoming request object into our expected request object
56
+ #
57
+ def request_wrapper
58
+ @request_wrapper ||= ::Protobuf::Socketrpc::Request.decode(env.encoded_request)
59
+ rescue => exception
60
+ raise BadRequestData, "Unable to decode request: #{exception.message}"
61
+ end
62
+
63
+ def rpc_method
64
+ @rpc_method ||= service.rpcs[method_name]
65
+ end
66
+
67
+ def service
68
+ @service ||= service_name.constantize
69
+ rescue NameError
70
+ raise ServiceNotFound, "Service class #{service_name} is not defined."
71
+ end
72
+
73
+ def service_name
74
+ @service_name ||= request_wrapper.service_name
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end