Class: Gapic::ServiceStub::RpcCall

Inherits:
Object
  • Object
show all
Defined in:
lib/gapic/grpc/service_stub/rpc_call.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stub_method, stub_logger: nil, method_name: nil) ⇒ RpcCall

Creates an API object for making a single RPC call.

In typical usage, stub_method will be a proc used to make an RPC request. This will mostly likely be a bound method from a request Stub used to make an RPC call.

The result is created by applying a series of function decorators defined in this module to stub_method.

The result is another proc which has the same signature as the original.

Parameters:

  • stub_method (Proc)

    Used to make a bare rpc call.



36
37
38
39
40
41
# File 'lib/gapic/grpc/service_stub/rpc_call.rb', line 36

def initialize stub_method, stub_logger: nil, method_name: nil
  @stub_method = stub_method
  @stub_logger = stub_logger
  @method_name = method_name
  @request_id = LoggingConcerns.random_uuid4
end

Instance Attribute Details

#stub_methodObject (readonly)

Returns the value of attribute stub_method.



22
23
24
# File 'lib/gapic/grpc/service_stub/rpc_call.rb', line 22

def stub_method
  @stub_method
end

Instance Method Details

#call(request, options: nil) {|response, operation| ... } ⇒ Object

Invoke the RPC call.

Examples:

require "google/showcase/v1beta1/echo_pb"
require "google/showcase/v1beta1/echo_services_pb"
require "gapic"
require "gapic/grpc"

echo_channel = ::GRPC::Core::Channel.new(
  "localhost:7469", nil, :this_channel_is_insecure
)
echo_stub = Gapic::ServiceStub.new(
  Google::Showcase::V1beta1::Echo::Stub,
  endpoint: "localhost:7469", credentials: echo_channel
)
echo_call = Gapic::ServiceStub::RpcCall.new echo_stub.method :echo

request = Google::Showcase::V1beta1::EchoRequest.new
response = echo_call.call request

Using custom call options:

require "google/showcase/v1beta1/echo_pb"
require "google/showcase/v1beta1/echo_services_pb"
require "gapic"
require "gapic/grpc"

echo_channel = ::GRPC::Core::Channel.new(
  "localhost:7469", nil, :this_channel_is_insecure
)
echo_stub = Gapic::ServiceStub.new(
  Google::Showcase::V1beta1::Echo::Stub,
  endpoint: "localhost:7469", credentials: echo_channel
)
echo_call = Gapic::ServiceStub::RpcCall.new echo_stub.method :echo

request = Google::Showcase::V1beta1::EchoRequest.new
options = Gapic::CallOptions.new(
  retry_policy = {
    retry_codes: [::GRPC::Core::StatusCodes::UNAVAILABLE]
  }
)
response = echo_call.call request, options: options

Accessing the RPC operation using a block:

require "google/showcase/v1beta1/echo_pb"
require "google/showcase/v1beta1/echo_services_pb"
require "gapic"
require "gapic/grpc"

echo_channel = ::GRPC::Core::Channel.new(
  "localhost:7469", nil, :this_channel_is_insecure
)
echo_stub = Gapic::ServiceStub.new(
  Google::Showcase::V1beta1::Echo::Stub,
  endpoint: "localhost:7469", credentials: echo_channel
)
echo_call = Gapic::ServiceStub::RpcCall.new echo_stub.method :echo

request = Google::Showcase::V1beta1::EchoRequest.new
 = echo_call.call request do |_response, operation|
  throw :response, operation.
end

Parameters:

  • request (Object)

    The request object.

  • options (Gapic::CallOptions, Hash) (defaults to: nil)

    The options for making the RPC call. A Hash can be provided to customize the options object, using keys that match the arguments for CallOptions.new. This object should only be used once.

Yields:

  • (response, operation)

    Access the response along with the RPC operation. Additionally, throwing :response, obj within the block will change the return value to obj.

Yield Parameters:

  • response (Object)

    The response object.

  • operation (::GRPC::ActiveCall::Operation)

    The RPC operation for the response.

Returns:

  • (Object)

    The response object.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/gapic/grpc/service_stub/rpc_call.rb', line 119

def call request, options: nil
  # Converts hash and nil to an options object
  options = Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h
  deadline = calculate_deadline options
   = options.

  try_number = 1
  retried_exception = nil
  begin
    request = log_request request, , try_number
    operation = stub_method.call request, deadline: deadline, metadata: , return_op: true
    response = operation.execute
    catch :response do
      response = log_response response, try_number
      yield response, operation if block_given?
      response
    end
  rescue ::GRPC::DeadlineExceeded => e
    log_response e, try_number
    raise Gapic::GRPC::DeadlineExceededError.new e.message, root_cause: retried_exception
  rescue StandardError => e
    e = normalize_exception e
    log_response e, try_number

    if check_retry?(deadline) && options.retry_policy.call(e)
      retried_exception = e
      try_number += 1
      retry
    end

    raise e
  end
end