Package io.grpc.internal
Class GrpcUtil
- java.lang.Object
-
- io.grpc.internal.GrpcUtil
-
public final class GrpcUtil extends java.lang.Object
Common utilities for GRPC.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
GrpcUtil.AcceptEncodingMarshaller
static class
GrpcUtil.AuthorityEscaper
Percent encode theauthority
based on https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.static class
GrpcUtil.GrpcBuildVersion
static class
GrpcUtil.Http2Error
All error codes identified by the HTTP/2 spec.(package private) static class
GrpcUtil.TimeoutMarshaller
Marshals 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.Splitter
ACCEPT_ENCODING_SPLITTER
static 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
.static java.lang.String
CONTENT_ACCEPT_ENCODING
The accepted content-encodings that can be used to compress the full gRPC stream.static Metadata.Key<byte[]>
CONTENT_ACCEPT_ENCODING_KEY
Metadata.Key
for the stream's accepted content encoding header.static java.lang.String
CONTENT_ENCODING
The content-encoding used to compress the full gRPC stream.static Metadata.Key<java.lang.String>
CONTENT_ENCODING_KEY
Metadata.Key
for the stream's content encoding header.(package private) static Metadata.Key<java.lang.String>
CONTENT_LENGTH_KEY
static java.lang.String
CONTENT_TYPE_GRPC
Content-Type used for GRPC-over-HTTP/2.static Metadata.Key<java.lang.String>
CONTENT_TYPE_KEY
Metadata.Key
for the Content-Type request/response header.static long
DEFAULT_KEEPALIVE_TIMEOUT_NANOS
The default timeout in nanos for a keepalive ping request.static java.lang.String
DEFAULT_LB_POLICY
The very default load-balancing policy.static int
DEFAULT_MAX_HEADER_LIST_SIZE
The default maximum size (in bytes) for inbound header/trailer.static int
DEFAULT_MAX_MESSAGE_SIZE
The default maximum uncompressed size (in bytes) for inbound messages.static int
DEFAULT_PORT_PLAINTEXT
The default port for plain-text connections.static int
DEFAULT_PORT_SSL
The default port for SSL connections.static ProxyDetector
DEFAULT_PROXY_DETECTOR
The default proxy detector.static long
DEFAULT_SERVER_KEEPALIVE_TIME_NANOS
The default delay in nanos for server keepalive.static long
DEFAULT_SERVER_KEEPALIVE_TIMEOUT_NANOS
The default timeout in nanos for a server keepalive ping request.static java.lang.String
HTTP_METHOD
The HTTP method used for GRPC requests.static java.lang.String
IMPLEMENTATION_VERSION
private static java.util.Set<Status.Code>
INAPPROPRIATE_CONTROL_PLANE_STATUS
static long
KEEPALIVE_TIME_NANOS_DISABLED
The magic keepalive time value that disables client keepalive.private static java.util.logging.Logger
log
static java.lang.String
MESSAGE_ACCEPT_ENCODING
The accepted message encodings (i.e.static Metadata.Key<byte[]>
MESSAGE_ACCEPT_ENCODING_KEY
Metadata.Key
for the accepted message encodings header.static java.lang.String
MESSAGE_ENCODING
The message encoding (i.e.static Metadata.Key<java.lang.String>
MESSAGE_ENCODING_KEY
Metadata.Key
for the message encoding header.static ProxyDetector
NOOP_PROXY_DETECTOR
A proxy detector that always claims no proxy is needed.private static ClientStreamTracer
NOOP_TRACER
static long
SERVER_KEEPALIVE_TIME_NANOS_DISABLED
The magic keepalive time value that disables keepalive.static SharedResourceHolder.Resource<java.util.concurrent.Executor>
SHARED_CHANNEL_EXECUTOR
Shared executor for channels.static com.google.common.base.Supplier<com.google.common.base.Stopwatch>
STOPWATCH_SUPPLIER
The factory of default Stopwatches.static Metadata.Key<java.lang.String>
TE_HEADER
Metadata.Key
for the Transfer encoding.static java.lang.String
TE_TRAILERS
The TE (transport encoding) header for requests over HTTP/2.static java.lang.String
TIMEOUT
The Timeout header name.static Metadata.Key<java.lang.Long>
TIMEOUT_KEY
Metadata.Key
for the timeout header.static SharedResourceHolder.Resource<java.util.concurrent.ScheduledExecutorService>
TIMER_SERVICE
Shared single-threaded executor for managing channel timers.static java.nio.charset.Charset
US_ASCII
static Metadata.Key<java.lang.String>
USER_AGENT_KEY
Metadata.Key
for the Content-Type request/response header.
-
Constructor Summary
Constructors Modifier Constructor Description private
GrpcUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
authorityFromHostAndPort(java.lang.String host, int port)
Combine a host and port into an authority string.static java.net.URI
authorityToUri(java.lang.String authority)
Parse an authority into a URI for retrieving the host and port.static java.lang.String
checkAuthority(java.lang.String authority)
Verifyauthority
is valid for use with gRPC.(package private) static void
closeQuietly(StreamListener.MessageProducer producer)
Quietly closes all messages in MessageProducer.static void
closeQuietly(java.io.Closeable message)
Closes a Closeable, ignoring IOExceptions.static void
exhaust(java.io.InputStream in)
Readsin
until end of stream.static ClientStreamTracer[]
getClientStreamTracers(CallOptions callOptions, Metadata headers, int previousAttempts, boolean isTransparentRetry)
Gets stream tracers based on CallOptions.static boolean
getFlag(java.lang.String envVarName, boolean enableByDefault)
static GrpcUtil.GrpcBuildVersion
getGrpcBuildVersion()
Returns the build version of gRPC.static java.lang.String
getGrpcUserAgent(java.lang.String transportName, java.lang.String applicationUserAgent)
Gets the User-Agent string for the gRPC transport.static java.util.concurrent.ThreadFactory
getThreadFactory(java.lang.String nameFormat, boolean daemon)
Get aThreadFactory
suitable for use in the current environment.(package private) static ClientTransport
getTransportFromPickResult(LoadBalancer.PickResult result, boolean isWaitForReady)
Returns a transport out of a PickResult, ornull
if the result is "buffer".private static Status.Code
httpStatusToGrpcCode(int httpStatusCode)
static Status
httpStatusToGrpcStatus(int httpStatusCode)
Maps HTTP error response status codes to transport codes, as defined in http-grpc-status-mapping.md.static boolean
isGrpcContentType(java.lang.String contentType)
Indicates whether or not the given value is a valid gRPC content-type.(package private) static <T> boolean
iterableContains(java.lang.Iterable<T> iterable, T item)
Checks whether the given item exists in the iterable.static Status
replaceInappropriateControlPlaneStatus(Status status)
Some status codes from the control plane are not appropritate to use in the data plane.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.
-
-
-
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.Key
for the timeout header.
-
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.
-
TE_HEADER
public static final Metadata.Key<java.lang.String> TE_HEADER
Metadata.Key
for the Transfer encoding.
-
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 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)
Verifyauthority
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 aThreadFactory
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, ornull
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
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.IOException
Readsin
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'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)
-
-