|
|
@ -32,6 +32,8 @@ require 'grpc/version' |
|
|
|
|
|
|
|
|
|
|
|
# GRPC contains the General RPC module. |
|
|
|
# GRPC contains the General RPC module. |
|
|
|
module GRPC |
|
|
|
module GRPC |
|
|
|
|
|
|
|
# rubocop:disable Metrics/ParameterLists |
|
|
|
|
|
|
|
|
|
|
|
# ClientStub represents an endpoint used to send requests to GRPC servers. |
|
|
|
# ClientStub represents an endpoint used to send requests to GRPC servers. |
|
|
|
class ClientStub |
|
|
|
class ClientStub |
|
|
|
include Core::StatusCodes |
|
|
|
include Core::StatusCodes |
|
|
@ -68,6 +70,12 @@ module GRPC |
|
|
|
update_metadata |
|
|
|
update_metadata |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Allows users of the stub to modify the propagate mask. |
|
|
|
|
|
|
|
# |
|
|
|
|
|
|
|
# This is an advanced feature for use when making calls to another gRPC |
|
|
|
|
|
|
|
# server whilst running in the handler of an existing one. |
|
|
|
|
|
|
|
attr_writer :propagate_mask |
|
|
|
|
|
|
|
|
|
|
|
# Creates a new ClientStub. |
|
|
|
# Creates a new ClientStub. |
|
|
|
# |
|
|
|
# |
|
|
|
# Minimally, a stub is created with the just the host of the gRPC service |
|
|
|
# Minimally, a stub is created with the just the host of the gRPC service |
|
|
@ -91,8 +99,8 @@ module GRPC |
|
|
|
# |
|
|
|
# |
|
|
|
# - :update_metadata |
|
|
|
# - :update_metadata |
|
|
|
# when present, this a func that takes a hash and returns a hash |
|
|
|
# when present, this a func that takes a hash and returns a hash |
|
|
|
# it can be used to update metadata, i.e, remove, change or update |
|
|
|
# it can be used to update metadata, i.e, remove, or amend |
|
|
|
# amend metadata values. |
|
|
|
# metadata values. |
|
|
|
# |
|
|
|
# |
|
|
|
# @param host [String] the host the stub connects to |
|
|
|
# @param host [String] the host the stub connects to |
|
|
|
# @param q [Core::CompletionQueue] used to wait for events |
|
|
|
# @param q [Core::CompletionQueue] used to wait for events |
|
|
@ -105,6 +113,7 @@ module GRPC |
|
|
|
channel_override: nil, |
|
|
|
channel_override: nil, |
|
|
|
timeout: nil, |
|
|
|
timeout: nil, |
|
|
|
creds: nil, |
|
|
|
creds: nil, |
|
|
|
|
|
|
|
propagate_mask: nil, |
|
|
|
update_metadata: nil, |
|
|
|
update_metadata: nil, |
|
|
|
**kw) |
|
|
|
**kw) |
|
|
|
fail(TypeError, '!CompletionQueue') unless q.is_a?(Core::CompletionQueue) |
|
|
|
fail(TypeError, '!CompletionQueue') unless q.is_a?(Core::CompletionQueue) |
|
|
@ -113,6 +122,7 @@ module GRPC |
|
|
|
@update_metadata = ClientStub.check_update_metadata(update_metadata) |
|
|
|
@update_metadata = ClientStub.check_update_metadata(update_metadata) |
|
|
|
alt_host = kw[Core::Channel::SSL_TARGET] |
|
|
|
alt_host = kw[Core::Channel::SSL_TARGET] |
|
|
|
@host = alt_host.nil? ? host : alt_host |
|
|
|
@host = alt_host.nil? ? host : alt_host |
|
|
|
|
|
|
|
@propagate_mask = propagate_mask |
|
|
|
@timeout = timeout.nil? ? DEFAULT_TIMEOUT : timeout |
|
|
|
@timeout = timeout.nil? ? DEFAULT_TIMEOUT : timeout |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
@ -151,11 +161,15 @@ module GRPC |
|
|
|
# @param marshal [Function] f(obj)->string that marshals requests |
|
|
|
# @param marshal [Function] f(obj)->string that marshals requests |
|
|
|
# @param unmarshal [Function] f(string)->obj that unmarshals responses |
|
|
|
# @param unmarshal [Function] f(string)->obj that unmarshals responses |
|
|
|
# @param timeout [Numeric] (optional) the max completion time in seconds |
|
|
|
# @param timeout [Numeric] (optional) the max completion time in seconds |
|
|
|
|
|
|
|
# @param parent [Core::Call] a prior call whose reserved metadata |
|
|
|
|
|
|
|
# will be propagated by this one. |
|
|
|
# @param return_op [true|false] return an Operation if true |
|
|
|
# @param return_op [true|false] return an Operation if true |
|
|
|
# @return [Object] the response received from the server |
|
|
|
# @return [Object] the response received from the server |
|
|
|
def request_response(method, req, marshal, unmarshal, timeout = nil, |
|
|
|
def request_response(method, req, marshal, unmarshal, timeout = nil, |
|
|
|
return_op: false, **kw) |
|
|
|
return_op: false, |
|
|
|
c = new_active_call(method, marshal, unmarshal, timeout) |
|
|
|
parent: parent, |
|
|
|
|
|
|
|
**kw) |
|
|
|
|
|
|
|
c = new_active_call(method, marshal, unmarshal, timeout, parent: parent) |
|
|
|
kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method) |
|
|
|
kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method) |
|
|
|
md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri) |
|
|
|
md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri) |
|
|
|
return c.request_response(req, **md) unless return_op |
|
|
|
return c.request_response(req, **md) unless return_op |
|
|
@ -210,10 +224,14 @@ module GRPC |
|
|
|
# @param unmarshal [Function] f(string)->obj that unmarshals responses |
|
|
|
# @param unmarshal [Function] f(string)->obj that unmarshals responses |
|
|
|
# @param timeout [Numeric] the max completion time in seconds |
|
|
|
# @param timeout [Numeric] the max completion time in seconds |
|
|
|
# @param return_op [true|false] return an Operation if true |
|
|
|
# @param return_op [true|false] return an Operation if true |
|
|
|
|
|
|
|
# @param parent [Core::Call] a prior call whose reserved metadata |
|
|
|
|
|
|
|
# will be propagated by this one. |
|
|
|
# @return [Object|Operation] the response received from the server |
|
|
|
# @return [Object|Operation] the response received from the server |
|
|
|
def client_streamer(method, requests, marshal, unmarshal, timeout = nil, |
|
|
|
def client_streamer(method, requests, marshal, unmarshal, timeout = nil, |
|
|
|
return_op: false, **kw) |
|
|
|
return_op: false, |
|
|
|
c = new_active_call(method, marshal, unmarshal, timeout) |
|
|
|
parent: nil, |
|
|
|
|
|
|
|
**kw) |
|
|
|
|
|
|
|
c = new_active_call(method, marshal, unmarshal, timeout, parent: parent) |
|
|
|
kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method) |
|
|
|
kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method) |
|
|
|
md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri) |
|
|
|
md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri) |
|
|
|
return c.client_streamer(requests, **md) unless return_op |
|
|
|
return c.client_streamer(requests, **md) unless return_op |
|
|
@ -276,11 +294,16 @@ module GRPC |
|
|
|
# @param unmarshal [Function] f(string)->obj that unmarshals responses |
|
|
|
# @param unmarshal [Function] f(string)->obj that unmarshals responses |
|
|
|
# @param timeout [Numeric] the max completion time in seconds |
|
|
|
# @param timeout [Numeric] the max completion time in seconds |
|
|
|
# @param return_op [true|false]return an Operation if true |
|
|
|
# @param return_op [true|false]return an Operation if true |
|
|
|
|
|
|
|
# @param parent [Core::Call] a prior call whose reserved metadata |
|
|
|
|
|
|
|
# will be propagated by this one. |
|
|
|
# @param blk [Block] when provided, is executed for each response |
|
|
|
# @param blk [Block] when provided, is executed for each response |
|
|
|
# @return [Enumerator|Operation|nil] as discussed above |
|
|
|
# @return [Enumerator|Operation|nil] as discussed above |
|
|
|
def server_streamer(method, req, marshal, unmarshal, timeout = nil, |
|
|
|
def server_streamer(method, req, marshal, unmarshal, timeout = nil, |
|
|
|
return_op: false, **kw, &blk) |
|
|
|
return_op: false, |
|
|
|
c = new_active_call(method, marshal, unmarshal, timeout) |
|
|
|
parent: nil, |
|
|
|
|
|
|
|
**kw, |
|
|
|
|
|
|
|
&blk) |
|
|
|
|
|
|
|
c = new_active_call(method, marshal, unmarshal, timeout, parent: parent) |
|
|
|
kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method) |
|
|
|
kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method) |
|
|
|
md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri) |
|
|
|
md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri) |
|
|
|
return c.server_streamer(req, **md, &blk) unless return_op |
|
|
|
return c.server_streamer(req, **md, &blk) unless return_op |
|
|
@ -381,12 +404,17 @@ module GRPC |
|
|
|
# @param marshal [Function] f(obj)->string that marshals requests |
|
|
|
# @param marshal [Function] f(obj)->string that marshals requests |
|
|
|
# @param unmarshal [Function] f(string)->obj that unmarshals responses |
|
|
|
# @param unmarshal [Function] f(string)->obj that unmarshals responses |
|
|
|
# @param timeout [Numeric] (optional) the max completion time in seconds |
|
|
|
# @param timeout [Numeric] (optional) the max completion time in seconds |
|
|
|
# @param blk [Block] when provided, is executed for each response |
|
|
|
# @param parent [Core::Call] a prior call whose reserved metadata |
|
|
|
|
|
|
|
# will be propagated by this one. |
|
|
|
# @param return_op [true|false] return an Operation if true |
|
|
|
# @param return_op [true|false] return an Operation if true |
|
|
|
|
|
|
|
# @param blk [Block] when provided, is executed for each response |
|
|
|
# @return [Enumerator|nil|Operation] as discussed above |
|
|
|
# @return [Enumerator|nil|Operation] as discussed above |
|
|
|
def bidi_streamer(method, requests, marshal, unmarshal, timeout = nil, |
|
|
|
def bidi_streamer(method, requests, marshal, unmarshal, timeout = nil, |
|
|
|
return_op: false, **kw, &blk) |
|
|
|
return_op: false, |
|
|
|
c = new_active_call(method, marshal, unmarshal, timeout) |
|
|
|
parent: nil, |
|
|
|
|
|
|
|
**kw, |
|
|
|
|
|
|
|
&blk) |
|
|
|
|
|
|
|
c = new_active_call(method, marshal, unmarshal, timeout, parent: parent) |
|
|
|
kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method) |
|
|
|
kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method) |
|
|
|
md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri) |
|
|
|
md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri) |
|
|
|
return c.bidi_streamer(requests, **md, &blk) unless return_op |
|
|
|
return c.bidi_streamer(requests, **md, &blk) unless return_op |
|
|
@ -407,10 +435,17 @@ module GRPC |
|
|
|
# @param method [string] the method being called. |
|
|
|
# @param method [string] the method being called. |
|
|
|
# @param marshal [Function] f(obj)->string that marshals requests |
|
|
|
# @param marshal [Function] f(obj)->string that marshals requests |
|
|
|
# @param unmarshal [Function] f(string)->obj that unmarshals responses |
|
|
|
# @param unmarshal [Function] f(string)->obj that unmarshals responses |
|
|
|
|
|
|
|
# @param parent [Grpc::Call] a parent call, available when calls are |
|
|
|
|
|
|
|
# made from server |
|
|
|
# @param timeout [TimeConst] |
|
|
|
# @param timeout [TimeConst] |
|
|
|
def new_active_call(method, marshal, unmarshal, timeout = nil) |
|
|
|
def new_active_call(method, marshal, unmarshal, timeout = nil, parent: nil) |
|
|
|
deadline = from_relative_time(timeout.nil? ? @timeout : timeout) |
|
|
|
deadline = from_relative_time(timeout.nil? ? @timeout : timeout) |
|
|
|
call = @ch.create_call(@queue, method, nil, deadline) |
|
|
|
call = @ch.create_call(@queue, |
|
|
|
|
|
|
|
parent, # parent call |
|
|
|
|
|
|
|
@propagate_mask, # propagation options |
|
|
|
|
|
|
|
method, |
|
|
|
|
|
|
|
nil, # host use nil, |
|
|
|
|
|
|
|
deadline) |
|
|
|
ActiveCall.new(call, @queue, marshal, unmarshal, deadline, started: false) |
|
|
|
ActiveCall.new(call, @queue, marshal, unmarshal, deadline, started: false) |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|