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.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/README.md +4 -0
- data/examples/interceptors/call_stream.rb +18 -0
- data/examples/interceptors/client_logging_interceptor.rb +6 -17
- data/examples/interceptors/server_logging_interceptor.rb +6 -9
- data/examples/routeguide_client.rb +7 -10
- data/examples/routeguide_server.rb +4 -6
- data/lib/grpc_kit.rb +1 -1
- data/lib/grpc_kit/call.rb +45 -0
- data/lib/grpc_kit/calls.rb +2 -52
- data/lib/grpc_kit/calls/client_bidi_streamer.rb +13 -11
- data/lib/grpc_kit/calls/client_client_streamer.rb +5 -15
- data/lib/grpc_kit/calls/client_request_response.rb +6 -11
- data/lib/grpc_kit/calls/client_server_streamer.rb +7 -14
- data/lib/grpc_kit/calls/server_bidi_streamer.rb +11 -10
- data/lib/grpc_kit/calls/server_client_streamer.rb +12 -10
- data/lib/grpc_kit/calls/server_request_response.rb +6 -10
- data/lib/grpc_kit/calls/server_server_streamer.rb +5 -11
- data/lib/grpc_kit/errors.rb +17 -17
- data/lib/grpc_kit/grpc/dsl.rb +1 -1
- data/lib/grpc_kit/grpc/interceptor.rb +8 -8
- data/lib/grpc_kit/interceptor_registory.rb +35 -0
- data/lib/grpc_kit/interceptors.rb +6 -12
- data/lib/grpc_kit/interceptors/client_request_response.rb +6 -9
- data/lib/grpc_kit/interceptors/server_request_response.rb +7 -10
- data/lib/grpc_kit/rpcs/client_bidi_streamer.rb +1 -1
- data/lib/grpc_kit/rpcs/client_client_streamer.rb +1 -1
- data/lib/grpc_kit/rpcs/client_request_response.rb +6 -6
- data/lib/grpc_kit/rpcs/client_server_streamer.rb +9 -4
- data/lib/grpc_kit/rpcs/server_bidi_streamer.rb +5 -1
- data/lib/grpc_kit/rpcs/server_client_streamer.rb +7 -3
- data/lib/grpc_kit/rpcs/server_request_response.rb +15 -11
- data/lib/grpc_kit/rpcs/server_server_streamer.rb +7 -3
- data/lib/grpc_kit/session/server_session.rb +1 -1
- data/lib/grpc_kit/status_codes.rb +17 -17
- data/lib/grpc_kit/stream/client_stream.rb +3 -10
- data/lib/grpc_kit/stream/server_stream.rb +0 -5
- data/lib/grpc_kit/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb504e4e1ba14e8e0a411336254cd5f7b3b20a39305cc31e506ae4d0d81cd77a
|
4
|
+
data.tar.gz: 2595cda7ac23058f72e76e0e8955b70d305c585fe34d63e1bd183d1fc40c104e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f00be6d6aace37b66fa123f50b89903998b6520446369e067dc56afc4342b1d62153f15f631f170fb0aed6b0dba27f0db270d94edd78ad7ccd5f712eb0163b5b
|
7
|
+
data.tar.gz: 83671c633699f7a0e49adbfffbf9fe383a7eae57216d7876760b9bbd6114018cf606183420135807406ece0ebbbd6fdb6a5fd251956b5e1cd59adef6c68f4369
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -59,6 +59,10 @@ puts message
|
|
59
59
|
$ bundle install
|
60
60
|
```
|
61
61
|
|
62
|
+
## Projects using grpc_kit
|
63
|
+
|
64
|
+
* [griffin](https://github.com/ganmacs/griffin) Multi process gRPC server in Ruby
|
65
|
+
|
62
66
|
## Contributing
|
63
67
|
|
64
68
|
Bug reports and pull requests are welcome on GitHub at https://github.com/ganmacs/grpc_kit.
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_kit'
|
4
|
+
require 'forwardable'
|
5
|
+
|
6
|
+
class CallStream < GrpcKit::Call
|
7
|
+
extend Forwardable
|
8
|
+
delegate %i[send_msg recv] => :@inner
|
9
|
+
|
10
|
+
# @params call [GrpcKit::Call]
|
11
|
+
def initialize(inner)
|
12
|
+
@inner = inner
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_missing(name, *args, &block)
|
16
|
+
@inner.public_send(name, *args, &block)
|
17
|
+
end
|
18
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'grpc_kit'
|
4
|
+
require_relative 'call_stream'
|
4
5
|
|
5
6
|
class LoggingInterceptor < GRPC::ClientInterceptor
|
6
7
|
def request_response(request: nil, method: nil, **)
|
@@ -26,28 +27,16 @@ class LoggingInterceptor < GRPC::ClientInterceptor
|
|
26
27
|
yield(LoggingStream.new(call))
|
27
28
|
end
|
28
29
|
|
29
|
-
class LoggingStream
|
30
|
-
def
|
31
|
-
@stream = stream
|
32
|
-
end
|
33
|
-
|
34
|
-
def send_msg(msg, **opts)
|
30
|
+
class LoggingStream < CallStream
|
31
|
+
def send_msg(msg)
|
35
32
|
GrpcKit.logger.info("logging interceptor send #{msg.inspect}")
|
36
|
-
|
33
|
+
super
|
37
34
|
end
|
38
35
|
|
39
|
-
def recv
|
40
|
-
|
36
|
+
def recv
|
37
|
+
super.tap do |v|
|
41
38
|
GrpcKit.logger.info("logging interceptor recv #{v.inspect}")
|
42
39
|
end
|
43
40
|
end
|
44
|
-
|
45
|
-
def close_and_recv
|
46
|
-
@stream.close_and_recv
|
47
|
-
end
|
48
|
-
|
49
|
-
def close_and_send
|
50
|
-
@stream.close_and_send
|
51
|
-
end
|
52
41
|
end
|
53
42
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'grpc_kit'
|
4
|
+
require_relative 'call_stream'
|
4
5
|
|
5
6
|
class LoggingInterceptor < GRPC::ServerInterceptor
|
6
7
|
def request_response(request: nil, call: nil, method: nil)
|
@@ -26,18 +27,14 @@ class LoggingInterceptor < GRPC::ServerInterceptor
|
|
26
27
|
yield(LoggingStream.new(call))
|
27
28
|
end
|
28
29
|
|
29
|
-
class LoggingStream
|
30
|
-
def
|
31
|
-
@stream = stream
|
32
|
-
end
|
33
|
-
|
34
|
-
def send_msg(msg, **opt)
|
30
|
+
class LoggingStream < CallStream
|
31
|
+
def send_msg(msg)
|
35
32
|
GrpcKit.logger.info("logging interceptor send #{msg.inspect}")
|
36
|
-
|
33
|
+
super
|
37
34
|
end
|
38
35
|
|
39
|
-
def recv
|
40
|
-
|
36
|
+
def recv
|
37
|
+
super.tap do |v|
|
41
38
|
GrpcKit.logger.info("logging interceptor recv #{v.inspect}")
|
42
39
|
end
|
43
40
|
end
|
@@ -15,7 +15,7 @@ def get_feature(stub)
|
|
15
15
|
GRPC.logger.info('===== get_feature =====')
|
16
16
|
points = [
|
17
17
|
Routeguide::Point.new(latitude: 409_146_138, longitude: -746_188_906),
|
18
|
-
Routeguide::Point.new(latitude: 0, longitude: 0)
|
18
|
+
Routeguide::Point.new(latitude: 0, longitude: 0),
|
19
19
|
]
|
20
20
|
|
21
21
|
points.each do |pt|
|
@@ -36,9 +36,7 @@ def list_features(stub)
|
|
36
36
|
)
|
37
37
|
|
38
38
|
stream = stub.list_features(rect)
|
39
|
-
|
40
|
-
loop do
|
41
|
-
r = stream.recv
|
39
|
+
stream.each do |r|
|
42
40
|
GRPC.logger.info("Found #{r.name} at #{r.location.inspect}")
|
43
41
|
end
|
44
42
|
end
|
@@ -55,13 +53,13 @@ def record_route(stub, size)
|
|
55
53
|
size.times do
|
56
54
|
location = features.sample['location']
|
57
55
|
point = Routeguide::Point.new(latitude: location['latitude'], longitude: location['longitude'])
|
58
|
-
|
56
|
+
GRPC.logger.info("Next point is #{point.inspect}")
|
59
57
|
stream.send_msg(point)
|
60
58
|
sleep(rand(0..1))
|
61
59
|
end
|
62
60
|
|
63
|
-
resp = stream.
|
64
|
-
|
61
|
+
resp = stream.recv
|
62
|
+
GRPC.logger.info("summary: #{resp.inspect}")
|
65
63
|
end
|
66
64
|
|
67
65
|
ROUTE_CHAT_NOTES = [
|
@@ -79,9 +77,8 @@ def route_chat(stub)
|
|
79
77
|
call = stub.route_chat({})
|
80
78
|
|
81
79
|
t = Thread.new do
|
82
|
-
|
83
|
-
rn
|
84
|
-
GRPC.logger.info("Got message #{rn.message} at point point(#{rn.location.latitude}, #{rn.location.longitude})")
|
80
|
+
call.each do |rn|
|
81
|
+
GRPC.logger.info("Got message #{rn.message} at point (#{rn.location.latitude}, #{rn.location.longitude})")
|
85
82
|
end
|
86
83
|
end
|
87
84
|
|
@@ -20,6 +20,7 @@ class Server < Routeguide::RouteGuide::Service
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def get_feature(point, ctx)
|
23
|
+
GRPC.logger.info('===== get_feature =====')
|
23
24
|
name = @features.fetch({ 'longitude' => point.longitude, 'latitude' => point.latitude }, '')
|
24
25
|
GRPC.logger.info("Point longitude=#{point.longitude}, latitude=#{point.latitude}, metadata=#{ctx.metadata}")
|
25
26
|
Routeguide::Feature.new(location: point, name: name)
|
@@ -48,8 +49,7 @@ class Server < Routeguide::RouteGuide::Service
|
|
48
49
|
start_at = Time.now.to_i
|
49
50
|
last = nil
|
50
51
|
|
51
|
-
|
52
|
-
point = stream.recv # XXX: raise StopIteration
|
52
|
+
stream.each do |point|
|
53
53
|
GRPC.logger.info(point)
|
54
54
|
|
55
55
|
count += 1
|
@@ -71,8 +71,8 @@ class Server < Routeguide::RouteGuide::Service
|
|
71
71
|
end
|
72
72
|
|
73
73
|
def route_chat(call)
|
74
|
-
|
75
|
-
|
74
|
+
GRPC.logger.info('===== record_chat =====')
|
75
|
+
call.each do |rn|
|
76
76
|
GRPC.logger.info("route_note location=#{rn.location.inspect}, message=#{rn.message}")
|
77
77
|
key = "#{rn.location.latitude} #{rn.location.longitude}"
|
78
78
|
saved_msgs = @route_notes[key]
|
@@ -115,8 +115,6 @@ class Server < Routeguide::RouteGuide::Service
|
|
115
115
|
end
|
116
116
|
|
117
117
|
sock = TCPServer.new(50051)
|
118
|
-
GrpcKit.logger = Logger.new('hoge', level: :debug)
|
119
|
-
|
120
118
|
opts = {}
|
121
119
|
|
122
120
|
if ENV['GRPC_INTERCEPTOR']
|
data/lib/grpc_kit.rb
CHANGED
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrpcKit
|
4
|
+
class Call
|
5
|
+
Name = Struct.new(:name, :receiver)
|
6
|
+
Reciver = Struct.new(:class)
|
7
|
+
Klass = Struct.new(:service_name)
|
8
|
+
|
9
|
+
# @return [GrpcKit::Call::Name] gRPC method object
|
10
|
+
attr_reader :method
|
11
|
+
|
12
|
+
# @return [Symbol] gRPC method name
|
13
|
+
attr_reader :method_name
|
14
|
+
|
15
|
+
# @return [String] gRPC service name
|
16
|
+
attr_reader :service_name
|
17
|
+
|
18
|
+
# @return [Hash<String, String>] gRPC metadata
|
19
|
+
attr_reader :metadata
|
20
|
+
|
21
|
+
# @param stream [GrpcKit::Stream::ServerStream|GrpcKit::Stream::ClientStream]
|
22
|
+
# @param config [GrpcKit::MethodConfig]
|
23
|
+
# @param metadata [Hash<String,String>]
|
24
|
+
def initialize(stream:, config:, metadata:, timeout: nil)
|
25
|
+
@config = config
|
26
|
+
@metadata = metadata
|
27
|
+
@method_name = @config.method_name
|
28
|
+
@service_name = @config.service_name
|
29
|
+
@protobuf = @config.protobuf
|
30
|
+
@timeout = timeout
|
31
|
+
@stream = stream
|
32
|
+
|
33
|
+
# for compatible
|
34
|
+
klass = Klass.new(@service_name)
|
35
|
+
@method ||= Name.new(@method_name, Reciver.new(klass))
|
36
|
+
end
|
37
|
+
|
38
|
+
# @return [Time] deadline of this rpc call
|
39
|
+
def deadline
|
40
|
+
return @deadline if instance_variable_defined?(:@deadline)
|
41
|
+
|
42
|
+
@deadline = @timeout && @timeout.to_absolute_time
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/grpc_kit/calls.rb
CHANGED
@@ -2,57 +2,7 @@
|
|
2
2
|
|
3
3
|
module GrpcKit
|
4
4
|
module Calls
|
5
|
-
|
6
|
-
|
7
|
-
Reciver = Struct.new(:class)
|
8
|
-
Klass = Struct.new(:service_name)
|
9
|
-
|
10
|
-
# @return [GrpcKit::Calls::Call::Name] gRPC method object
|
11
|
-
attr_reader :method
|
12
|
-
|
13
|
-
# @return [Symbol] gRPC method name
|
14
|
-
attr_reader :method_name
|
15
|
-
|
16
|
-
# @return [String] gRPC service name
|
17
|
-
attr_reader :service_name
|
18
|
-
|
19
|
-
# @return [Hash<String, String>] gRPC metadata
|
20
|
-
attr_reader :metadata
|
21
|
-
|
22
|
-
# @param stream [GrpcKit::Stream::ServerStream|GrpcKit::Stream::ClientStream]
|
23
|
-
# @param config [GrpcKit::MethodConfig]
|
24
|
-
# @param metadata [Hash<String,String>]
|
25
|
-
def initialize(stream:, config:, metadata:, timeout: nil)
|
26
|
-
@config = config
|
27
|
-
@metadata = metadata
|
28
|
-
@method_name = @config.method_name
|
29
|
-
@service_name = @config.service_name
|
30
|
-
@protobuf = @config.protobuf
|
31
|
-
@timeout = timeout
|
32
|
-
@stream = stream
|
33
|
-
|
34
|
-
# for compatible
|
35
|
-
klass = Klass.new(@service_name)
|
36
|
-
@method ||= Name.new(@method_name, Reciver.new(klass))
|
37
|
-
@restrict = false
|
38
|
-
end
|
39
|
-
|
40
|
-
# @return [void]
|
41
|
-
def restrict_mode
|
42
|
-
@restrict = true
|
43
|
-
end
|
44
|
-
|
45
|
-
# @return [void]
|
46
|
-
def normal_mode
|
47
|
-
@restrict = false
|
48
|
-
end
|
49
|
-
|
50
|
-
# @return [Time] deadline of this rpc call
|
51
|
-
def deadline
|
52
|
-
return @deadline if instance_variable_defined?(:@deadline)
|
53
|
-
|
54
|
-
@deadline = @timeout && @timeout.to_absolute_time
|
55
|
-
end
|
56
|
-
end
|
5
|
+
module Client; end
|
6
|
+
module Server; end
|
57
7
|
end
|
58
8
|
end
|
@@ -1,10 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'grpc_kit/call'
|
3
4
|
require 'grpc_kit/calls'
|
4
5
|
|
5
6
|
module GrpcKit
|
6
7
|
module Calls::Client
|
7
|
-
class BidiStreamer < GrpcKit::
|
8
|
+
class BidiStreamer < GrpcKit::Call
|
9
|
+
include Enumerable
|
10
|
+
|
8
11
|
alias outgoing_metadata metadata
|
9
12
|
|
10
13
|
def initialize(*)
|
@@ -14,17 +17,14 @@ module GrpcKit
|
|
14
17
|
end
|
15
18
|
|
16
19
|
# @param data [Object] request message
|
17
|
-
# @param last [Boolean]
|
18
20
|
# @return [void]
|
19
|
-
def send_msg(data
|
20
|
-
raise 'No method error' if @restrict
|
21
|
-
|
21
|
+
def send_msg(data)
|
22
22
|
if @reason
|
23
23
|
raise "Upstream returns an error status: #{@reason}"
|
24
24
|
end
|
25
25
|
|
26
26
|
@mutex.synchronize do
|
27
|
-
@stream.send_msg(data,
|
27
|
+
@stream.send_msg(data, metadata: outgoing_metadata)
|
28
28
|
end
|
29
29
|
|
30
30
|
@send = true
|
@@ -32,16 +32,13 @@ module GrpcKit
|
|
32
32
|
|
33
33
|
# This method not is expected to be call in the main thread where #send_msg is called
|
34
34
|
#
|
35
|
-
# @param last [Boolean]
|
36
35
|
# @return [Object] response object
|
37
|
-
def recv
|
38
|
-
raise 'No method error' if @restrict
|
39
|
-
|
36
|
+
def recv
|
40
37
|
sleep 0.1 until @send
|
41
38
|
|
42
39
|
loop do
|
43
40
|
msg = @mutex.synchronize do
|
44
|
-
@stream.recv_msg(
|
41
|
+
@stream.recv_msg(blocking: false)
|
45
42
|
end
|
46
43
|
|
47
44
|
unless msg == :wait_readable
|
@@ -60,6 +57,11 @@ module GrpcKit
|
|
60
57
|
@stream.close_and_send
|
61
58
|
end
|
62
59
|
end
|
60
|
+
|
61
|
+
# @yieldparam response [Object] each response object of bidi streaming RPC
|
62
|
+
def each
|
63
|
+
loop { yield(recv) }
|
64
|
+
end
|
63
65
|
end
|
64
66
|
end
|
65
67
|
end
|
@@ -1,31 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'grpc_kit/call'
|
3
4
|
require 'grpc_kit/calls'
|
4
5
|
|
5
6
|
module GrpcKit
|
6
7
|
module Calls::Client
|
7
|
-
class ClientStreamer < GrpcKit::
|
8
|
+
class ClientStreamer < GrpcKit::Call
|
8
9
|
alias outgoing_metadata metadata
|
9
10
|
|
10
11
|
# @param data [Object] request message
|
11
|
-
# @param last [Boolean]
|
12
12
|
# @return [void]
|
13
|
-
def send_msg(data
|
14
|
-
|
15
|
-
|
16
|
-
@stream.send_msg(data, last: last, metadata: outgoing_metadata)
|
13
|
+
def send_msg(data)
|
14
|
+
@stream.send_msg(data, metadata: outgoing_metadata)
|
17
15
|
end
|
18
16
|
|
19
|
-
# @param last [Boolean]
|
20
17
|
# @return [Object] response object
|
21
|
-
def recv
|
22
|
-
raise 'No method error' if @restrict
|
23
|
-
|
24
|
-
@stream.recv_msg(last: last)
|
25
|
-
end
|
26
|
-
|
27
|
-
# @return [Array<Object>] response objects
|
28
|
-
def close_and_recv
|
18
|
+
def recv
|
29
19
|
@stream.close_and_recv
|
30
20
|
end
|
31
21
|
end
|
@@ -1,27 +1,22 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'grpc_kit/call'
|
3
4
|
require 'grpc_kit/calls'
|
4
5
|
|
5
6
|
module GrpcKit
|
6
7
|
module Calls::Client
|
7
|
-
class RequestResponse < GrpcKit::
|
8
|
+
class RequestResponse < GrpcKit::Call
|
8
9
|
alias outgoing_metadata metadata
|
9
10
|
|
10
11
|
# @param data [Object] request message
|
11
|
-
# @param last [Boolean]
|
12
12
|
# @return [void]
|
13
|
-
def send_msg(data
|
14
|
-
|
15
|
-
|
16
|
-
@stream.send_msg(data, last: last, metadata: outgoing_metadata)
|
13
|
+
def send_msg(data)
|
14
|
+
@stream.send_msg(data, last: true, metadata: outgoing_metadata)
|
17
15
|
end
|
18
16
|
|
19
|
-
# @param last [Boolean]
|
20
17
|
# @return [Object] response object
|
21
|
-
def recv
|
22
|
-
|
23
|
-
|
24
|
-
@stream.recv_msg(last: last)
|
18
|
+
def recv
|
19
|
+
@stream.recv_msg(last: true)
|
25
20
|
end
|
26
21
|
end
|
27
22
|
end
|