1818
1919import static com .google .cloud .datastore .Validator .validateNamespace ;
2020
21- import com .google .api .core .ApiFunction ;
2221import com .google .api .core .BetaApi ;
22+ import com .google .api .core .ObsoleteApi ;
2323import com .google .api .gax .grpc .ChannelPoolSettings ;
2424import com .google .api .gax .grpc .InstantiatingGrpcChannelProvider ;
2525import com .google .api .gax .rpc .TransportChannelProvider ;
3737import com .google .cloud .http .HttpTransportOptions ;
3838import com .google .common .base .MoreObjects ;
3939import com .google .common .collect .ImmutableSet ;
40- import io .grpc .ManagedChannelBuilder ;
4140import java .io .IOException ;
4241import java .lang .reflect .Method ;
4342import java .util .Objects ;
@@ -54,9 +53,18 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreOptions
5453 private static final String DEFAULT_DATABASE_ID = "" ;
5554 public static final String PROJECT_ID_ENV_VAR = "DATASTORE_PROJECT_ID" ;
5655 public static final String LOCAL_HOST_ENV_VAR = "DATASTORE_EMULATOR_HOST" ;
57- public static final int INIT_CHANNEL_COUNT = 1 ;
56+ public static final int INIT_CHANNEL_COUNT = 5 ;
57+ static final int CHANNEL_POOL_DEFAULT_RESIZE_DELTA = 5 ;
58+ // Configure this default to be 100 to match the typical default `MAX_CONCURRENT_STREAMS`.
59+ // Larger values *may* experience possible client-side queueing as excess streams cannot be
60+ // multiplexed onto a full Http2 connection.
61+ static final int CHANNEL_POOL_MAX_RPCS_PER_CHANNEL = 100 ;
5862 public static final int MIN_CHANNEL_COUNT = 1 ;
59- public static final int MAX_CHANNEL_COUNT = 4 ;
63+
64+ // This is a default max channel constant value set to handle the default initial channel
65+ // count and resize delta.
66+ @ ObsoleteApi ("This constant is obsolete and will be removed in a future version" )
67+ public static final int MAX_CHANNEL_COUNT = 10 ;
6068
6169 private transient TransportChannelProvider channelProvider = null ;
6270
@@ -233,33 +241,29 @@ private DatastoreOptions(Builder builder) {
233241 "Only gRPC transport allows setting of channel provider or credentials provider" );
234242 } else if (getTransportOptions () instanceof GrpcTransportOptions ) {
235243 if (builder .channelProvider == null ) {
236- /*
237- The default gRPC connection pool is configured with a minimum of 1 channel.
238- The maximum channel count automatically defaults to 200 (Defined in gax-grpc).
239- */
244+ // Set the default gRPC connection pool to be configured with a minimum of 1 channel.
245+ // The maximum channel count automatically defaults to 200 (as defined in gax-grpc).
246+ // Datastore sets the initial channel pool count to be 5 channels to allow better handle
247+ // large loads of requests and the resize delta to be 5 to scale quicker. In cases of low
248+ // load, the channel count will scale down as needed and memory will be freed. The default
249+ // configuration is set to try and handle ~500 QPS and will scale up and down as needed.
240250 ChannelPoolSettings datastoreChannelPoolSettings =
241251 ChannelPoolSettings .builder ()
242252 .setInitialChannelCount (INIT_CHANNEL_COUNT )
243253 .setMinChannelCount (MIN_CHANNEL_COUNT )
254+ .setMaxRpcsPerChannel (CHANNEL_POOL_MAX_RPCS_PER_CHANNEL )
255+ .setMaxResizeDelta (CHANNEL_POOL_DEFAULT_RESIZE_DELTA )
244256 .build ();
245257
246- ApiFunction <ManagedChannelBuilder , ManagedChannelBuilder > channelConfigurator =
247- this .traceUtil .getChannelConfigurator ();
248- if (channelConfigurator == null ) {
249- this .channelProvider =
250- GrpcTransportOptions .setUpChannelProvider (
251- DatastoreSettings .defaultGrpcTransportProviderBuilder ()
252- .setChannelPoolSettings (datastoreChannelPoolSettings ),
253- this );
254- } else {
258+ InstantiatingGrpcChannelProvider .Builder channelProviderBuilder =
259+ DatastoreSettings .defaultGrpcTransportProviderBuilder ()
260+ .setChannelPoolSettings (datastoreChannelPoolSettings )
261+ .setEndpoint (getHost ());
262+ if (traceUtil .getChannelConfigurator () != null ) {
255263 // Intercept the grpc channel calls to add telemetry info.
256- this .channelProvider =
257- GrpcTransportOptions .setUpChannelProvider (
258- DatastoreSettings .defaultGrpcTransportProviderBuilder ()
259- .setChannelPoolSettings (datastoreChannelPoolSettings )
260- .setChannelConfigurator (channelConfigurator ),
261- this );
264+ channelProviderBuilder .setChannelConfigurator (traceUtil .getChannelConfigurator ());
262265 }
266+ this .channelProvider = channelProviderBuilder .build ();
263267 } else {
264268 this .channelProvider = builder .channelProvider ;
265269 }
0 commit comments