grpc_kit 0.1.14 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/README.md +4 -0
  4. data/examples/interceptors/call_stream.rb +18 -0
  5. data/examples/interceptors/client_logging_interceptor.rb +6 -17
  6. data/examples/interceptors/server_logging_interceptor.rb +6 -9
  7. data/examples/routeguide_client.rb +7 -10
  8. data/examples/routeguide_server.rb +4 -6
  9. data/lib/grpc_kit.rb +1 -1
  10. data/lib/grpc_kit/call.rb +45 -0
  11. data/lib/grpc_kit/calls.rb +2 -52
  12. data/lib/grpc_kit/calls/client_bidi_streamer.rb +13 -11
  13. data/lib/grpc_kit/calls/client_client_streamer.rb +5 -15
  14. data/lib/grpc_kit/calls/client_request_response.rb +6 -11
  15. data/lib/grpc_kit/calls/client_server_streamer.rb +7 -14
  16. data/lib/grpc_kit/calls/server_bidi_streamer.rb +11 -10
  17. data/lib/grpc_kit/calls/server_client_streamer.rb +12 -10
  18. data/lib/grpc_kit/calls/server_request_response.rb +6 -10
  19. data/lib/grpc_kit/calls/server_server_streamer.rb +5 -11
  20. data/lib/grpc_kit/errors.rb +17 -17
  21. data/lib/grpc_kit/grpc/dsl.rb +1 -1
  22. data/lib/grpc_kit/grpc/interceptor.rb +8 -8
  23. data/lib/grpc_kit/interceptor_registory.rb +35 -0
  24. data/lib/grpc_kit/interceptors.rb +6 -12
  25. data/lib/grpc_kit/interceptors/client_request_response.rb +6 -9
  26. data/lib/grpc_kit/interceptors/server_request_response.rb +7 -10
  27. data/lib/grpc_kit/rpcs/client_bidi_streamer.rb +1 -1
  28. data/lib/grpc_kit/rpcs/client_client_streamer.rb +1 -1
  29. data/lib/grpc_kit/rpcs/client_request_response.rb +6 -6
  30. data/lib/grpc_kit/rpcs/client_server_streamer.rb +9 -4
  31. data/lib/grpc_kit/rpcs/server_bidi_streamer.rb +5 -1
  32. data/lib/grpc_kit/rpcs/server_client_streamer.rb +7 -3
  33. data/lib/grpc_kit/rpcs/server_request_response.rb +15 -11
  34. data/lib/grpc_kit/rpcs/server_server_streamer.rb +7 -3
  35. data/lib/grpc_kit/session/server_session.rb +1 -1
  36. data/lib/grpc_kit/status_codes.rb +17 -17
  37. data/lib/grpc_kit/stream/client_stream.rb +3 -10
  38. data/lib/grpc_kit/stream/server_stream.rb +0 -5
  39. data/lib/grpc_kit/version.rb +1 -1
  40. metadata +5 -2
@@ -1,36 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'grpc_kit/interceptors'
4
+ require 'grpc_kit/interceptor_registory'
4
5
 
5
6
  module GrpcKit
6
7
  module Interceptors::Server
7
8
  class RequestResponse
8
9
  # @param interceptors [Array<GrpcKit::GRPC::ServerInterceptor>]
9
10
  def initialize(interceptors)
10
- @interceptors = interceptors
11
+ @registry = GrpcKit::InterceptorRegistry.new(interceptors)
11
12
  end
12
13
 
14
+ # @param request [Object] Recevied request objects
13
15
  # @param call [GrpcKit::Calls::Client::RequestResponse]
16
+ # @yieldreturn [Object] Response object server sent
14
17
  def intercept(request, call, &block)
15
- if @interceptors && !@interceptors.empty?
16
- do_intercept(@interceptors.dup, request, call, &block)
17
- else
18
- yield(request, call)
19
- end
18
+ do_intercept(@registry.build, request, call, &block)
20
19
  end
21
20
 
22
21
  private
23
22
 
24
23
  def do_intercept(interceptors, request, call)
25
24
  if interceptors.empty?
26
- return yield(request, call)
25
+ return yield
27
26
  end
28
27
 
29
28
  interceptor = interceptors.pop
30
29
  interceptor.request_response(request: request, call: call, method: call.method) do
31
- do_intercept(interceptors, request, call) do |req, c|
32
- yield(req, c)
33
- end
30
+ do_intercept(interceptors, request, call) { yield }
34
31
  end
35
32
  end
36
33
  end
@@ -20,7 +20,7 @@ module GrpcKit
20
20
  )
21
21
 
22
22
  if @config.interceptor
23
- @config.interceptor.intercept(call, metadata) { |s| s }
23
+ @config.interceptor.intercept(call, metadata) { |c| c }
24
24
  else
25
25
  call
26
26
  end
@@ -20,7 +20,7 @@ module GrpcKit
20
20
  )
21
21
 
22
22
  if @config.interceptor
23
- @config.interceptor.intercept(call, metadata) { |s| s }
23
+ @config.interceptor.intercept(call, metadata) { |c| c }
24
24
  else
25
25
  call
26
26
  end
@@ -11,7 +11,7 @@ module GrpcKit
11
11
  # @param metadata [Hash<String, String>]
12
12
  # @param timeout [GrpcKit::GrpcTime]
13
13
  # @return [Object] response message
14
- def invoke(stream, request, metadata: {}, timeout: nil)
14
+ def invoke(stream, request, metadata: {}, timeout: nil)
15
15
  call = GrpcKit::Calls::Client::RequestResponse.new(
16
16
  metadata: metadata,
17
17
  config: @config,
@@ -21,13 +21,13 @@ module GrpcKit
21
21
 
22
22
  Timeout.timeout(timeout&.to_f, GrpcKit::Errors::DeadlineExceeded) do
23
23
  if @config.interceptor
24
- @config.interceptor.intercept(request, call, call.metadata) do |r, c, _|
25
- call.send_msg(request, last: true)
26
- call.recv(last: true)
24
+ @config.interceptor.intercept(request, call, call.metadata) do
25
+ call.send_msg(request)
26
+ call.recv
27
27
  end
28
28
  else
29
- call.send_msg(request, last: true)
30
- call.recv(last: true)
29
+ call.send_msg(request)
30
+ call.recv
31
31
  end
32
32
  end
33
33
  end
@@ -12,15 +12,20 @@ module GrpcKit
12
12
  # @param timeout [GrpcKit::GrpcTime]
13
13
  # @return [GrpcKit::Calls::Client::ServerStreamer]
14
14
  def invoke(stream, request, metadata: {}, timeout: nil)
15
- call = GrpcKit::Calls::Client::ServerStreamer.new(metadata: metadata, config: @config, timeout: timeout, stream: stream)
15
+ call = GrpcKit::Calls::Client::ServerStreamer.new(
16
+ metadata: metadata,
17
+ config: @config,
18
+ timeout: timeout,
19
+ stream: stream,
20
+ )
16
21
 
17
22
  if @config.interceptor
18
- @config.interceptor.intercept(call, metadata) do |c, m|
19
- c.send_msg(request, last: true)
23
+ @config.interceptor.intercept(call, metadata) do |c|
24
+ c.send_msg(request)
20
25
  c
21
26
  end
22
27
  else
23
- call.send_msg(request, last: true)
28
+ call.send_msg(request)
24
29
  call
25
30
  end
26
31
  end
@@ -10,7 +10,11 @@ module GrpcKit
10
10
  # @param metadata [Hash<String, String>]
11
11
  # @return [void]
12
12
  def invoke(stream, metadata: {})
13
- call = GrpcKit::Calls::Server::BidiStreamer.new(metadata: metadata, config: @config, stream: stream)
13
+ call = GrpcKit::Calls::Server::BidiStreamer.new(
14
+ metadata: metadata,
15
+ config: @config,
16
+ stream: stream,
17
+ )
14
18
 
15
19
  if @config.interceptor
16
20
  @config.interceptor.intercept(call) do |c|
@@ -10,16 +10,20 @@ module GrpcKit
10
10
  # @param metadata [Hash<String, String>]
11
11
  # @return [void]
12
12
  def invoke(stream, metadata: {})
13
- call = GrpcKit::Calls::Server::ClientStreamer.new(metadata: metadata, config: @config, stream: stream)
13
+ call = GrpcKit::Calls::Server::ClientStreamer.new(
14
+ metadata: metadata,
15
+ config: @config,
16
+ stream: stream,
17
+ )
14
18
 
15
19
  if @config.interceptor
16
20
  @config.interceptor.intercept(call) do |c|
17
21
  resp = @handler.send(@config.ruby_style_method_name, c)
18
- c.send_msg(resp, last: true)
22
+ c.send_msg(resp)
19
23
  end
20
24
  else
21
25
  resp = @handler.send(@config.ruby_style_method_name, call)
22
- call.send_msg(resp, last: true)
26
+ call.send_msg(resp)
23
27
  end
24
28
  end
25
29
  end
@@ -10,19 +10,23 @@ module GrpcKit
10
10
  # @param metadata [Hash<String, String>]
11
11
  # @return [void]
12
12
  def invoke(stream, metadata: {})
13
- call = GrpcKit::Calls::Server::RequestResponse.new(metadata: metadata, config: @config, stream: stream)
14
- request = call.recv(last: true)
13
+ call = GrpcKit::Calls::Server::RequestResponse.new(
14
+ metadata: metadata,
15
+ config: @config,
16
+ stream: stream,
17
+ )
15
18
 
16
- resp =
17
- if @config.interceptor
18
- @config.interceptor.intercept(request, call) do |_, _|
19
- @handler.send(@config.ruby_style_method_name, request, call)
20
- end
21
- else
22
- @handler.send(@config.ruby_style_method_name, request, call)
19
+ request = call.recv
20
+ if @config.interceptor
21
+ @config.interceptor.intercept(request, call) do
22
+ resp = @handler.send(@config.ruby_style_method_name, request, call)
23
+ call.send_msg(resp)
24
+ resp
23
25
  end
24
-
25
- call.send_msg(resp, last: true)
26
+ else
27
+ resp = @handler.send(@config.ruby_style_method_name, request, call)
28
+ call.send_msg(resp)
29
+ end
26
30
  end
27
31
  end
28
32
  end
@@ -10,15 +10,19 @@ module GrpcKit
10
10
  # @param metadata [Hash<String, String>]
11
11
  # @return [void]
12
12
  def invoke(stream, metadata: {})
13
- call = GrpcKit::Calls::Server::ServerStreamer.new(metadata: metadata, config: @config, stream: stream)
13
+ call = GrpcKit::Calls::Server::ServerStreamer.new(
14
+ metadata: metadata,
15
+ config: @config,
16
+ stream: stream,
17
+ )
14
18
 
15
19
  if @config.interceptor
16
20
  @config.interceptor.intercept(call) do |c|
17
- request = c.recv(last: true)
21
+ request = c.recv
18
22
  @handler.send(@config.ruby_style_method_name, request, c)
19
23
  end
20
24
  else
21
- request = call.recv(last: true)
25
+ request = call.recv
22
26
  @handler.send(@config.ruby_style_method_name, request, call)
23
27
  end
24
28
 
@@ -34,7 +34,7 @@ module GrpcKit
34
34
  loop do
35
35
  invoke
36
36
 
37
- if @streams.size == 0
37
+ if @streams.empty?
38
38
  unless @io.wait_readable
39
39
  shutdown
40
40
  break
@@ -22,24 +22,24 @@ module GrpcKit
22
22
  DO_NOT_USE = '-1'
23
23
 
24
24
  CODE_NAME = {
25
- OK => 'OK',
26
- CANCELLED => 'CANCELLED',
27
- UNKNOWN => 'UNKNOWN',
28
- INVALID_ARGUMENT => 'INVALID_ARGUMENT',
29
- DEADLINE_EXCEEDED => 'DEADLINE_EXCEEDED',
30
- NOT_FOUND => 'NOT_FOUND',
31
- ALREADY_EXISTS => 'ALREADY_EXISTS',
32
- PERMISSION_DENIED => 'PERMISSION_DENIED',
33
- RESOURCE_EXHAUSTED => 'RESOURCE_EXHAUSTED',
25
+ OK => 'OK',
26
+ CANCELLED => 'CANCELLED',
27
+ UNKNOWN => 'UNKNOWN',
28
+ INVALID_ARGUMENT => 'INVALID_ARGUMENT',
29
+ DEADLINE_EXCEEDED => 'DEADLINE_EXCEEDED',
30
+ NOT_FOUND => 'NOT_FOUND',
31
+ ALREADY_EXISTS => 'ALREADY_EXISTS',
32
+ PERMISSION_DENIED => 'PERMISSION_DENIED',
33
+ RESOURCE_EXHAUSTED => 'RESOURCE_EXHAUSTED',
34
34
  FAILED_PRECONDITION => 'FAILED_PRECONDITION',
35
- ABORTED => 'ABORTED',
36
- OUT_OF_RANGE => 'OUT_OF_RANGE',
37
- UNIMPLEMENTED => 'UNIMPLEMENTED',
38
- INTERNAL => 'INTERNAL',
39
- UNAVAILABLE => 'UNAVAILABLE',
40
- DATA_LOSS => 'DATA_LOSS',
41
- UNAUTHENTICATED => 'UNAUTHENTICATED',
42
- DO_NOT_USE => 'DO_NOT_USE',
35
+ ABORTED => 'ABORTED',
36
+ OUT_OF_RANGE => 'OUT_OF_RANGE',
37
+ UNIMPLEMENTED => 'UNIMPLEMENTED',
38
+ INTERNAL => 'INTERNAL',
39
+ UNAVAILABLE => 'UNAVAILABLE',
40
+ DATA_LOSS => 'DATA_LOSS',
41
+ UNAUTHENTICATED => 'UNAUTHENTICATED',
42
+ DO_NOT_USE => 'DO_NOT_USE',
43
43
  }.freeze
44
44
  end
45
45
  end
@@ -47,12 +47,6 @@ module GrpcKit
47
47
  end
48
48
  end
49
49
 
50
- def each
51
- validate_if_request_start!
52
-
53
- loop { yield(do_recv) }
54
- end
55
-
56
50
  # @raise [StopIteration] when recving message finished
57
51
  # @param last [Boolean]
58
52
  # @param blocking [Boolean]
@@ -80,20 +74,19 @@ module GrpcKit
80
74
  @transport.close_and_flush
81
75
  end
82
76
 
83
- # @return [Array<Object>]
77
+ # @return [Object]
84
78
  def close_and_recv
85
79
  validate_if_request_start!
86
80
 
87
81
  @transport.close_and_flush
88
82
 
89
- data = []
90
- loop { data.push(do_recv) }
83
+ ret = do_recv(last: true)
91
84
 
92
85
  if @deadline && Time.now > @deadline
93
86
  raise GrpcKit::Errors::DeadlineExceeded, @deadline
94
87
  end
95
88
 
96
- data
89
+ ret
97
90
  end
98
91
 
99
92
  private
@@ -81,11 +81,6 @@ module GrpcKit
81
81
  end
82
82
  end
83
83
 
84
- # @param protobuf [GrpcKit::ProtoBuffer]
85
- def each(protobuf)
86
- loop { yield(recv_msg(protobuf)) }
87
- end
88
-
89
84
  # @param status [GrpcKit::StatusCodes::BadStatus, GrpcKit::StatusCodes::OK]
90
85
  # @param msg [String,nil]
91
86
  # @param metadata [Hash<String,String>]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GrpcKit
4
- VERSION = '0.1.14'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grpc_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ganmacs
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-11 00:00:00.000000000 Z
11
+ date: 2018-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ds9
@@ -159,6 +159,7 @@ files:
159
159
  - examples/helloworld/helloworld_services_pb.rb
160
160
  - examples/helloworld_client.rb
161
161
  - examples/helloworld_server.rb
162
+ - examples/interceptors/call_stream.rb
162
163
  - examples/interceptors/client_logging_interceptor.rb
163
164
  - examples/interceptors/server_logging_interceptor.rb
164
165
  - examples/routeguide/routeguide.json
@@ -170,6 +171,7 @@ files:
170
171
  - grpc_kit.gemspec
171
172
  - lib/grpc.rb
172
173
  - lib/grpc_kit.rb
174
+ - lib/grpc_kit/call.rb
173
175
  - lib/grpc_kit/calls.rb
174
176
  - lib/grpc_kit/calls/client_bidi_streamer.rb
175
177
  - lib/grpc_kit/calls/client_client_streamer.rb
@@ -189,6 +191,7 @@ files:
189
191
  - lib/grpc_kit/grpc/logger.rb
190
192
  - lib/grpc_kit/grpc/stream.rb
191
193
  - lib/grpc_kit/grpc_time.rb
194
+ - lib/grpc_kit/interceptor_registory.rb
192
195
  - lib/grpc_kit/interceptors.rb
193
196
  - lib/grpc_kit/interceptors/client_bidi_streamer.rb
194
197
  - lib/grpc_kit/interceptors/client_client_streamer.rb