Class GrpcUtil


  • public final class GrpcUtil
    extends java.lang.Object
    Common utilities for GRPC.
    • Field Detail

      • log

        private static final java.util.logging.Logger log
      • INAPPROPRIATE_CONTROL_PLANE_STATUS

        private static final java.util.Set<Status.Code> INAPPROPRIATE_CONTROL_PLANE_STATUS
      • US_ASCII

        public static final java.nio.charset.Charset US_ASCII
      • MESSAGE_ENCODING_KEY

        public static final Metadata.Key<java.lang.String> MESSAGE_ENCODING_KEY
        Metadata.Key for the message encoding header.
      • MESSAGE_ACCEPT_ENCODING_KEY

        public static final Metadata.Key<byte[]> MESSAGE_ACCEPT_ENCODING_KEY
        Metadata.Key for the accepted message encodings header.
      • CONTENT_ENCODING_KEY

        public static final Metadata.Key<java.lang.String> CONTENT_ENCODING_KEY
        Metadata.Key for the stream's content encoding header.
      • CONTENT_ACCEPT_ENCODING_KEY

        public static final Metadata.Key<byte[]> CONTENT_ACCEPT_ENCODING_KEY
        Metadata.Key for the stream's accepted content encoding header.
      • CONTENT_LENGTH_KEY

        static final Metadata.Key<java.lang.String> CONTENT_LENGTH_KEY
      • CONTENT_TYPE_KEY

        public static final Metadata.Key<java.lang.String> CONTENT_TYPE_KEY
        Metadata.Key for the Content-Type request/response header.
      • USER_AGENT_KEY

        public static final Metadata.Key<java.lang.String> USER_AGENT_KEY
        Metadata.Key for the Content-Type request/response header.
      • DEFAULT_PORT_PLAINTEXT

        public static final int DEFAULT_PORT_PLAINTEXT
        The default port for plain-text connections.
        See Also:
        Constant Field Values
      • DEFAULT_PORT_SSL

        public static final int DEFAULT_PORT_SSL
        The default port for SSL connections.
        See Also:
        Constant Field Values
      • CONTENT_TYPE_GRPC

        public static final java.lang.String CONTENT_TYPE_GRPC
        Content-Type used for GRPC-over-HTTP/2.
        See Also:
        Constant Field Values
      • HTTP_METHOD

        public static final java.lang.String HTTP_METHOD
        The HTTP method used for GRPC requests.
        See Also:
        Constant Field Values
      • TE_TRAILERS

        public static final java.lang.String TE_TRAILERS
        The TE (transport encoding) header for requests over HTTP/2.
        See Also:
        Constant Field Values
      • TIMEOUT

        public static final java.lang.String TIMEOUT
        The Timeout header name.
        See Also:
        Constant Field Values
      • MESSAGE_ENCODING

        public static final java.lang.String MESSAGE_ENCODING
        The message encoding (i.e. compression) that can be used in the stream.
        See Also:
        Constant Field Values
      • MESSAGE_ACCEPT_ENCODING

        public static final java.lang.String MESSAGE_ACCEPT_ENCODING
        The accepted message encodings (i.e. compression) that can be used in the stream.
        See Also:
        Constant Field Values
      • CONTENT_ENCODING

        public static final java.lang.String CONTENT_ENCODING
        The content-encoding used to compress the full gRPC stream.
        See Also:
        Constant Field Values
      • CONTENT_ACCEPT_ENCODING

        public static final java.lang.String CONTENT_ACCEPT_ENCODING
        The accepted content-encodings that can be used to compress the full gRPC stream.
        See Also:
        Constant Field Values
      • DEFAULT_MAX_MESSAGE_SIZE

        public static final int DEFAULT_MAX_MESSAGE_SIZE
        The default maximum uncompressed size (in bytes) for inbound messages. Defaults to 4 MiB.
        See Also:
        Constant Field Values
      • DEFAULT_MAX_HEADER_LIST_SIZE

        public static final int DEFAULT_MAX_HEADER_LIST_SIZE
        The default maximum size (in bytes) for inbound header/trailer.
        See Also:
        Constant Field Values
      • ACCEPT_ENCODING_SPLITTER

        public static final com.google.common.base.Splitter ACCEPT_ENCODING_SPLITTER
      • IMPLEMENTATION_VERSION

        public static final java.lang.String IMPLEMENTATION_VERSION
        See Also:
        Constant Field Values
      • DEFAULT_KEEPALIVE_TIMEOUT_NANOS

        public static final long DEFAULT_KEEPALIVE_TIMEOUT_NANOS
        The default timeout in nanos for a keepalive ping request.
      • KEEPALIVE_TIME_NANOS_DISABLED

        public static final long KEEPALIVE_TIME_NANOS_DISABLED
        The magic keepalive time value that disables client keepalive.
        See Also:
        Constant Field Values
      • DEFAULT_SERVER_KEEPALIVE_TIME_NANOS

        public static final long DEFAULT_SERVER_KEEPALIVE_TIME_NANOS
        The default delay in nanos for server keepalive.
      • DEFAULT_SERVER_KEEPALIVE_TIMEOUT_NANOS

        public static final long DEFAULT_SERVER_KEEPALIVE_TIMEOUT_NANOS
        The default timeout in nanos for a server keepalive ping request.
      • SERVER_KEEPALIVE_TIME_NANOS_DISABLED

        public static final long SERVER_KEEPALIVE_TIME_NANOS_DISABLED
        The magic keepalive time value that disables keepalive.
        See Also:
        Constant Field Values
      • DEFAULT_PROXY_DETECTOR

        public static final ProxyDetector DEFAULT_PROXY_DETECTOR
        The default proxy detector.
      • NOOP_PROXY_DETECTOR

        public static final ProxyDetector NOOP_PROXY_DETECTOR
        A proxy detector that always claims no proxy is needed.
      • DEFAULT_LB_POLICY

        public static final java.lang.String DEFAULT_LB_POLICY
        The very default load-balancing policy.
        See Also:
        Constant Field Values
      • CALL_OPTIONS_RPC_OWNED_BY_BALANCER

        public static final CallOptions.Key<java.lang.Boolean> CALL_OPTIONS_RPC_OWNED_BY_BALANCER
        RPCs created on the Channel returned by LoadBalancer.Subchannel.asChannel() will have this option with value true. They will be treated differently from the ones created by application.
      • SHARED_CHANNEL_EXECUTOR

        public static final SharedResourceHolder.Resource<java.util.concurrent.Executor> SHARED_CHANNEL_EXECUTOR
        Shared executor for channels.
      • TIMER_SERVICE

        public static final SharedResourceHolder.Resource<java.util.concurrent.ScheduledExecutorService> TIMER_SERVICE
        Shared single-threaded executor for managing channel timers.
      • STOPWATCH_SUPPLIER

        public static final com.google.common.base.Supplier<com.google.common.base.Stopwatch> STOPWATCH_SUPPLIER
        The factory of default Stopwatches.
    • Constructor Detail

      • GrpcUtil

        private GrpcUtil()
    • Method Detail

      • shouldBeCountedForInUse

        public static boolean shouldBeCountedForInUse​(CallOptions callOptions)
        Returns true if an RPC with the given properties should be counted when calculating the in-use state of a transport.
      • httpStatusToGrpcStatus

        public static Status httpStatusToGrpcStatus​(int httpStatusCode)
        Maps HTTP error response status codes to transport codes, as defined in http-grpc-status-mapping.md. Never returns a status for which status.isOk() is true.
      • httpStatusToGrpcCode

        private static Status.Code httpStatusToGrpcCode​(int httpStatusCode)
      • isGrpcContentType

        public static boolean isGrpcContentType​(java.lang.String contentType)
        Indicates whether or not the given value is a valid gRPC content-type.
      • getGrpcUserAgent

        public static java.lang.String getGrpcUserAgent​(java.lang.String transportName,
                                                        @Nullable
                                                        java.lang.String applicationUserAgent)
        Gets the User-Agent string for the gRPC transport.
      • authorityToUri

        public static java.net.URI authorityToUri​(java.lang.String authority)
        Parse an authority into a URI for retrieving the host and port.
      • checkAuthority

        public static java.lang.String checkAuthority​(java.lang.String authority)
        Verify authority is valid for use with gRPC. The syntax must be valid and it must not include userinfo.
        Returns:
        the authority provided
      • authorityFromHostAndPort

        public static java.lang.String authorityFromHostAndPort​(java.lang.String host,
                                                                int port)
        Combine a host and port into an authority string.
      • getThreadFactory

        public static java.util.concurrent.ThreadFactory getThreadFactory​(java.lang.String nameFormat,
                                                                          boolean daemon)
        Get a ThreadFactory suitable for use in the current environment.
        Parameters:
        nameFormat - to apply to threads created by the factory.
        daemon - true if the threads the factory creates are daemon threads, false otherwise.
        Returns:
        a ThreadFactory.
      • getTransportFromPickResult

        @Nullable
        static ClientTransport getTransportFromPickResult​(LoadBalancer.PickResult result,
                                                          boolean isWaitForReady)
        Returns a transport out of a PickResult, or null if the result is "buffer".
      • getClientStreamTracers

        public static ClientStreamTracer[] getClientStreamTracers​(CallOptions callOptions,
                                                                  Metadata headers,
                                                                  int previousAttempts,
                                                                  boolean isTransparentRetry)
        Gets stream tracers based on CallOptions.
      • closeQuietly

        public static void closeQuietly​(@Nullable
                                        java.io.Closeable message)
        Closes a Closeable, ignoring IOExceptions. This method exists because Guava's Closeables.closeQuietly() is beta.
      • exhaust

        public static void exhaust​(java.io.InputStream in)
                            throws java.io.IOException
        Reads in until end of stream.
        Throws:
        java.io.IOException
      • replaceInappropriateControlPlaneStatus

        public static Status replaceInappropriateControlPlaneStatus​(Status status)
        Some status codes from the control plane are not appropritate to use in the data plane. If one is given it will be replaced with INTERNAL, indicating a bug in the control plane implementation.
      • iterableContains

        static <T> boolean iterableContains​(java.lang.Iterable<T> iterable,
                                            T item)
        Checks whether the given item exists in the iterable. This is copied from Guava Collect's Iterables.contains() because Guava Collect is not Android-friendly thus core can't depend on it.
      • getFlag

        public static boolean getFlag​(java.lang.String envVarName,
                                      boolean enableByDefault)