grpc_kit 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/TODO.md +1 -1
- data/examples/helloworld_client.rb +5 -1
- data/examples/routeguide_client.rb +5 -2
- data/lib/grpc_kit/calls/client_client_streamer.rb +29 -0
- data/lib/grpc_kit/calls/client_request_response.rb +25 -0
- data/lib/grpc_kit/calls/client_server_streamer.rb +29 -0
- data/lib/grpc_kit/calls/server_client_streamer.rb +32 -0
- data/lib/grpc_kit/calls/server_request_response.rb +32 -0
- data/lib/grpc_kit/calls/server_server_streamer.rb +32 -0
- data/lib/grpc_kit/calls.rb +39 -0
- data/lib/grpc_kit/client.rb +17 -17
- data/lib/grpc_kit/grpc_time.rb +100 -0
- data/lib/grpc_kit/interceptors/client_client_streamer.rb +19 -0
- data/lib/grpc_kit/interceptors/client_request_response.rb +39 -0
- data/lib/grpc_kit/interceptors/client_server_streamer.rb +19 -0
- data/lib/grpc_kit/interceptors/server_client_streamer.rb +15 -0
- data/lib/grpc_kit/interceptors/server_request_response.rb +36 -0
- data/lib/grpc_kit/interceptors/server_server_streamer.rb +17 -0
- data/lib/grpc_kit/interceptors.rb +64 -5
- data/lib/grpc_kit/rpc_desc.rb +18 -2
- data/lib/grpc_kit/rpcs/client_bidi_streamer.rb +11 -0
- data/lib/grpc_kit/rpcs/client_client_streamer.rb +23 -0
- data/lib/grpc_kit/rpcs/client_request_response.rb +31 -0
- data/lib/grpc_kit/rpcs/client_server_streamer.rb +19 -0
- data/lib/grpc_kit/rpcs/server_bidi_streamer.rb +10 -0
- data/lib/grpc_kit/rpcs/server_client_streamer.rb +24 -0
- data/lib/grpc_kit/rpcs/server_request_response.rb +26 -0
- data/lib/grpc_kit/rpcs/server_server_streamer.rb +26 -0
- data/lib/grpc_kit/rpcs.rb +21 -5
- data/lib/grpc_kit/server.rb +16 -7
- data/lib/grpc_kit/session/headers.rb +2 -2
- data/lib/grpc_kit/session/stream.rb +5 -0
- data/lib/grpc_kit/{session/client.rb → sessions/client_session.rb} +5 -6
- data/lib/grpc_kit/{session/server.rb → sessions/server_session.rb} +21 -8
- data/lib/grpc_kit/streams/client_stream.rb +146 -0
- data/lib/grpc_kit/streams/server_stream.rb +105 -0
- data/lib/grpc_kit/{streams → transport}/packable.rb +2 -2
- data/lib/grpc_kit/{streams → transport}/send_buffer.rb +1 -1
- data/lib/grpc_kit/transports/client_transport.rb +96 -0
- data/lib/grpc_kit/transports/server_transport.rb +68 -0
- data/lib/grpc_kit/version.rb +1 -1
- metadata +32 -21
- data/lib/grpc_kit/interceptors/client_streamer.rb +0 -31
- data/lib/grpc_kit/interceptors/request_response.rb +0 -70
- data/lib/grpc_kit/interceptors/server_streamer.rb +0 -33
- data/lib/grpc_kit/interceptors/streaming.rb +0 -70
- data/lib/grpc_kit/rpcs/base.rb +0 -30
- data/lib/grpc_kit/rpcs/bidi_streamer.rb +0 -18
- data/lib/grpc_kit/rpcs/call.rb +0 -27
- data/lib/grpc_kit/rpcs/client_streamer.rb +0 -38
- data/lib/grpc_kit/rpcs/error.rb +0 -23
- data/lib/grpc_kit/rpcs/request_response.rb +0 -64
- data/lib/grpc_kit/rpcs/server_streamer.rb +0 -42
- data/lib/grpc_kit/session/duration.rb +0 -97
- data/lib/grpc_kit/stream.rb +0 -120
- data/lib/grpc_kit/streams/client.rb +0 -113
- data/lib/grpc_kit/streams/server.rb +0 -54
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7415d182781136999b2e1630ef9dff77a61196225b4207e9d9535a2b566fceee
|
4
|
+
data.tar.gz: 9836c54ed198740c4a75e90b957a641528b06333a30314c03441b9dce35f8731
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '083b685dd4f7966156a1ac41eab594ace1664c813105e1204c521ce85e727945170ef1c701097ee6f93467407b13d08ea649d6da5f86af54ebd60ca1e373336e'
|
7
|
+
data.tar.gz: 505029bdcbfb035cb41b10ed845cec6ce27eae2ff350a27f49cb8a3b2cb4afeca92b8e5b9a7bc70e7dc0e2e8bde9d88591cc8899a871d81d2665c5c8141a1d14
|
data/.travis.yml
CHANGED
data/TODO.md
CHANGED
@@ -45,7 +45,7 @@
|
|
45
45
|
- [x] resouce exhausted (body size is to large)
|
46
46
|
- [x] internal
|
47
47
|
- [ ] resouce exhausted (worker is exhausted)
|
48
|
-
- [
|
48
|
+
- [x] duration parse in header
|
49
49
|
- [ ] send `grpc-status` along with header frame if possible
|
50
50
|
- need to support https://nghttp2.org/documentation/nghttp2_submit_response.html, data_prd is not NULL
|
51
51
|
- [x] unimplemented error
|
@@ -7,6 +7,10 @@ require 'socket'
|
|
7
7
|
require 'pry'
|
8
8
|
require 'helloworld_services_pb'
|
9
9
|
|
10
|
-
|
10
|
+
HOST = 'localhost'
|
11
|
+
PORT = 50051
|
12
|
+
|
13
|
+
sock = TCPSocket.new(HOST, PORT)
|
14
|
+
stub = Helloworld::Greeter::Stub.new(sock)
|
11
15
|
message = stub.say_hello(Helloworld::HelloRequest.new(name: 'ganmacs')).message
|
12
16
|
p message
|
@@ -11,6 +11,8 @@ require 'logger'
|
|
11
11
|
require 'routeguide_services_pb'
|
12
12
|
|
13
13
|
RESOURCE_PATH = './examples/routeguide/routeguide.json'
|
14
|
+
HOST = 'localhost'
|
15
|
+
PORT = 50051
|
14
16
|
|
15
17
|
$logger = Logger.new(STDOUT)
|
16
18
|
|
@@ -22,7 +24,7 @@ def get_feature(stub)
|
|
22
24
|
]
|
23
25
|
|
24
26
|
points.each do |pt|
|
25
|
-
feature = stub.get_feature(pt
|
27
|
+
feature = stub.get_feature(pt)
|
26
28
|
if feature.name == ''
|
27
29
|
$logger.info("Found nothing at #{feature.inspect}")
|
28
30
|
else
|
@@ -74,7 +76,8 @@ elsif ENV['GRPC_TIMEOUT']
|
|
74
76
|
opts[:timeout] = Integer(ENV['GRPC_TIMEOUT'])
|
75
77
|
end
|
76
78
|
|
77
|
-
|
79
|
+
sock = TCPSocket.new(HOST, PORT)
|
80
|
+
stub = Routeguide::RouteGuide::Stub.new(sock, **opts)
|
78
81
|
|
79
82
|
get_feature(stub)
|
80
83
|
list_features(stub)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_kit/calls'
|
4
|
+
|
5
|
+
module GrpcKit
|
6
|
+
module Calls::Client
|
7
|
+
class ClientStreamer < GrpcKit::Calls::Call
|
8
|
+
attr_reader :deadline, :service_name, :method_name
|
9
|
+
attr_reader :metadata
|
10
|
+
alias outgoing_metadata metadata
|
11
|
+
|
12
|
+
def send_msg(data, timeout: nil, metadata: {}, last: false)
|
13
|
+
raise 'No method error' if @restrict
|
14
|
+
|
15
|
+
@stream.send_msg(data, last: last, timeout: timeout, metadata: metadata)
|
16
|
+
end
|
17
|
+
|
18
|
+
def recv(last: false)
|
19
|
+
raise 'No method error' if @restrict
|
20
|
+
|
21
|
+
@stream.recv_msg(last: last)
|
22
|
+
end
|
23
|
+
|
24
|
+
def close_and_recv
|
25
|
+
@stream.close_and_recv
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_kit/calls'
|
4
|
+
|
5
|
+
module GrpcKit
|
6
|
+
module Calls::Client
|
7
|
+
class RequestResponse < GrpcKit::Calls::Call
|
8
|
+
attr_reader :deadline, :service_name, :method_name
|
9
|
+
attr_reader :metadata
|
10
|
+
alias outgoing_metadata metadata
|
11
|
+
|
12
|
+
def send_msg(data, timeout: nil, metadata: {}, last: false)
|
13
|
+
raise 'No method error' if @restrict
|
14
|
+
|
15
|
+
@stream.send_msg(data, last: last, timeout: timeout, metadata: metadata)
|
16
|
+
end
|
17
|
+
|
18
|
+
def recv(last: false)
|
19
|
+
raise 'No method error' if @restrict
|
20
|
+
|
21
|
+
@stream.recv_msg(last: last)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_kit/calls'
|
4
|
+
|
5
|
+
module GrpcKit
|
6
|
+
module Calls::Client
|
7
|
+
class ServerStreamer < GrpcKit::Calls::Call
|
8
|
+
attr_reader :deadline, :service_name, :method_name
|
9
|
+
attr_reader :metadata
|
10
|
+
alias outgoing_metadata metadata
|
11
|
+
|
12
|
+
def send_msg(data, timeout: nil, metadata: {}, last: false)
|
13
|
+
raise 'No method error' if @restrict
|
14
|
+
|
15
|
+
@stream.send_msg(data, last: last, timeout: timeout, metadata: metadata)
|
16
|
+
end
|
17
|
+
|
18
|
+
def recv(last: false)
|
19
|
+
raise 'No method error' if @restrict
|
20
|
+
|
21
|
+
@stream.recv_msg(last: last)
|
22
|
+
end
|
23
|
+
|
24
|
+
def each
|
25
|
+
# TODO
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_kit/calls'
|
4
|
+
|
5
|
+
module GrpcKit
|
6
|
+
module Calls::Server
|
7
|
+
class ClientStreamer < GrpcKit::Calls::Call
|
8
|
+
attr_reader :deadline, :metadata
|
9
|
+
attr_reader :outgoing_initial_metadata, :outgoing_trailing_metadata
|
10
|
+
alias incoming_metadata metadata
|
11
|
+
|
12
|
+
def initialize(*)
|
13
|
+
super
|
14
|
+
|
15
|
+
@outgoing_initial_metadata = {}
|
16
|
+
@outgoing_trailing_metadata = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def send_msg(data, last: false)
|
20
|
+
raise 'No method error' if @restrict
|
21
|
+
|
22
|
+
@stream.send_msg(data, @protobuf, last: last, limit_size: @config.max_send_message_size)
|
23
|
+
end
|
24
|
+
|
25
|
+
def recv(last: false)
|
26
|
+
raise 'No method error' if @restrict
|
27
|
+
|
28
|
+
@stream.recv_msg(@protobuf, last: last, limit_size: @config.max_receive_message_size)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_kit/calls'
|
4
|
+
|
5
|
+
module GrpcKit
|
6
|
+
module Calls::Server
|
7
|
+
class RequestResponse < GrpcKit::Calls::Call
|
8
|
+
attr_reader :deadline, :metadata
|
9
|
+
attr_reader :outgoing_initial_metadata, :outgoing_trailing_metadata
|
10
|
+
alias incoming_metadata metadata
|
11
|
+
|
12
|
+
def initialize(*)
|
13
|
+
super
|
14
|
+
|
15
|
+
@outgoing_initial_metadata = {}
|
16
|
+
@outgoing_trailing_metadata = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def send_msg(data, last: false)
|
20
|
+
raise 'No method error' if @restrict
|
21
|
+
|
22
|
+
@stream.send_msg(data, @protobuf, last: last, limit_size: @config.max_send_message_size)
|
23
|
+
end
|
24
|
+
|
25
|
+
def recv(last: false)
|
26
|
+
raise 'No method error' if @restrict
|
27
|
+
|
28
|
+
@stream.recv_msg(@protobuf, last: last, limit_size: @config.max_receive_message_size)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_kit/calls'
|
4
|
+
|
5
|
+
module GrpcKit
|
6
|
+
module Calls::Server
|
7
|
+
class ServerStreamer < GrpcKit::Calls::Call
|
8
|
+
attr_reader :deadline, :metadata
|
9
|
+
attr_reader :outgoing_initial_metadata, :outgoing_trailing_metadata
|
10
|
+
alias incoming_metadata metadata
|
11
|
+
|
12
|
+
def initialize(*)
|
13
|
+
super
|
14
|
+
|
15
|
+
@outgoing_initial_metadata = {}
|
16
|
+
@outgoing_trailing_metadata = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def send_msg(data, last: false)
|
20
|
+
raise 'No method error' if @restrict
|
21
|
+
|
22
|
+
@stream.send_msg(data, @protobuf, last: last, limit_size: @config.max_send_message_size)
|
23
|
+
end
|
24
|
+
|
25
|
+
def recv(last: false)
|
26
|
+
raise 'No method error' if @restrict
|
27
|
+
|
28
|
+
@stream.recv_msg(@protobuf, last: last, limit_size: @config.max_receive_message_size)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrpcKit
|
4
|
+
module Calls
|
5
|
+
class Call
|
6
|
+
Name = Struct.new(:name, :receiver)
|
7
|
+
Reciver = Struct.new(:class)
|
8
|
+
Klass = Struct.new(:service_name)
|
9
|
+
attr_reader :method
|
10
|
+
|
11
|
+
def initialize(stream:, config:, metadata:, timeout: nil)
|
12
|
+
@config = config
|
13
|
+
@metadata = metadata
|
14
|
+
@method_name = @config.method_name
|
15
|
+
@service_name = @config.service_name
|
16
|
+
@protobuf = @config.protobuf
|
17
|
+
@timeout = timeout
|
18
|
+
@stream = stream
|
19
|
+
|
20
|
+
# for compatible
|
21
|
+
klass = Klass.new(@service_name)
|
22
|
+
@method ||= Name.new(@method_name, Reciver.new(klass))
|
23
|
+
@restrict = false
|
24
|
+
end
|
25
|
+
|
26
|
+
def restrict_mode
|
27
|
+
@restrict = true
|
28
|
+
end
|
29
|
+
|
30
|
+
def normal_mode
|
31
|
+
@restrict = false
|
32
|
+
end
|
33
|
+
|
34
|
+
def deadline
|
35
|
+
@deadline ||= @timeout.to_absolute_time
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/grpc_kit/client.rb
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
# frozen_string_literal: false
|
2
2
|
|
3
|
-
require '
|
4
|
-
require 'grpc_kit/session/client'
|
5
|
-
require 'grpc_kit/session/duration'
|
3
|
+
require 'grpc_kit/grpc_time'
|
6
4
|
require 'grpc_kit/session/io'
|
7
|
-
require 'grpc_kit/
|
5
|
+
require 'grpc_kit/sessions/client_session'
|
6
|
+
require 'grpc_kit/streams/client_stream'
|
7
|
+
require 'grpc_kit/transports/client_transport'
|
8
8
|
|
9
9
|
module GrpcKit
|
10
10
|
class Client
|
11
|
-
def initialize(
|
12
|
-
@
|
13
|
-
@
|
14
|
-
|
15
|
-
|
16
|
-
@timeout =
|
17
|
-
if timeout
|
18
|
-
GrpcKit::Session::Duration.from_numeric(timeout)
|
11
|
+
def initialize(sock, authority: nil, interceptors: [], timeout: nil)
|
12
|
+
@sock = sock
|
13
|
+
@authority =
|
14
|
+
if authority
|
15
|
+
authority
|
19
16
|
else
|
20
|
-
|
17
|
+
addr = sock.addr
|
18
|
+
"#{addr[3]}:#{addr[1]}"
|
21
19
|
end
|
20
|
+
@interceptors = interceptors
|
21
|
+
@timeout = timeout && GrpcKit::GrpcTime.new(timeout)
|
22
22
|
end
|
23
23
|
|
24
24
|
def request_response(rpc, request, opts = {})
|
@@ -48,12 +48,12 @@ module GrpcKit
|
|
48
48
|
private
|
49
49
|
|
50
50
|
def do_request(rpc, request, **opts)
|
51
|
-
|
52
|
-
session = GrpcKit::Session::Client.new(GrpcKit::Session::IO.new(sock), rpc)
|
51
|
+
session = GrpcKit::Sessions::ClientSession.new(GrpcKit::Session::IO.new(@sock))
|
53
52
|
session.submit_settings([])
|
54
53
|
|
55
|
-
|
56
|
-
|
54
|
+
t = GrpcKit::Transports::ClientTransport.new(session)
|
55
|
+
cs = GrpcKit::Streams::ClientStream.new(t, rpc.config, authority: @authority, timeout: @timeout)
|
56
|
+
rpc.invoke(cs, request, opts.merge(timeout: @timeout))
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrpcKit
|
4
|
+
class GrpcTime
|
5
|
+
MAX = 10**9-1
|
6
|
+
|
7
|
+
# @params value [String|Integer]
|
8
|
+
def initialize(value)
|
9
|
+
@unit = nil
|
10
|
+
@value = nil
|
11
|
+
|
12
|
+
if value.is_a?(String)
|
13
|
+
from_string(value)
|
14
|
+
elsif value.is_a?(Integer)
|
15
|
+
from_integer(value)
|
16
|
+
else
|
17
|
+
raise ArgumentError, "unsupported value: #{value}, class=#{value.class}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# @return [Float]
|
22
|
+
def to_f
|
23
|
+
case @unit
|
24
|
+
when 'S'
|
25
|
+
@value * 1.0
|
26
|
+
when 'H'
|
27
|
+
@value * 60 * 60.0
|
28
|
+
when 'M'
|
29
|
+
@value * 60.0
|
30
|
+
when 'm'
|
31
|
+
@value * 10**-3
|
32
|
+
when 'u'
|
33
|
+
@value * 10**-6
|
34
|
+
when 'n'
|
35
|
+
@value * 10**-9
|
36
|
+
else
|
37
|
+
raise 'This case would never happen'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_s
|
42
|
+
"#{@value}#{@unit}"
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_absolute_time
|
46
|
+
case @unit
|
47
|
+
when 'S'
|
48
|
+
Time.now + @value
|
49
|
+
when 'H'
|
50
|
+
Time.now + @value * 60 * 60
|
51
|
+
when 'M'
|
52
|
+
Time.now + @value * 60
|
53
|
+
when 'm'
|
54
|
+
t = Time.now
|
55
|
+
Time.at(t.to_i, (t.nsec * (10**-3)) + (@value * 10**3))
|
56
|
+
when 'u'
|
57
|
+
t = Time.now
|
58
|
+
Time.at(t.to_i, (t.nsec * (10**-3)) + @value)
|
59
|
+
when 'n'
|
60
|
+
t = Time.now
|
61
|
+
Time.at(t.to_i, (t.nsec * (10**-3)) + (@value * 10**-3))
|
62
|
+
else
|
63
|
+
raise 'This case would never happen'
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def from_integer(value)
|
70
|
+
if value < 0
|
71
|
+
@value = MAX
|
72
|
+
else
|
73
|
+
@value = value
|
74
|
+
end
|
75
|
+
|
76
|
+
@unit = 'S'
|
77
|
+
end
|
78
|
+
|
79
|
+
def from_string(value)
|
80
|
+
size = value.size
|
81
|
+
if size < 2
|
82
|
+
raise ArgumentError, "Invalid format: too short #{value}"
|
83
|
+
end
|
84
|
+
|
85
|
+
if size > 9
|
86
|
+
raise ArgumentError, "Invalid format: too long #{value}"
|
87
|
+
end
|
88
|
+
|
89
|
+
unit = value.byteslice(-1, 1)
|
90
|
+
value = Integer(value.byteslice(0, size - 1))
|
91
|
+
case unit
|
92
|
+
when 'H', 'M', 'S', 'm', 'u', 'n'
|
93
|
+
@value = value
|
94
|
+
@unit = unit
|
95
|
+
else
|
96
|
+
raise ArgumentError, "Invalid unit `#{unit}`: #{value}"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_kit/interceptors'
|
4
|
+
|
5
|
+
module GrpcKit
|
6
|
+
module Interceptors::Client
|
7
|
+
class ClientStreamer < Streaming
|
8
|
+
private
|
9
|
+
|
10
|
+
def invoke(interceptor, call, metadata)
|
11
|
+
# We don't need a `:requests` parameter but,
|
12
|
+
# it shuoldn't remove from paramters due to having a compatibility of grpc gem.
|
13
|
+
interceptor.client_streamer(requests: nil, call: call, method: call.method, metadata: metadata) do
|
14
|
+
yield(call, metadata)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_kit/interceptors'
|
4
|
+
|
5
|
+
module GrpcKit
|
6
|
+
module Interceptors::Client
|
7
|
+
class RequestResponse
|
8
|
+
attr_writer :interceptors
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
# Cant' get interceptor at definition time...
|
12
|
+
@interceptors = nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def intercept(request, call, metadata, &block)
|
16
|
+
if @interceptors && !@interceptors.empty?
|
17
|
+
do_intercept(@interceptors.dup, request, call, metadata, &block)
|
18
|
+
else
|
19
|
+
yield(request, call, metadata)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def do_intercept(interceptors, request, call, metadata)
|
26
|
+
if interceptors.empty?
|
27
|
+
return yield(request, call, metadata)
|
28
|
+
end
|
29
|
+
|
30
|
+
interceptor = interceptors.pop
|
31
|
+
interceptor.request_response(request: request, call: call, method: call.method, metadata: metadata) do
|
32
|
+
do_intercept(interceptors, request, call, metadata) do |r, c, m|
|
33
|
+
yield(r, c, m)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_kit/interceptors'
|
4
|
+
|
5
|
+
module GrpcKit
|
6
|
+
module Interceptors::Client
|
7
|
+
class ServerStreamer < Streaming
|
8
|
+
private
|
9
|
+
|
10
|
+
def invoke(interceptor, call, metadata)
|
11
|
+
# We don't need a `:request` parameter but,
|
12
|
+
# it shuoldn't remove from paramters due to having a compatibility of grpc gem.
|
13
|
+
interceptor.server_streamer(request: nil, call: call, method: call.method, metadata: metadata) do
|
14
|
+
yield(call, metadata)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_kit/interceptors'
|
4
|
+
|
5
|
+
module GrpcKit
|
6
|
+
module Interceptors::Server
|
7
|
+
class ClientStreamer < Streaming
|
8
|
+
def invoke(interceptor, call)
|
9
|
+
interceptor.client_streamer(call: call, method: call.method) do
|
10
|
+
yield(call)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_kit/interceptors'
|
4
|
+
|
5
|
+
module GrpcKit
|
6
|
+
module Interceptors::Server
|
7
|
+
class RequestResponse
|
8
|
+
def initialize(interceptors)
|
9
|
+
@interceptors = interceptors
|
10
|
+
end
|
11
|
+
|
12
|
+
def intercept(request, call, &block)
|
13
|
+
if @interceptors && !@interceptors.empty?
|
14
|
+
do_intercept(@interceptors.dup, request, call, &block)
|
15
|
+
else
|
16
|
+
yield(request, call)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def do_intercept(interceptors, request, call)
|
23
|
+
if interceptors.empty?
|
24
|
+
return yield(request, call)
|
25
|
+
end
|
26
|
+
|
27
|
+
interceptor = interceptors.pop
|
28
|
+
interceptor.request_response(request: request, call: call, method: call.method) do
|
29
|
+
do_intercept(interceptors, request, call) do |req, c|
|
30
|
+
yield(req, c)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_kit/interceptors'
|
4
|
+
|
5
|
+
module GrpcKit
|
6
|
+
module Interceptors::Server
|
7
|
+
class ServerStreamer < Streaming
|
8
|
+
def invoke(interceptor, call)
|
9
|
+
# We don't need a `:request` parameter but,
|
10
|
+
# it shuoldn't remove from paramters due to having a compatibility of grpc gem.
|
11
|
+
interceptor.server_streamer(request: nil, call: call, method: call.method) do
|
12
|
+
yield(call)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -1,11 +1,70 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'grpc_kit/interceptors/request_response'
|
4
|
-
require 'grpc_kit/interceptors/server_streamer'
|
5
|
-
require 'grpc_kit/interceptors/client_streamer'
|
6
|
-
# TODO: bidi
|
7
|
-
|
8
3
|
module GrpcKit
|
9
4
|
module Interceptors
|
5
|
+
module Client
|
6
|
+
class Streaming
|
7
|
+
attr_writer :interceptors
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
# Cant' get interceptor at definition time...
|
11
|
+
@interceptors = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def intercept(call, metadata, &block)
|
15
|
+
if @interceptors && !@interceptors.empty?
|
16
|
+
do_intercept(@interceptors.dup, call, metadata, &block)
|
17
|
+
else
|
18
|
+
yield(call, metadata)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def do_intercept(interceptors, call, metadata)
|
25
|
+
if interceptors.empty?
|
26
|
+
return yield(call, metadata)
|
27
|
+
end
|
28
|
+
|
29
|
+
interceptor = interceptors.pop
|
30
|
+
invoke(interceptor, call, metadata) do |inter_call, meta|
|
31
|
+
do_intercept(interceptors, inter_call, meta) do |c, m|
|
32
|
+
yield(c, m)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module Server
|
40
|
+
class Streaming
|
41
|
+
def initialize(interceptors)
|
42
|
+
@interceptors = interceptors
|
43
|
+
end
|
44
|
+
|
45
|
+
def intercept(call, &block)
|
46
|
+
if @interceptors && !@interceptors.empty?
|
47
|
+
do_intercept(@interceptors.dup, call, &block)
|
48
|
+
else
|
49
|
+
yield(call)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def do_intercept(interceptors, call)
|
56
|
+
if interceptors.empty?
|
57
|
+
return yield(call)
|
58
|
+
end
|
59
|
+
|
60
|
+
interceptor = interceptors.pop
|
61
|
+
invoke(interceptor, call) do |inter_call|
|
62
|
+
do_intercept(interceptors, inter_call) do |c|
|
63
|
+
yield(c)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
10
69
|
end
|
11
70
|
end
|