grpc 1.6.7 → 1.7.0.pre1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +579 -77
  3. data/include/grpc/byte_buffer.h +1 -63
  4. data/include/grpc/compression.h +27 -5
  5. data/include/grpc/fork.h +24 -0
  6. data/include/grpc/grpc.h +12 -6
  7. data/include/grpc/grpc_security.h +28 -7
  8. data/include/grpc/impl/codegen/atm.h +1 -0
  9. data/include/grpc/impl/codegen/byte_buffer.h +86 -0
  10. data/include/grpc/impl/codegen/compression_types.h +63 -5
  11. data/include/grpc/impl/codegen/fork.h +48 -0
  12. data/include/grpc/impl/codegen/grpc_types.h +26 -9
  13. data/include/grpc/impl/codegen/port_platform.h +11 -4
  14. data/include/grpc/impl/codegen/slice.h +6 -1
  15. data/include/grpc/impl/codegen/sync.h +3 -1
  16. data/include/grpc/impl/codegen/sync_custom.h +36 -0
  17. data/include/grpc/module.modulemap +75 -3
  18. data/include/grpc/slice.h +1 -5
  19. data/include/grpc/support/sync_custom.h +24 -0
  20. data/src/core/ext/census/base_resources.c +14 -14
  21. data/src/core/ext/census/context.c +7 -5
  22. data/src/core/ext/census/grpc_filter.c +12 -14
  23. data/src/core/ext/census/mlog.c +2 -1
  24. data/src/core/ext/census/resource.c +13 -9
  25. data/src/core/ext/filters/client_channel/channel_connectivity.c +15 -8
  26. data/src/core/ext/filters/client_channel/client_channel.c +418 -439
  27. data/src/core/ext/filters/client_channel/client_channel_factory.c +4 -5
  28. data/src/core/ext/filters/client_channel/client_channel_plugin.c +2 -2
  29. data/src/core/ext/filters/client_channel/http_connect_handshaker.c +7 -5
  30. data/src/core/ext/filters/client_channel/http_proxy.c +17 -21
  31. data/src/core/ext/filters/client_channel/lb_policy.c +1 -1
  32. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.c +7 -7
  33. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.c +371 -257
  34. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.c +7 -5
  35. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.c +25 -14
  36. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.c +16 -16
  37. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.c +33 -28
  38. data/src/core/ext/filters/client_channel/lb_policy_factory.c +10 -8
  39. data/src/core/ext/filters/client_channel/lb_policy_factory.h +1 -1
  40. data/src/core/ext/filters/client_channel/proxy_mapper_registry.c +1 -1
  41. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.c +7 -6
  42. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.c +62 -28
  43. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.c +29 -23
  44. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.c +25 -14
  45. data/src/core/ext/filters/client_channel/retry_throttle.c +9 -6
  46. data/src/core/ext/filters/client_channel/subchannel.c +30 -30
  47. data/src/core/ext/filters/client_channel/subchannel.h +1 -4
  48. data/src/core/ext/filters/client_channel/subchannel_index.c +31 -15
  49. data/src/core/ext/filters/client_channel/subchannel_index.h +7 -0
  50. data/src/core/ext/filters/client_channel/uri_parser.c +4 -3
  51. data/src/core/ext/filters/deadline/deadline_filter.c +78 -39
  52. data/src/core/ext/filters/deadline/deadline_filter.h +7 -1
  53. data/src/core/ext/filters/http/client/http_client_filter.c +14 -14
  54. data/src/core/ext/filters/http/http_filters_plugin.c +1 -1
  55. data/src/core/ext/filters/http/message_compress/message_compress_filter.c +240 -175
  56. data/src/core/ext/filters/http/server/http_server_filter.c +48 -36
  57. data/src/core/ext/filters/load_reporting/{load_reporting_filter.c → server_load_reporting_filter.c} +11 -12
  58. data/src/core/ext/filters/load_reporting/{load_reporting_filter.h → server_load_reporting_filter.h} +6 -5
  59. data/src/core/ext/filters/load_reporting/{load_reporting.c → server_load_reporting_plugin.c} +19 -13
  60. data/src/core/ext/filters/load_reporting/{load_reporting.h → server_load_reporting_plugin.h} +4 -3
  61. data/src/core/ext/filters/max_age/max_age_filter.c +2 -3
  62. data/src/core/ext/filters/message_size/message_size_filter.c +4 -2
  63. data/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.c +0 -1
  64. data/src/core/ext/transport/chttp2/client/chttp2_connector.c +5 -5
  65. data/src/core/ext/transport/chttp2/client/insecure/channel_create.c +1 -1
  66. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.c +1 -1
  67. data/src/core/ext/transport/chttp2/server/chttp2_server.c +20 -18
  68. data/src/core/ext/transport/chttp2/transport/chttp2_plugin.c +1 -0
  69. data/src/core/ext/transport/chttp2/transport/chttp2_transport.c +493 -210
  70. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +1 -0
  71. data/src/core/ext/transport/chttp2/transport/flow_control.c +9 -8
  72. data/src/core/ext/transport/chttp2/transport/frame_data.c +2 -2
  73. data/src/core/ext/transport/chttp2/transport/frame_goaway.c +2 -2
  74. data/src/core/ext/transport/chttp2/transport/frame_ping.c +5 -4
  75. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.c +1 -1
  76. data/src/core/ext/transport/chttp2/transport/frame_settings.c +10 -9
  77. data/src/core/ext/transport/chttp2/transport/frame_window_update.c +9 -5
  78. data/src/core/ext/transport/chttp2/transport/hpack_encoder.c +62 -41
  79. data/src/core/ext/transport/chttp2/transport/hpack_parser.c +52 -8
  80. data/src/core/ext/transport/chttp2/transport/hpack_table.c +2 -2
  81. data/src/core/ext/transport/chttp2/transport/incoming_metadata.c +3 -2
  82. data/src/core/ext/transport/chttp2/transport/internal.h +60 -30
  83. data/src/core/ext/transport/chttp2/transport/parsing.c +16 -5
  84. data/src/core/ext/transport/chttp2/transport/stream_lists.c +36 -16
  85. data/src/core/ext/transport/chttp2/transport/stream_map.c +6 -4
  86. data/src/core/ext/transport/chttp2/transport/writing.c +133 -105
  87. data/src/core/ext/transport/inproc/inproc_transport.c +61 -65
  88. data/src/core/lib/channel/channel_args.c +112 -12
  89. data/src/core/lib/channel/channel_args.h +31 -0
  90. data/src/core/lib/channel/channel_stack.c +1 -15
  91. data/src/core/lib/channel/channel_stack.h +3 -10
  92. data/src/core/lib/channel/channel_stack_builder.c +41 -10
  93. data/src/core/lib/channel/channel_stack_builder.h +10 -0
  94. data/src/core/lib/channel/connected_channel.c +94 -23
  95. data/src/core/lib/channel/handshaker.c +8 -6
  96. data/src/core/lib/channel/handshaker_registry.c +1 -1
  97. data/src/core/lib/compression/algorithm_metadata.h +14 -0
  98. data/src/core/lib/compression/compression.c +101 -1
  99. data/src/core/lib/compression/stream_compression.c +32 -146
  100. data/src/core/lib/compression/stream_compression.h +28 -4
  101. data/src/core/lib/compression/stream_compression_gzip.c +228 -0
  102. data/src/core/lib/{iomgr/ev_epoll_thread_pool_linux.h → compression/stream_compression_gzip.h} +5 -7
  103. data/src/core/lib/compression/stream_compression_identity.c +94 -0
  104. data/src/core/lib/{iomgr/ev_epoll_limited_pollers_linux.h → compression/stream_compression_identity.h} +7 -8
  105. data/src/core/lib/debug/stats.c +174 -0
  106. data/src/core/lib/debug/stats.h +61 -0
  107. data/src/core/lib/debug/stats_data.c +687 -0
  108. data/src/core/lib/debug/stats_data.h +470 -0
  109. data/src/core/lib/debug/trace.c +3 -3
  110. data/src/core/lib/debug/trace.h +1 -1
  111. data/src/core/lib/http/format_request.c +1 -1
  112. data/src/core/lib/http/httpcli.c +8 -7
  113. data/src/core/lib/http/httpcli_security_connector.c +2 -1
  114. data/src/core/lib/http/parser.c +4 -3
  115. data/src/core/lib/iomgr/call_combiner.c +202 -0
  116. data/src/core/lib/iomgr/call_combiner.h +121 -0
  117. data/src/core/lib/iomgr/closure.c +18 -4
  118. data/src/core/lib/iomgr/combiner.c +11 -4
  119. data/src/core/lib/iomgr/error.c +26 -24
  120. data/src/core/lib/iomgr/ev_epoll1_linux.c +395 -212
  121. data/src/core/lib/iomgr/ev_epollex_linux.c +141 -128
  122. data/src/core/lib/iomgr/ev_epollsig_linux.c +44 -41
  123. data/src/core/lib/iomgr/ev_poll_posix.c +99 -75
  124. data/src/core/lib/iomgr/ev_posix.c +5 -9
  125. data/src/core/lib/iomgr/ev_posix.h +1 -1
  126. data/src/core/lib/iomgr/exec_ctx.h +6 -1
  127. data/src/core/lib/iomgr/executor.c +142 -36
  128. data/src/core/lib/iomgr/executor.h +6 -1
  129. data/src/core/lib/iomgr/fork_posix.c +88 -0
  130. data/src/core/lib/iomgr/fork_windows.c +39 -0
  131. data/src/core/lib/iomgr/iocp_windows.c +2 -0
  132. data/src/core/lib/iomgr/iomgr.c +2 -8
  133. data/src/core/lib/iomgr/is_epollexclusive_available.c +6 -6
  134. data/src/core/lib/iomgr/load_file.c +2 -1
  135. data/src/core/lib/iomgr/polling_entity.c +9 -9
  136. data/src/core/lib/iomgr/polling_entity.h +7 -1
  137. data/src/core/lib/iomgr/pollset.h +1 -1
  138. data/src/core/lib/iomgr/pollset_uv.c +1 -1
  139. data/src/core/lib/iomgr/pollset_windows.c +3 -3
  140. data/src/core/lib/iomgr/port.h +4 -0
  141. data/src/core/lib/iomgr/resolve_address_posix.c +8 -7
  142. data/src/core/lib/iomgr/resolve_address_windows.c +1 -1
  143. data/src/core/lib/iomgr/resource_quota.c +24 -19
  144. data/src/core/lib/iomgr/socket_factory_posix.c +4 -4
  145. data/src/core/lib/iomgr/socket_mutator.c +4 -4
  146. data/src/core/lib/iomgr/socket_utils_windows.c +0 -4
  147. data/src/core/lib/iomgr/tcp_client_posix.c +5 -4
  148. data/src/core/lib/iomgr/tcp_posix.c +181 -20
  149. data/src/core/lib/iomgr/tcp_server_posix.c +8 -7
  150. data/src/core/lib/iomgr/tcp_server_utils_posix_common.c +1 -1
  151. data/src/core/lib/iomgr/timer.h +4 -0
  152. data/src/core/lib/iomgr/timer_generic.c +138 -3
  153. data/src/core/lib/iomgr/timer_generic.h +3 -0
  154. data/src/core/lib/iomgr/timer_heap.c +4 -4
  155. data/src/core/lib/iomgr/timer_manager.c +2 -2
  156. data/src/core/lib/iomgr/timer_uv.c +2 -0
  157. data/src/core/lib/iomgr/udp_server.c +10 -8
  158. data/src/core/lib/iomgr/unix_sockets_posix.c +4 -2
  159. data/src/core/lib/iomgr/wakeup_fd_cv.c +9 -8
  160. data/src/core/lib/iomgr/wakeup_fd_cv.h +2 -2
  161. data/src/core/lib/json/json.c +1 -1
  162. data/src/core/lib/json/json_string.c +13 -13
  163. data/src/core/lib/profiling/timers.h +18 -8
  164. data/src/core/lib/security/credentials/composite/composite_credentials.c +4 -10
  165. data/src/core/lib/security/credentials/google_default/google_default_credentials.c +2 -1
  166. data/src/core/lib/security/credentials/jwt/jwt_verifier.c +11 -6
  167. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.c +4 -4
  168. data/src/core/lib/security/credentials/plugin/plugin_credentials.c +132 -50
  169. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +2 -0
  170. data/src/core/lib/security/transport/client_auth_filter.c +68 -135
  171. data/src/core/lib/security/transport/secure_endpoint.c +110 -90
  172. data/src/core/lib/security/transport/secure_endpoint.h +8 -3
  173. data/src/core/lib/security/transport/security_connector.c +10 -12
  174. data/src/core/lib/security/transport/security_handshaker.c +45 -24
  175. data/src/core/lib/security/transport/server_auth_filter.c +71 -20
  176. data/src/core/lib/slice/b64.c +2 -2
  177. data/src/core/lib/slice/slice.c +16 -14
  178. data/src/core/lib/slice/slice_buffer.c +5 -4
  179. data/src/core/lib/slice/slice_hash_table.c +3 -2
  180. data/src/core/lib/slice/slice_intern.c +8 -5
  181. data/src/core/lib/support/block_annotate.h +22 -0
  182. data/src/core/lib/support/fork.c +62 -0
  183. data/src/core/lib/support/fork.h +35 -0
  184. data/src/core/lib/support/log_linux.c +1 -1
  185. data/src/core/lib/support/string.c +15 -1
  186. data/src/core/lib/support/string.h +3 -0
  187. data/src/core/lib/support/thd_internal.h +6 -0
  188. data/src/core/lib/support/thd_posix.c +56 -0
  189. data/src/core/lib/support/thd_windows.c +2 -0
  190. data/src/core/lib/surface/alarm.c +22 -15
  191. data/src/core/lib/surface/byte_buffer.c +4 -2
  192. data/src/core/lib/surface/call.c +442 -141
  193. data/src/core/lib/surface/call.h +6 -6
  194. data/src/core/lib/surface/call_log_batch.c +1 -1
  195. data/src/core/lib/surface/call_test_only.h +12 -0
  196. data/src/core/lib/surface/channel.c +39 -4
  197. data/src/core/lib/surface/channel_init.c +6 -6
  198. data/src/core/lib/surface/channel_ping.c +2 -2
  199. data/src/core/lib/surface/completion_queue.c +56 -57
  200. data/src/core/lib/surface/init.c +17 -3
  201. data/src/core/lib/surface/init_secure.c +5 -1
  202. data/src/core/lib/surface/lame_client.cc +9 -10
  203. data/src/core/lib/surface/server.c +81 -72
  204. data/src/core/lib/surface/version.c +2 -2
  205. data/src/core/lib/transport/byte_stream.c +1 -0
  206. data/src/core/lib/transport/byte_stream.h +3 -1
  207. data/src/core/lib/transport/connectivity_state.c +2 -1
  208. data/src/core/lib/transport/metadata.c +7 -4
  209. data/src/core/lib/transport/metadata_batch.c +18 -16
  210. data/src/core/lib/transport/metadata_batch.h +1 -0
  211. data/src/core/lib/transport/service_config.c +5 -3
  212. data/src/core/lib/transport/static_metadata.c +395 -614
  213. data/src/core/lib/transport/static_metadata.h +165 -133
  214. data/src/core/lib/transport/status_conversion.c +1 -1
  215. data/src/core/lib/transport/transport.c +20 -20
  216. data/src/core/lib/transport/transport.h +8 -5
  217. data/src/core/lib/transport/transport_impl.h +0 -3
  218. data/src/core/lib/transport/transport_op_string.c +8 -1
  219. data/src/core/plugin_registry/grpc_plugin_registry.c +4 -4
  220. data/src/core/tsi/fake_transport_security.c +133 -2
  221. data/src/core/tsi/fake_transport_security.h +5 -0
  222. data/src/core/tsi/ssl_transport_security.c +105 -8
  223. data/src/core/tsi/ssl_transport_security.h +30 -7
  224. data/src/core/tsi/transport_security.h +8 -2
  225. data/src/core/tsi/transport_security_grpc.c +20 -13
  226. data/src/core/tsi/transport_security_grpc.h +13 -9
  227. data/src/ruby/ext/grpc/rb_call_credentials.c +6 -2
  228. data/src/ruby/ext/grpc/rb_grpc.c +1 -1
  229. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +30 -20
  230. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +50 -35
  231. data/src/ruby/lib/grpc.rb +1 -0
  232. data/src/ruby/lib/grpc/generic/active_call.rb +34 -9
  233. data/src/ruby/lib/grpc/generic/bidi_call.rb +19 -10
  234. data/src/ruby/lib/grpc/generic/client_stub.rb +95 -38
  235. data/src/ruby/lib/grpc/generic/interceptor_registry.rb +53 -0
  236. data/src/ruby/lib/grpc/generic/interceptors.rb +186 -0
  237. data/src/ruby/lib/grpc/generic/rpc_desc.rb +66 -20
  238. data/src/ruby/lib/grpc/generic/rpc_server.rb +15 -3
  239. data/src/ruby/lib/grpc/google_rpc_status_utils.rb +1 -2
  240. data/src/ruby/lib/grpc/version.rb +1 -1
  241. data/src/ruby/pb/grpc/testing/duplicate/echo_duplicate_services_pb.rb +1 -0
  242. data/src/ruby/spec/channel_connection_spec.rb +1 -34
  243. data/src/ruby/spec/client_server_spec.rb +188 -82
  244. data/src/ruby/spec/generic/active_call_spec.rb +65 -11
  245. data/src/ruby/spec/generic/client_interceptors_spec.rb +153 -0
  246. data/src/ruby/spec/generic/interceptor_registry_spec.rb +65 -0
  247. data/src/ruby/spec/generic/rpc_desc_spec.rb +38 -0
  248. data/src/ruby/spec/generic/rpc_server_spec.rb +1 -34
  249. data/src/ruby/spec/generic/server_interceptors_spec.rb +218 -0
  250. data/src/ruby/spec/spec_helper.rb +4 -0
  251. data/src/ruby/spec/support/helpers.rb +73 -0
  252. data/src/ruby/spec/support/services.rb +147 -0
  253. data/third_party/cares/ares_build.h +21 -62
  254. data/third_party/cares/cares/ares.h +23 -1
  255. data/third_party/cares/cares/ares__close_sockets.c +2 -2
  256. data/third_party/cares/cares/ares_create_query.c +3 -3
  257. data/third_party/cares/cares/ares_expand_name.c +6 -2
  258. data/third_party/cares/cares/ares_expand_string.c +1 -1
  259. data/third_party/cares/cares/ares_getnameinfo.c +27 -7
  260. data/third_party/cares/cares/ares_init.c +407 -39
  261. data/third_party/cares/cares/ares_library_init.c +10 -0
  262. data/third_party/cares/cares/ares_library_init.h +2 -1
  263. data/third_party/cares/cares/ares_nowarn.c +6 -6
  264. data/third_party/cares/cares/ares_nowarn.h +2 -2
  265. data/third_party/cares/cares/ares_parse_naptr_reply.c +6 -1
  266. data/third_party/cares/cares/ares_private.h +11 -0
  267. data/third_party/cares/cares/ares_process.c +126 -37
  268. data/third_party/cares/cares/ares_version.h +2 -2
  269. data/third_party/cares/cares/ares_writev.c +2 -2
  270. data/third_party/cares/cares/config-win32.h +8 -34
  271. data/third_party/cares/cares/inet_net_pton.c +2 -2
  272. data/third_party/cares/cares/setup_once.h +5 -5
  273. data/third_party/cares/config_darwin/ares_config.h +98 -196
  274. data/third_party/cares/config_linux/ares_config.h +103 -203
  275. metadata +47 -20
  276. data/src/core/lib/iomgr/ev_epoll_limited_pollers_linux.c +0 -1957
  277. data/src/core/lib/iomgr/ev_epoll_thread_pool_linux.c +0 -1182
@@ -24,6 +24,7 @@ require_relative 'grpc/generic/active_call'
24
24
  require_relative 'grpc/generic/client_stub'
25
25
  require_relative 'grpc/generic/service'
26
26
  require_relative 'grpc/generic/rpc_server'
27
+ require_relative 'grpc/generic/interceptors'
27
28
 
28
29
  begin
29
30
  file = File.open(ssl_roots_path)
@@ -154,6 +154,15 @@ module GRPC
154
154
  Operation.new(self)
155
155
  end
156
156
 
157
+ ##
158
+ # Returns a restricted view of this ActiveCall for use in interceptors
159
+ #
160
+ # @return [InterceptableView]
161
+ #
162
+ def interceptable
163
+ InterceptableView.new(self)
164
+ end
165
+
157
166
  def receive_and_check_status
158
167
  batch_result = @call.run_batch(RECV_STATUS_ON_CLIENT => nil)
159
168
  set_input_stream_done
@@ -515,15 +524,27 @@ module GRPC
515
524
  # This does not mean that must necessarily be one. E.g, the replies
516
525
  # produced by gen_each_reply could ignore the received_msgs
517
526
  #
518
- # @param gen_each_reply [Proc] generates the BiDi stream replies
519
- def run_server_bidi(gen_each_reply)
520
- bd = BidiCall.new(@call,
521
- @marshal,
522
- @unmarshal,
523
- metadata_received: @metadata_received,
524
- req_view: MultiReqView.new(self))
525
-
526
- bd.run_on_server(gen_each_reply, proc { set_input_stream_done })
527
+ # @param mth [Proc] generates the BiDi stream replies
528
+ # @param interception_ctx [InterceptionContext]
529
+ #
530
+ def run_server_bidi(mth, interception_ctx)
531
+ view = multi_req_view
532
+ bidi_call = BidiCall.new(
533
+ @call,
534
+ @marshal,
535
+ @unmarshal,
536
+ metadata_received: @metadata_received,
537
+ req_view: view
538
+ )
539
+ requests = bidi_call.read_next_loop(proc { set_input_stream_done }, false)
540
+ interception_ctx.intercept!(
541
+ :bidi_streamer,
542
+ call: view,
543
+ method: mth,
544
+ requests: requests
545
+ ) do
546
+ bidi_call.run_on_server(mth, requests)
547
+ end
527
548
  end
528
549
 
529
550
  # Waits till an operation completes
@@ -645,5 +666,9 @@ module GRPC
645
666
  Operation = view_class(:cancel, :cancelled?, :deadline, :execute,
646
667
  :metadata, :status, :start_call, :wait, :write_flag,
647
668
  :write_flag=, :trailing_metadata)
669
+
670
+ # InterceptableView further limits access to an ActiveCall's methods
671
+ # for use in interceptors on the client, exposing only the deadline
672
+ InterceptableView = view_class(:deadline)
648
673
  end
649
674
  end
@@ -87,23 +87,32 @@ module GRPC
87
87
  # This does not mean that must necessarily be one. E.g, the replies
88
88
  # produced by gen_each_reply could ignore the received_msgs
89
89
  #
90
- # @param gen_each_reply [Proc] generates the BiDi stream replies.
91
- # @param set_input_steam_done [Proc] call back to call when
92
- # the reads have been completely read through.
93
- def run_on_server(gen_each_reply, set_input_stream_done)
90
+ # @param [Proc] gen_each_reply generates the BiDi stream replies.
91
+ # @param [Enumerable] requests The enumerable of requests to run
92
+ def run_on_server(gen_each_reply, requests)
93
+ replies = nil
94
+
94
95
  # Pass in the optional call object parameter if possible
95
96
  if gen_each_reply.arity == 1
96
- replys = gen_each_reply.call(
97
- read_loop(set_input_stream_done, is_client: false))
97
+ replies = gen_each_reply.call(requests)
98
98
  elsif gen_each_reply.arity == 2
99
- replys = gen_each_reply.call(
100
- read_loop(set_input_stream_done, is_client: false),
101
- @req_view)
99
+ replies = gen_each_reply.call(requests, @req_view)
102
100
  else
103
101
  fail 'Illegal arity of reply generator'
104
102
  end
105
103
 
106
- write_loop(replys, is_client: false)
104
+ write_loop(replies, is_client: false)
105
+ end
106
+
107
+ ##
108
+ # Read the next stream iteration
109
+ #
110
+ # @param [Proc] finalize_stream callback to call when the reads have been
111
+ # completely read through.
112
+ # @param [Boolean] is_client If this is a client or server request
113
+ #
114
+ def read_next_loop(finalize_stream, is_client = false)
115
+ read_loop(finalize_stream, is_client: is_client)
107
116
  end
108
117
 
109
118
  private
@@ -89,17 +89,23 @@ module GRPC
89
89
  # used within a gRPC server.
90
90
  # @param channel_args [Hash] the channel arguments. Note: this argument is
91
91
  # ignored if the channel_override argument is provided.
92
+ # @param interceptors [Array<GRPC::ClientInterceptor>] An array of
93
+ # GRPC::ClientInterceptor objects that will be used for
94
+ # intercepting calls before they are executed
95
+ # Interceptors are an EXPERIMENTAL API.
92
96
  def initialize(host, creds,
93
97
  channel_override: nil,
94
98
  timeout: nil,
95
99
  propagate_mask: nil,
96
- channel_args: {})
100
+ channel_args: {},
101
+ interceptors: [])
97
102
  @ch = ClientStub.setup_channel(channel_override, host, creds,
98
103
  channel_args)
99
104
  alt_host = channel_args[Core::Channel::SSL_TARGET]
100
105
  @host = alt_host.nil? ? host : alt_host
101
106
  @propagate_mask = propagate_mask
102
107
  @timeout = timeout.nil? ? DEFAULT_TIMEOUT : timeout
108
+ @interceptors = InterceptorRegistry.new(interceptors)
103
109
  end
104
110
 
105
111
  # request_response sends a request to a GRPC server, and returns the
@@ -149,16 +155,29 @@ module GRPC
149
155
  deadline: deadline,
150
156
  parent: parent,
151
157
  credentials: credentials)
152
- return c.request_response(req, metadata: metadata) unless return_op
153
-
154
- # return the operation view of the active_call; define #execute as a
155
- # new method for this instance that invokes #request_response.
156
- c.merge_metadata_to_send(metadata)
157
- op = c.operation
158
- op.define_singleton_method(:execute) do
159
- c.request_response(req, metadata: metadata)
158
+ interception_context = @interceptors.build_context
159
+ intercept_args = {
160
+ method: method,
161
+ request: req,
162
+ call: c.interceptable,
163
+ metadata: metadata
164
+ }
165
+ if return_op
166
+ # return the operation view of the active_call; define #execute as a
167
+ # new method for this instance that invokes #request_response.
168
+ c.merge_metadata_to_send(metadata)
169
+ op = c.operation
170
+ op.define_singleton_method(:execute) do
171
+ interception_context.intercept!(:request_response, intercept_args) do
172
+ c.request_response(req, metadata: metadata)
173
+ end
174
+ end
175
+ op
176
+ else
177
+ interception_context.intercept!(:request_response, intercept_args) do
178
+ c.request_response(req, metadata: metadata)
179
+ end
160
180
  end
161
- op
162
181
  end
163
182
 
164
183
  # client_streamer sends a stream of requests to a GRPC server, and
@@ -213,16 +232,29 @@ module GRPC
213
232
  deadline: deadline,
214
233
  parent: parent,
215
234
  credentials: credentials)
216
- return c.client_streamer(requests, metadata: metadata) unless return_op
217
-
218
- # return the operation view of the active_call; define #execute as a
219
- # new method for this instance that invokes #client_streamer.
220
- c.merge_metadata_to_send(metadata)
221
- op = c.operation
222
- op.define_singleton_method(:execute) do
223
- c.client_streamer(requests)
235
+ interception_context = @interceptors.build_context
236
+ intercept_args = {
237
+ method: method,
238
+ requests: requests,
239
+ call: c.interceptable,
240
+ metadata: metadata
241
+ }
242
+ if return_op
243
+ # return the operation view of the active_call; define #execute as a
244
+ # new method for this instance that invokes #client_streamer.
245
+ c.merge_metadata_to_send(metadata)
246
+ op = c.operation
247
+ op.define_singleton_method(:execute) do
248
+ interception_context.intercept!(:client_streamer, intercept_args) do
249
+ c.client_streamer(requests)
250
+ end
251
+ end
252
+ op
253
+ else
254
+ interception_context.intercept!(:client_streamer, intercept_args) do
255
+ c.client_streamer(requests, metadata: metadata)
256
+ end
224
257
  end
225
- op
226
258
  end
227
259
 
228
260
  # server_streamer sends one request to the GRPC server, which yields a
@@ -292,16 +324,29 @@ module GRPC
292
324
  deadline: deadline,
293
325
  parent: parent,
294
326
  credentials: credentials)
295
- return c.server_streamer(req, metadata: metadata, &blk) unless return_op
296
-
297
- # return the operation view of the active_call; define #execute
298
- # as a new method for this instance that invokes #server_streamer
299
- c.merge_metadata_to_send(metadata)
300
- op = c.operation
301
- op.define_singleton_method(:execute) do
302
- c.server_streamer(req, &blk)
327
+ interception_context = @interceptors.build_context
328
+ intercept_args = {
329
+ method: method,
330
+ request: req,
331
+ call: c.interceptable,
332
+ metadata: metadata
333
+ }
334
+ if return_op
335
+ # return the operation view of the active_call; define #execute
336
+ # as a new method for this instance that invokes #server_streamer
337
+ c.merge_metadata_to_send(metadata)
338
+ op = c.operation
339
+ op.define_singleton_method(:execute) do
340
+ interception_context.intercept!(:server_streamer, intercept_args) do
341
+ c.server_streamer(req, &blk)
342
+ end
343
+ end
344
+ op
345
+ else
346
+ interception_context.intercept!(:server_streamer, intercept_args) do
347
+ c.server_streamer(req, metadata: metadata, &blk)
348
+ end
303
349
  end
304
- op
305
350
  end
306
351
 
307
352
  # bidi_streamer sends a stream of requests to the GRPC server, and yields
@@ -405,17 +450,29 @@ module GRPC
405
450
  deadline: deadline,
406
451
  parent: parent,
407
452
  credentials: credentials)
408
- return c.bidi_streamer(requests, metadata: metadata,
409
- &blk) unless return_op
410
-
411
- # return the operation view of the active_call; define #execute
412
- # as a new method for this instance that invokes #bidi_streamer
413
- c.merge_metadata_to_send(metadata)
414
- op = c.operation
415
- op.define_singleton_method(:execute) do
416
- c.bidi_streamer(requests, &blk)
453
+ interception_context = @interceptors.build_context
454
+ intercept_args = {
455
+ method: method,
456
+ requests: requests,
457
+ call: c.interceptable,
458
+ metadata: metadata
459
+ }
460
+ if return_op
461
+ # return the operation view of the active_call; define #execute
462
+ # as a new method for this instance that invokes #bidi_streamer
463
+ c.merge_metadata_to_send(metadata)
464
+ op = c.operation
465
+ op.define_singleton_method(:execute) do
466
+ interception_context.intercept!(:bidi_streamer, intercept_args) do
467
+ c.bidi_streamer(requests, &blk)
468
+ end
469
+ end
470
+ op
471
+ else
472
+ interception_context.intercept!(:bidi_streamer, intercept_args) do
473
+ c.bidi_streamer(requests, metadata: metadata, &blk)
474
+ end
417
475
  end
418
- op
419
476
  end
420
477
 
421
478
  private
@@ -0,0 +1,53 @@
1
+ # Copyright 2017 gRPC authors.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # GRPC contains the General RPC module.
16
+ module GRPC
17
+ ##
18
+ # Represents a registry of added interceptors available for enumeration.
19
+ # The registry can be used for both server and client interceptors.
20
+ # This class is internal to gRPC and not meant for public usage.
21
+ #
22
+ class InterceptorRegistry
23
+ ##
24
+ # An error raised when an interceptor is attempted to be added
25
+ # that does not extend GRPC::Interceptor
26
+ #
27
+ class DescendantError < StandardError; end
28
+
29
+ ##
30
+ # Initialize the registry with an empty interceptor list
31
+ # This is an EXPERIMENTAL API.
32
+ #
33
+ def initialize(interceptors = [])
34
+ @interceptors = []
35
+ interceptors.each do |i|
36
+ base = GRPC::Interceptor
37
+ unless i.class.ancestors.include?(base)
38
+ fail DescendantError, "Interceptors must descend from #{base}"
39
+ end
40
+ @interceptors << i
41
+ end
42
+ end
43
+
44
+ ##
45
+ # Builds an interception context from this registry
46
+ #
47
+ # @return [InterceptionContext]
48
+ #
49
+ def build_context
50
+ InterceptionContext.new(@interceptors)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,186 @@
1
+ # Copyright 2017 gRPC authors.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ require_relative 'interceptor_registry'
15
+
16
+ # GRPC contains the General RPC module.
17
+ module GRPC
18
+ ##
19
+ # Base class for interception in GRPC
20
+ #
21
+ class Interceptor
22
+ ##
23
+ # @param [Hash] options A hash of options that will be used
24
+ # by the interceptor. This is an EXPERIMENTAL API.
25
+ #
26
+ def initialize(options = {})
27
+ @options = options || {}
28
+ end
29
+ end
30
+
31
+ ##
32
+ # ClientInterceptor allows for wrapping outbound gRPC client stub requests.
33
+ # This is an EXPERIMENTAL API.
34
+ #
35
+ class ClientInterceptor < Interceptor
36
+ ##
37
+ # Intercept a unary request response call
38
+ #
39
+ # @param [Object] request
40
+ # @param [GRPC::ActiveCall] call
41
+ # @param [Method] method
42
+ # @param [Hash] metadata
43
+ #
44
+ def request_response(request:, call:, method:, metadata:)
45
+ GRPC.logger.debug "Intercepting request response method #{method}" \
46
+ " for request #{request} with call #{call} and metadata: #{metadata}"
47
+ yield
48
+ end
49
+
50
+ ##
51
+ # Intercept a client streaming call
52
+ #
53
+ # @param [Enumerable] requests
54
+ # @param [GRPC::ActiveCall] call
55
+ # @param [Method] method
56
+ # @param [Hash] metadata
57
+ #
58
+ def client_streamer(requests:, call:, method:, metadata:)
59
+ GRPC.logger.debug "Intercepting client streamer method #{method}" \
60
+ " for requests #{requests} with call #{call} and metadata: #{metadata}"
61
+ yield
62
+ end
63
+
64
+ ##
65
+ # Intercept a server streaming call
66
+ #
67
+ # @param [Object] request
68
+ # @param [GRPC::ActiveCall] call
69
+ # @param [Method] method
70
+ # @param [Hash] metadata
71
+ #
72
+ def server_streamer(request:, call:, method:, metadata:)
73
+ GRPC.logger.debug "Intercepting server streamer method #{method}" \
74
+ " for request #{request} with call #{call} and metadata: #{metadata}"
75
+ yield
76
+ end
77
+
78
+ ##
79
+ # Intercept a BiDi streaming call
80
+ #
81
+ # @param [Enumerable] requests
82
+ # @param [GRPC::ActiveCall] call
83
+ # @param [Method] method
84
+ # @param [Hash] metadata
85
+ #
86
+ def bidi_streamer(requests:, call:, method:, metadata:)
87
+ GRPC.logger.debug "Intercepting bidi streamer method #{method}" \
88
+ " for requests #{requests} with call #{call} and metadata: #{metadata}"
89
+ yield
90
+ end
91
+ end
92
+
93
+ ##
94
+ # ServerInterceptor allows for wrapping gRPC server execution handling.
95
+ # This is an EXPERIMENTAL API.
96
+ #
97
+ class ServerInterceptor < Interceptor
98
+ ##
99
+ # Intercept a unary request response call.
100
+ #
101
+ # @param [Object] request
102
+ # @param [GRPC::ActiveCall::SingleReqView] call
103
+ # @param [Method] method
104
+ #
105
+ def request_response(request:, call:, method:)
106
+ GRPC.logger.debug "Intercepting request response method #{method}" \
107
+ " for request #{request} with call #{call}"
108
+ yield
109
+ end
110
+
111
+ ##
112
+ # Intercept a client streaming call
113
+ #
114
+ # @param [GRPC::ActiveCall::MultiReqView] call
115
+ # @param [Method] method
116
+ #
117
+ def client_streamer(call:, method:)
118
+ GRPC.logger.debug "Intercepting client streamer method #{method}" \
119
+ " with call #{call}"
120
+ yield
121
+ end
122
+
123
+ ##
124
+ # Intercept a server streaming call
125
+ #
126
+ # @param [Object] request
127
+ # @param [GRPC::ActiveCall::SingleReqView] call
128
+ # @param [Method] method
129
+ #
130
+ def server_streamer(request:, call:, method:)
131
+ GRPC.logger.debug "Intercepting server streamer method #{method}" \
132
+ " for request #{request} with call #{call}"
133
+ yield
134
+ end
135
+
136
+ ##
137
+ # Intercept a BiDi streaming call
138
+ #
139
+ # @param [Enumerable<Object>] requests
140
+ # @param [GRPC::ActiveCall::MultiReqView] call
141
+ # @param [Method] method
142
+ #
143
+ def bidi_streamer(requests:, call:, method:)
144
+ GRPC.logger.debug "Intercepting bidi streamer method #{method}" \
145
+ " for requests #{requests} with call #{call}"
146
+ yield
147
+ end
148
+ end
149
+
150
+ ##
151
+ # Represents the context in which an interceptor runs. Used to provide an
152
+ # injectable mechanism for handling interception. This is an EXPERIMENTAL API.
153
+ #
154
+ class InterceptionContext
155
+ ##
156
+ # @param [Array<GRPC::Interceptor>]
157
+ #
158
+ def initialize(interceptors = [])
159
+ @interceptors = interceptors.dup
160
+ end
161
+
162
+ ##
163
+ # Intercept the call and fire out to interceptors in a FIFO execution.
164
+ # This is an EXPERIMENTAL API.
165
+ #
166
+ # @param [Symbol] type The request type
167
+ # @param [Hash] args The arguments for the call
168
+ #
169
+ def intercept!(type, args = {})
170
+ return yield if @interceptors.none?
171
+
172
+ i = @interceptors.pop
173
+ return yield unless i
174
+
175
+ i.send(type, args) do
176
+ if @interceptors.any?
177
+ intercept!(type, args) do
178
+ yield
179
+ end
180
+ else
181
+ yield
182
+ end
183
+ end
184
+ end
185
+ end
186
+ end