Package io.grpc.internal
Class GrpcUtil
- java.lang.Object
-
- io.grpc.internal.GrpcUtil
-
public final class GrpcUtil extends java.lang.ObjectCommon utilities for GRPC.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classGrpcUtil.AcceptEncodingMarshallerstatic classGrpcUtil.AuthorityEscaperPercent encode theauthoritybased on https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.static classGrpcUtil.GrpcBuildVersionstatic classGrpcUtil.Http2ErrorAll error codes identified by the HTTP/2 spec.(package private) static classGrpcUtil.TimeoutMarshallerMarshals a nanoseconds representation of the timeout to and from a string representation, consisting of an ASCII decimal representation of a number with at most 8 digits, followed by a unit.
-
Field Summary
Fields Modifier and Type Field Description static com.google.common.base.SplitterACCEPT_ENCODING_SPLITTERstatic CallOptions.Key<java.lang.Boolean>CALL_OPTIONS_RPC_OWNED_BY_BALANCERRPCs created on the Channel returned byLoadBalancer.Subchannel.asChannel()will have this option with valuetrue.static java.lang.StringCONTENT_ACCEPT_ENCODINGThe accepted content-encodings that can be used to compress the full gRPC stream.static Metadata.Key<byte[]>CONTENT_ACCEPT_ENCODING_KEYMetadata.Keyfor the stream's accepted content encoding header.static java.lang.StringCONTENT_ENCODINGThe content-encoding used to compress the full gRPC stream.static Metadata.Key<java.lang.String>CONTENT_ENCODING_KEYMetadata.Keyfor the stream's content encoding header.(package private) static Metadata.Key<java.lang.String>CONTENT_LENGTH_KEYstatic java.lang.StringCONTENT_TYPE_GRPCContent-Type used for GRPC-over-HTTP/2.static Metadata.Key<java.lang.String>CONTENT_TYPE_KEYMetadata.Keyfor the Content-Type request/response header.static longDEFAULT_KEEPALIVE_TIMEOUT_NANOSThe default timeout in nanos for a keepalive ping request.static java.lang.StringDEFAULT_LB_POLICYThe very default load-balancing policy.static intDEFAULT_MAX_HEADER_LIST_SIZEThe default maximum size (in bytes) for inbound header/trailer.static intDEFAULT_MAX_MESSAGE_SIZEThe default maximum uncompressed size (in bytes) for inbound messages.static intDEFAULT_PORT_PLAINTEXTThe default port for plain-text connections.static intDEFAULT_PORT_SSLThe default port for SSL connections.static ProxyDetectorDEFAULT_PROXY_DETECTORThe default proxy detector.static longDEFAULT_SERVER_KEEPALIVE_TIME_NANOSThe default delay in nanos for server keepalive.static longDEFAULT_SERVER_KEEPALIVE_TIMEOUT_NANOSThe default timeout in nanos for a server keepalive ping request.static java.lang.StringHTTP_METHODThe HTTP method used for GRPC requests.static java.lang.StringIMPLEMENTATION_VERSIONprivate static java.util.Set<Status.Code>INAPPROPRIATE_CONTROL_PLANE_STATUSstatic longKEEPALIVE_TIME_NANOS_DISABLEDThe magic keepalive time value that disables client keepalive.private static java.util.logging.Loggerlogstatic java.lang.StringMESSAGE_ACCEPT_ENCODINGThe accepted message encodings (i.e.static Metadata.Key<byte[]>MESSAGE_ACCEPT_ENCODING_KEYMetadata.Keyfor the accepted message encodings header.static java.lang.StringMESSAGE_ENCODINGThe message encoding (i.e.static Metadata.Key<java.lang.String>MESSAGE_ENCODING_KEYMetadata.Keyfor the message encoding header.static ProxyDetectorNOOP_PROXY_DETECTORA proxy detector that always claims no proxy is needed.private static ClientStreamTracerNOOP_TRACERstatic longSERVER_KEEPALIVE_TIME_NANOS_DISABLEDThe magic keepalive time value that disables keepalive.static SharedResourceHolder.Resource<java.util.concurrent.Executor>SHARED_CHANNEL_EXECUTORShared executor for channels.static com.google.common.base.Supplier<com.google.common.base.Stopwatch>STOPWATCH_SUPPLIERThe factory of default Stopwatches.static Metadata.Key<java.lang.String>TE_HEADERMetadata.Keyfor the Transfer encoding.static java.lang.StringTE_TRAILERSThe TE (transport encoding) header for requests over HTTP/2.static java.lang.StringTIMEOUTThe Timeout header name.static Metadata.Key<java.lang.Long>TIMEOUT_KEYMetadata.Keyfor the timeout header.static SharedResourceHolder.Resource<java.util.concurrent.ScheduledExecutorService>TIMER_SERVICEShared single-threaded executor for managing channel timers.static java.nio.charset.CharsetUS_ASCIIstatic Metadata.Key<java.lang.String>USER_AGENT_KEYMetadata.Keyfor the Content-Type request/response header.
-
Constructor Summary
Constructors Modifier Constructor Description privateGrpcUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringauthorityFromHostAndPort(java.lang.String host, int port)Combine a host and port into an authority string.static java.net.URIauthorityToUri(java.lang.String authority)Parse an authority into a URI for retrieving the host and port.static java.lang.StringcheckAuthority(java.lang.String authority)Verifyauthorityis valid for use with gRPC.(package private) static voidcloseQuietly(StreamListener.MessageProducer producer)Quietly closes all messages in MessageProducer.static voidcloseQuietly(java.io.Closeable message)Closes a Closeable, ignoring IOExceptions.static voidexhaust(java.io.InputStream in)Readsinuntil end of stream.static ClientStreamTracer[]getClientStreamTracers(CallOptions callOptions, Metadata headers, int previousAttempts, boolean isTransparentRetry)Gets stream tracers based on CallOptions.static booleangetFlag(java.lang.String envVarName, boolean enableByDefault)static GrpcUtil.GrpcBuildVersiongetGrpcBuildVersion()Returns the build version of gRPC.static java.lang.StringgetGrpcUserAgent(java.lang.String transportName, java.lang.String applicationUserAgent)Gets the User-Agent string for the gRPC transport.static java.util.concurrent.ThreadFactorygetThreadFactory(java.lang.String nameFormat, boolean daemon)Get aThreadFactorysuitable for use in the current environment.(package private) static ClientTransportgetTransportFromPickResult(LoadBalancer.PickResult result, boolean isWaitForReady)Returns a transport out of a PickResult, ornullif the result is "buffer".private static Status.CodehttpStatusToGrpcCode(int httpStatusCode)static StatushttpStatusToGrpcStatus(int httpStatusCode)Maps HTTP error response status codes to transport codes, as defined in http-grpc-status-mapping.md.static booleanisGrpcContentType(java.lang.String contentType)Indicates whether or not the given value is a valid gRPC content-type.(package private) static <T> booleaniterableContains(java.lang.Iterable<T> iterable, T item)Checks whether the given item exists in the iterable.static StatusreplaceInappropriateControlPlaneStatus(Status status)Some status codes from the control plane are not appropritate to use in the data plane.static booleanshouldBeCountedForInUse(CallOptions callOptions)Returns true if an RPC with the given properties should be counted when calculating the in-use state of a transport.
-
-
-
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
-
TIMEOUT_KEY
public static final Metadata.Key<java.lang.Long> TIMEOUT_KEY
Metadata.Keyfor the timeout header.
-
MESSAGE_ENCODING_KEY
public static final Metadata.Key<java.lang.String> MESSAGE_ENCODING_KEY
Metadata.Keyfor the message encoding header.
-
MESSAGE_ACCEPT_ENCODING_KEY
public static final Metadata.Key<byte[]> MESSAGE_ACCEPT_ENCODING_KEY
Metadata.Keyfor the accepted message encodings header.
-
CONTENT_ENCODING_KEY
public static final Metadata.Key<java.lang.String> CONTENT_ENCODING_KEY
Metadata.Keyfor the stream's content encoding header.
-
CONTENT_ACCEPT_ENCODING_KEY
public static final Metadata.Key<byte[]> CONTENT_ACCEPT_ENCODING_KEY
Metadata.Keyfor 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.Keyfor the Content-Type request/response header.
-
TE_HEADER
public static final Metadata.Key<java.lang.String> TE_HEADER
Metadata.Keyfor the Transfer encoding.
-
USER_AGENT_KEY
public static final Metadata.Key<java.lang.String> USER_AGENT_KEY
Metadata.Keyfor 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 byLoadBalancer.Subchannel.asChannel()will have this option with valuetrue. They will be treated differently from the ones created by application.
-
NOOP_TRACER
private static final ClientStreamTracer NOOP_TRACER
-
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.
-
-
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 whichstatus.isOk()istrue.
-
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.
-
getGrpcBuildVersion
public static GrpcUtil.GrpcBuildVersion getGrpcBuildVersion()
Returns the build version of gRPC.
-
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)
Verifyauthorityis valid for use with gRPC. The syntax must be valid and it must not include userinfo.- Returns:
- the
authorityprovided
-
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 aThreadFactorysuitable for use in the current environment.- Parameters:
nameFormat- to apply to threads created by the factory.daemon-trueif the threads the factory creates are daemon threads,falseotherwise.- Returns:
- a
ThreadFactory.
-
getTransportFromPickResult
@Nullable static ClientTransport getTransportFromPickResult(LoadBalancer.PickResult result, boolean isWaitForReady)
Returns a transport out of a PickResult, ornullif the result is "buffer".
-
getClientStreamTracers
public static ClientStreamTracer[] getClientStreamTracers(CallOptions callOptions, Metadata headers, int previousAttempts, boolean isTransparentRetry)
Gets stream tracers based on CallOptions.
-
closeQuietly
static void closeQuietly(StreamListener.MessageProducer producer)
Quietly closes all messages in MessageProducer.
-
closeQuietly
public static void closeQuietly(@Nullable java.io.Closeable message)Closes a Closeable, ignoring IOExceptions. This method exists because Guava'sCloseables.closeQuietly()is beta.
-
exhaust
public static void exhaust(java.io.InputStream in) throws java.io.IOExceptionReadsinuntil 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'sIterables.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)
-
-