Package emissary.grpc

Class GrpcRoutingPlace

  • All Implemented Interfaces:
    IGrpcRoutingPlace, IServiceProviderPlace, ServiceProviderPlaceMBean
    Direct Known Subclasses:
    GrpcConnectionPlace, GrpcSampleServicePlace

    public abstract class GrpcRoutingPlace
    extends ServiceProviderPlace
    implements IGrpcRoutingPlace
    Place for processing data using gRPC connections to external services. Supports multiple end-points with shared configurations, where each endpoint is identified by a given target ID.

    Configuration Keys:

    • GRPC_HOST_{Target-ID} - gRPC service hostname or DNS target, where Target-ID is the unique identifier for the given host:port
    • GRPC_PORT_{Target-ID} - gRPC service port, where Target-ID is the unique identifier for the given host:port
    • See ConnectionFactory for supported pooling and gRPC channel configuration keys and defaults.
    • See RetryHandler for supported retry configuration keys and defaults.
    • Method Detail

      • getHostnameConfigs

        protected Map<String,​String> getHostnameConfigs()
      • getPortNumberConfigs

        protected Map<String,​Integer> getPortNumberConfigs()
      • validateConnection

        protected abstract boolean validateConnection​(io.grpc.ManagedChannel managedChannel)
        Validates whether a given ManagedChannel is capable of successfully communicating with its associated gRPC server.
        Parameters:
        managedChannel - the gRPC channel to validate
        Returns:
        true if the channel is healthy and the server responds successfully, else false
      • passivateConnection

        protected void passivateConnection​(io.grpc.ManagedChannel managedChannel)
        Called after a gRPC call to clean up the channel. No-op by default, since gRPC channels are designed to remain ready for reuse. Override this if using a stub that requires channels be reset or cleared between uses.
        Parameters:
        managedChannel - the gRPC channel to clean up
      • invokeGrpc

        protected <Q extends com.google.protobuf.GeneratedMessageV3,​R extends com.google.protobuf.GeneratedMessageV3,​S extends io.grpc.stub.AbstractBlockingStub<S>> R invokeGrpc​(String targetId,
                                                                                                                                                                                              Function<io.grpc.ManagedChannel,​S> stubFactory,
                                                                                                                                                                                              BiFunction<S,​Q,​R> callLogic,
                                                                                                                                                                                              Q request)
        Executes a unary gRPC call to a given endpoint using a BlockingStub. If the gRPC connection fails due to a PoolException or a ServiceNotAvailableException, the call will be tried again per the configurations set using RetryHandler. All other Exceptions are thrown on the spot. Will also throw an Exception once max attempts have been reached.
        Type Parameters:
        Q - the protobuf request type
        R - the protobuf response type
        S - the gRPC stub type
        Parameters:
        targetId - the identifier used in the configs for the given gRPC endpoint
        stubFactory - function that creates the appropriate gRPC stub from a ManagedChannel
        callLogic - function that performs the actual gRPC call using the stub and request
        request - the protobuf request message to send
        Returns:
        the response returned by the gRPC call
      • invokeBatchedGrpc

        protected <Q extends com.google.protobuf.GeneratedMessageV3,​R extends com.google.protobuf.GeneratedMessageV3,​S extends io.grpc.stub.AbstractFutureStub<S>> List<R> invokeBatchedGrpc​(String targetId,
                                                                                                                                                                                                         Function<io.grpc.ManagedChannel,​S> stubFactory,
                                                                                                                                                                                                         BiFunction<S,​Q,​com.google.common.util.concurrent.ListenableFuture<R>> callLogic,
                                                                                                                                                                                                         List<Q> requestList)
        Executes multiple unary gRPC calls to a given endpoint in parallel using a shared AbstractFutureStub.

        TODO: Determine channel handling strategy when some calls succeed and others fail
        TODO: Clarify expected blocking behavior for response collection

        Type Parameters:
        Q - the protobuf request type
        R - the protobuf response type
        S - the gRPC stub type
        Parameters:
        targetId - the identifier used in the configs for the given gRPC endpoint
        stubFactory - function that creates the appropriate FutureStub from a ManagedChannel
        callLogic - function that maps a stub and request to a ListenableFuture
        requestList - list of protobuf request messages to be sent
        Returns:
        list of gRPC responses in the same order as requestList
      • getHostname

        public String getHostname​(String targetId)
      • getPortNumber

        public int getPortNumber​(String targetId)
      • tableLookup

        protected <T> T tableLookup​(Map<String,​T> table,
                                    String targetId)