1515require "grpc"
1616require "googleauth"
1717require "gapic/grpc/service_stub/rpc_call"
18+ require "gapic/grpc/service_stub/channel"
19+ require "gapic/grpc/service_stub/channel_pool"
20+
1821
1922module Gapic
2023 ##
2124 # Gapic gRPC Stub
2225 #
23- # This class wraps the actual gRPC Stub object and it's RPC methods .
26+ # This class wraps the actual gRPC Stub and ChannelPool objects .
2427 #
2528 # @!attribute [r] grpc_stub
2629 # @return [Object] The instance of the gRPC stub class (`grpc_stub_class`) constructor argument.
30+ # @!attribute [r] channel_pool
31+ # @return [Gapic::ServiceStub::ChannelPool] The instance of the ChannelPool class.
2732 #
2833 class ServiceStub
2934 attr_reader :grpc_stub
35+ attr_reader :channel_pool
3036
3137 ##
3238 # Creates a Gapic gRPC stub object.
@@ -49,22 +55,49 @@ class ServiceStub
4955 # provided as a `::GRPC::Core::Channel`.)
5056 # @param interceptors [Array<::GRPC::ClientInterceptor>] An array of {::GRPC::ClientInterceptor} objects that will
5157 # be used for intercepting calls before they are executed Interceptors are an EXPERIMENTAL API.
58+ # @param channel_pool_config [::Gapic::ServiceStub:ChannelPool::Configuration] The configuration for channel
59+ # pool. This argument will raise error when `credentials` is provided as a `::GRPC::Core::Channel`.
5260 #
53- def initialize grpc_stub_class , endpoint :, credentials :, channel_args : nil , interceptors : nil
61+ def initialize grpc_stub_class , endpoint :, credentials :, channel_args : nil ,
62+ interceptors : nil , channel_pool_config : nil
5463 raise ArgumentError , "grpc_stub_class is required" if grpc_stub_class . nil?
5564 raise ArgumentError , "endpoint is required" if endpoint . nil?
5665 raise ArgumentError , "credentials is required" if credentials . nil?
5766
67+ @channel_pool = nil
68+ @grpc_stub = nil
5869 channel_args = Hash channel_args
5970 interceptors = Array interceptors
6071
72+
73+ if channel_pool_config && channel_pool_config . channel_count > 1
74+ create_channel_pool grpc_stub_class , endpoint : endpoint , credentials : credentials ,
75+ channel_args : channel_args , interceptors : interceptors ,
76+ channel_pool_config : channel_pool_config
77+ else
78+ create_grpc_stub grpc_stub_class , endpoint : endpoint , credentials : credentials ,
79+ channel_args : channel_args , interceptors : interceptors
80+ end
81+ end
82+
83+ def create_channel_pool grpc_stub_class , endpoint :, credentials :, channel_args : nil ,
84+ interceptors : nil , channel_pool_config : nil
85+ if credentials . is_a? ::GRPC ::Core ::Channel
86+ raise ArgumentError , "Cannot create a channel pool with GRPC::Core::Channel as credentials"
87+ end
88+ @channel_pool = ChannelPool . new grpc_stub_class , endpoint : endpoint , credentials : credentials ,
89+ channel_args : channel_args , interceptors : interceptors ,
90+ config : channel_pool_config
91+ end
92+
93+ def create_grpc_stub grpc_stub_class , endpoint :, credentials :, channel_args : nil , interceptors : nil
6194 @grpc_stub = case credentials
6295 when ::GRPC ::Core ::Channel
6396 grpc_stub_class . new endpoint , nil , channel_override : credentials ,
64- interceptors : interceptors
97+ interceptors : interceptors
6598 when ::GRPC ::Core ::ChannelCredentials , Symbol
6699 grpc_stub_class . new endpoint , credentials , channel_args : channel_args ,
67- interceptors : interceptors
100+ interceptors : interceptors
68101 else
69102 updater_proc = credentials . updater_proc if credentials . respond_to? :updater_proc
70103 updater_proc ||= credentials if credentials . is_a? Proc
@@ -73,7 +106,7 @@ def initialize grpc_stub_class, endpoint:, credentials:, channel_args: nil, inte
73106 call_creds = ::GRPC ::Core ::CallCredentials . new updater_proc
74107 chan_creds = ::GRPC ::Core ::ChannelCredentials . new . compose call_creds
75108 grpc_stub_class . new endpoint , chan_creds , channel_args : channel_args ,
76- interceptors : interceptors
109+ interceptors : interceptors
77110 end
78111 end
79112
@@ -152,8 +185,12 @@ def initialize grpc_stub_class, endpoint:, credentials:, channel_args: nil, inte
152185 # end
153186 #
154187 def call_rpc method_name , request , options : nil , &block
155- rpc_call = RpcCall . new @grpc_stub . method method_name
156- rpc_call . call request , options : options , &block
188+ if @channel_pool . nil?
189+ rpc_call = RpcCall . new @grpc_stub . method method_name
190+ rpc_call . call request , options : options , &block
191+ else
192+ @channel_pool . call_rpc method_name , request , options : options , &block
193+ end
157194 end
158195 end
159196end
0 commit comments