Class GcpManagedChannel

java.lang.Object
io.grpc.Channel
io.grpc.ManagedChannel
com.google.cloud.grpc.GcpManagedChannel

public class GcpManagedChannel extends io.grpc.ManagedChannel
A channel management factory that implements grpc.Channel APIs.
  • Field Details

    • DISABLE_AFFINITY_CTX_KEY

      public static final io.grpc.Context.Key<Boolean> DISABLE_AFFINITY_CTX_KEY
    • DISABLE_AFFINITY_KEY

      public static final io.grpc.CallOptions.Key<Boolean> DISABLE_AFFINITY_KEY
    • AFFINITY_CTX_KEY

      public static final io.grpc.Context.Key<String> AFFINITY_CTX_KEY
    • AFFINITY_KEY

      public static final io.grpc.CallOptions.Key<String> AFFINITY_KEY
    • CHANNEL_ID_KEY

      public static final io.grpc.CallOptions.Key<Integer> CHANNEL_ID_KEY
      CallOptions key that will be set by grpc-gcp with the actual channel ID used for the call. This can be read by downstream interceptors to get the real channel ID after channel selection.
  • Constructor Details

    • GcpManagedChannel

      public GcpManagedChannel(io.grpc.ManagedChannelBuilder<?> delegateChannelBuilder, ApiConfig apiConfig, GcpManagedChannelOptions options)
      Constructor for GcpManagedChannel.
      Parameters:
      delegateChannelBuilder - the underlying delegate ManagedChannelBuilder.
      apiConfig - the ApiConfig object for configuring GcpManagedChannel.
      options - the options for GcpManagedChannel.
    • GcpManagedChannel

      @Deprecated public GcpManagedChannel(io.grpc.ManagedChannelBuilder<?> delegateChannelBuilder, ApiConfig apiConfig, int poolSize, GcpManagedChannelOptions options)
      Deprecated.
      Constructor for GcpManagedChannel. Deprecated. Use the one without the poolSize and set the maximum pool size in options. However, note that if setting the pool size from options then concurrent streams low watermark (even the default one) will be also taken from the options and not apiConfig.
      Parameters:
      delegateChannelBuilder - the underlying delegate ManagedChannelBuilder.
      apiConfig - the ApiConfig object for configuring GcpManagedChannel.
      poolSize - maximum number of channels the pool can have.
      options - the options for GcpManagedChannel.
  • Method Details

    • notifyWhenStateChanged

      public void notifyWhenStateChanged(io.grpc.ConnectivityState source, Runnable callback)
      Overrides:
      notifyWhenStateChanged in class io.grpc.ManagedChannel
    • getMaxSize

      public int getMaxSize()
    • getMinSize

      public int getMinSize()
    • getNumberOfChannels

      public int getNumberOfChannels()
    • getStreamsLowWatermark

      public int getStreamsLowWatermark()
    • getMinActiveStreams

      public int getMinActiveStreams()
    • getMaxActiveStreams

      public int getMaxActiveStreams()
    • getChannelRefForBind

      protected GcpManagedChannel.ChannelRef getChannelRefForBind()
      Returns a GcpManagedChannel.ChannelRef from the pool for a binding call. If round-robin on bind is enabled, uses getChannelRefRoundRobin() otherwise getChannelRef(String)
      Returns:
      GcpManagedChannel.ChannelRef channel to use for a call.
    • getChannelRefRoundRobin

      protected GcpManagedChannel.ChannelRef getChannelRefRoundRobin()
      Returns a GcpManagedChannel.ChannelRef from the pool in round-robin manner. Creates a new channel in the pool until the pool reaches its max size.
      Returns:
      GcpManagedChannel.ChannelRef
    • getChannelRef

      protected GcpManagedChannel.ChannelRef getChannelRef(@Nullable String key)
      Pick a GcpManagedChannel.ChannelRef (and create a new one if necessary). If notReadyFallbackEnabled is true in the GcpManagedChannelOptions.GcpResiliencyOptions then instead of a channel in a non-READY state another channel in the READY state and having fewer than maximum allowed number of active streams will be provided if available. Subsequent calls with the same affinity key will provide the same fallback channel as long as the fallback channel is in the READY state.
      Parameters:
      key - affinity key. If it is specified, pick the ChannelRef bound with the affinity key. Otherwise pick the one with the smallest number of streams.
    • authority

      public String authority()
      Specified by:
      authority in class io.grpc.Channel
    • newCall

      public <ReqT, RespT> io.grpc.ClientCall<ReqT,RespT> newCall(io.grpc.MethodDescriptor<ReqT,RespT> methodDescriptor, io.grpc.CallOptions callOptions)
      Manage the channelpool using GcpClientCall().

      If method-affinity is specified, we will use the GcpClientCall to fetch the affinitykey and bind/unbind the channel, otherwise we just need the SimpleGcpClientCall to keep track of the number of streams in each channel.

      Specified by:
      newCall in class io.grpc.Channel
    • shutdownNow

      public io.grpc.ManagedChannel shutdownNow()
      Specified by:
      shutdownNow in class io.grpc.ManagedChannel
    • shutdown

      public io.grpc.ManagedChannel shutdown()
      Specified by:
      shutdown in class io.grpc.ManagedChannel
    • awaitTermination

      public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
      Specified by:
      awaitTermination in class io.grpc.ManagedChannel
      Throws:
      InterruptedException
    • isShutdown

      public boolean isShutdown()
      Specified by:
      isShutdown in class io.grpc.ManagedChannel
    • isTerminated

      public boolean isTerminated()
      Specified by:
      isTerminated in class io.grpc.ManagedChannel
    • getState

      public io.grpc.ConnectivityState getState(boolean requestConnection)
      Get the current connectivity state of the channel pool.
      Overrides:
      getState in class io.grpc.ManagedChannel
    • bind

      protected void bind(GcpManagedChannel.ChannelRef channelRef, List<String> affinityKeys)
      Bind channel with affinity key.

      One channel can be mapped to more than one keys. But one key can only be mapped to one channel.

    • unbind

      protected void unbind(List<String> affinityKeys)
      Unbind channel with affinity key.
    • checkKeys

      @Nullable protected <ReqT, RespT> List<String> checkKeys(Object message, boolean isReq, io.grpc.MethodDescriptor<ReqT,RespT> methodDescriptor)
      Fetch the affinity key from the message.
      Parameters:
      message - the <ReqT> or <RespT> prototype message.
      isReq - indicates if the message is a request message.