Package com.linecorp.armeria.client
Class DefaultClientRequestContext
java.lang.Object
com.linecorp.armeria.common.NonWrappingRequestContext
com.linecorp.armeria.client.DefaultClientRequestContext
- All Implemented Interfaces:
ClientRequestContext
,RequestContext
@UnstableApi
public final class DefaultClientRequestContext
extends NonWrappingRequestContext
implements ClientRequestContext
Default
ClientRequestContext
implementation.-
Constructor Summary
ConstructorDescriptionDefaultClientRequestContext(MeterRegistry meterRegistry, SessionProtocol sessionProtocol, RequestId id, HttpMethod method, String path, @Nullable String query, @Nullable String fragment, ClientOptions options, @Nullable HttpRequest req, @Nullable RpcRequest rpcReq, RequestOptions requestOptions, long requestStartTimeNanos, long requestStartTimeMicros, boolean hasBaseUri)
Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAdditionalRequestHeader(CharSequence name, Object value)
Adds a header with the specifiedname
andvalue
.alloc()
Returns theByteBufAllocator
for thisRequestContext
.void
Returns the cause of cancellation,null
if the request has not been cancelled.channel()
Returns theChannel
that is handling this request, ornull
if the connection is not established yet.void
Clears the previously scheduled response timeout, if any.endpoint()
Returns theEndpointGroup
used for the currentRequest
.Returns theContextAwareEventLoop
that is handling the currentRequest
.void
finishInitialization(boolean success)
Completes thewhenInitialized()
with the specified value.fragment()
Returns the fragment part of the URI of the currentRequest
, as defined in the section 3.5 of RFC3986.init(EndpointGroup endpointGroup)
Initializes this context with the specifiedEndpointGroup
.log()
Returns theRequestLogAccess
that provides the access to theRequestLog
, which contains the information collected while processing the currentRequest
.Returns theRequestLogBuilder
that collects the information about the currentRequest
.long
Returns the maximum length of the receivedResponse
.void
newDerivedContext(RequestId id, @Nullable HttpRequest req, @Nullable RpcRequest rpcReq, @Nullable Endpoint endpoint)
Creates a newClientRequestContext
whose properties andAttribute
s are copied from thisClientRequestContext
, except having differentRequest
,Endpoint
and its ownRequestLog
.options()
Returns theClientOptions
of the currentRequest
.long
root()
Returns the rootServiceRequestContext
of this context.void
setAdditionalRequestHeader(CharSequence name, Object value)
Sets a header with the specifiedname
andvalue
.void
setMaxResponseLength(long maxResponseLength)
Sets the maximum length of the receivedResponse
.void
setResponseTimeout(TimeoutMode mode, Duration responseTimeout)
Schedules the response timeout that is triggered when theResponse
is not fully received within the specifiedTimeoutMode
and the specifiedresponseTimeoutMillis
since theResponse
started orRequest
was fully sent.void
setResponseTimeoutMillis(TimeoutMode mode, long responseTimeoutMillis)
Schedules the response timeout that is triggered when theResponse
is not fully received within the specifiedTimeoutMode
and the specifiedresponseTimeoutMillis
since theResponse
started orRequest
was fully sent.void
setWriteTimeout(Duration writeTimeout)
Sets the amount of time allowed until the initial write attempt of the currentRequest
succeeds.void
setWriteTimeoutMillis(long writeTimeoutMillis)
Sets the amount of time allowed until the initial write attempt of the currentRequest
succeeds.TheSSLSession
for this request if the connection is made over TLS, ornull
if the connection is not established yet or the connection is not a TLS connection.toString()
protected void
validateHeaders(RequestHeaders headers)
Validates the specifiedRequestHeaders
.Returns aCompletableFuture
that will be completed if thisClientRequestContext
is initialized with anEndpointGroup
.Returns aCompletableFuture
which is completed with aThrowable
cancellation cause after theClientRequestContext
has been cancelled.Returns aCompletableFuture
which is completed with aThrowable
cancellation cause when theClientRequestContext
is about to get cancelled.Deprecated.Deprecated.long
Returns the amount of time allowed until the initial write attempt of the currentRequest
succeeds.Methods inherited from class com.linecorp.armeria.common.NonWrappingRequestContext
attr, attrs, decodedPath, hook, hook, id, localAddress, meterRegistry, method, ownAttr, ownAttrs, path, query, remoteAddress, request, rpcRequest, sessionProtocol, setAttr, unsafeUpdateRequest, updateRequest, updateRpcRequest
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.linecorp.armeria.client.ClientRequestContext
cancel, push, request, rpcRequest, setResponseTimeout, setResponseTimeoutMillis, timeoutNow
Methods inherited from interface com.linecorp.armeria.common.RequestContext
attr, attrs, decodedPath, hasAttr, hasOwnAttr, id, isCancelled, isTimedOut, localAddress, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, meterRegistry, method, ownAttr, ownAttrs, path, query, remoteAddress, replace, run, run, sessionProtocol, setAttr, updateRequest, updateRpcRequest
-
Constructor Details
-
DefaultClientRequestContext
public DefaultClientRequestContext(MeterRegistry meterRegistry, SessionProtocol sessionProtocol, RequestId id, HttpMethod method, String path, @Nullable @Nullable String query, @Nullable @Nullable String fragment, ClientOptions options, @Nullable @Nullable HttpRequest req, @Nullable @Nullable RpcRequest rpcReq, RequestOptions requestOptions, long requestStartTimeNanos, long requestStartTimeMicros, boolean hasBaseUri)Creates a new instance. Note thatinit(EndpointGroup)
method must be invoked to finish the construction of this context.- Parameters:
sessionProtocol
- theSessionProtocol
of the invocationid
- theRequestId
that contains the identifier of the currentRequest
andResponse
pair.req
- theHttpRequest
associated with this contextrpcReq
- theRpcRequest
associated with this contextrequestStartTimeNanos
-System.nanoTime()
value when the request started.requestStartTimeMicros
- the number of microseconds since the epoch, e.g.System.currentTimeMillis() * 1000
.hasBaseUri
- whether aClient
which initiates thisClientRequestContext
was created with a baseURI
.
-
-
Method Details
-
init
Initializes this context with the specifiedEndpointGroup
. This method must be invoked to finish the construction of this context.- Returns:
true
if the initialization has succeeded.false
if the initialization has failed and this context'sRequestLog
has been completed with the cause of the failure.
-
whenInitialized
Returns aCompletableFuture
that will be completed if thisClientRequestContext
is initialized with anEndpointGroup
.- See Also:
init(EndpointGroup)
-
finishInitialization
public void finishInitialization(boolean success)Completes thewhenInitialized()
with the specified value. -
root
Description copied from interface:RequestContext
Returns the rootServiceRequestContext
of this context.- Specified by:
root
in interfaceRequestContext
- Returns:
- the root
ServiceRequestContext
, ornull
if this context was not created in the context of a server request.
-
newDerivedContext
public ClientRequestContext newDerivedContext(RequestId id, @Nullable @Nullable HttpRequest req, @Nullable @Nullable RpcRequest rpcReq, @Nullable @Nullable Endpoint endpoint)Description copied from interface:ClientRequestContext
Creates a newClientRequestContext
whose properties andAttribute
s are copied from thisClientRequestContext
, except having differentRequest
,Endpoint
and its ownRequestLog
.Note that this method does not copy the
RequestLog
properties to the derived context.- Specified by:
newDerivedContext
in interfaceClientRequestContext
-
validateHeaders
Description copied from class:NonWrappingRequestContext
Validates the specifiedRequestHeaders
. By default, this method will raise anIllegalArgumentException
if it does not have":scheme"
or":authority"
header.- Overrides:
validateHeaders
in classNonWrappingRequestContext
-
channel
Description copied from class:NonWrappingRequestContext
Returns theChannel
that is handling this request, ornull
if the connection is not established yet.- Specified by:
channel
in classNonWrappingRequestContext
-
eventLoop
Description copied from interface:RequestContext
Returns theContextAwareEventLoop
that is handling the currentRequest
. TheContextAwareEventLoop
sets thisRequestContext
as the current context before executing any submitted tasks. If you want to useEventLoop
without setting this context, callContextAwareEventLoop.withoutContext()
and use the returnedEventLoop
.- Specified by:
eventLoop
in interfaceRequestContext
-
alloc
Description copied from interface:RequestContext
Returns theByteBufAllocator
for thisRequestContext
. Any buffers created by thisByteBufAllocator
must be reference-counted. If you don't know what this means, you should probably usebyte[]
orByteBuffer
directly instead of calling this method.- Specified by:
alloc
in interfaceRequestContext
-
sslSession
Description copied from interface:RequestContext
TheSSLSession
for this request if the connection is made over TLS, ornull
if the connection is not established yet or the connection is not a TLS connection.- Specified by:
sslSession
in interfaceRequestContext
-
options
Description copied from interface:ClientRequestContext
Returns theClientOptions
of the currentRequest
.- Specified by:
options
in interfaceClientRequestContext
-
endpointGroup
Description copied from interface:ClientRequestContext
Returns theEndpointGroup
used for the currentRequest
.- Specified by:
endpointGroup
in interfaceClientRequestContext
- Returns:
- the
EndpointGroup
if a user specified anEndpointGroup
when initiating aRequest
.null
if a user specified anEndpoint
.
-
endpoint
Description copied from interface:ClientRequestContext
- Specified by:
endpoint
in interfaceClientRequestContext
- Returns:
- the remote
Endpoint
, ornull
if theRequest
has failed because its remoteEndpoint
couldn't be determined.
-
fragment
Description copied from interface:ClientRequestContext
Returns the fragment part of the URI of the currentRequest
, as defined in the section 3.5 of RFC3986.- Specified by:
fragment
in interfaceClientRequestContext
- Returns:
- the fragment part of the request URI, or
null
if no fragment was specified
-
writeTimeoutMillis
public long writeTimeoutMillis()Description copied from interface:ClientRequestContext
Returns the amount of time allowed until the initial write attempt of the currentRequest
succeeds. This value is initially set fromClientOptions.WRITE_TIMEOUT_MILLIS
.- Specified by:
writeTimeoutMillis
in interfaceClientRequestContext
-
setWriteTimeoutMillis
public void setWriteTimeoutMillis(long writeTimeoutMillis)Description copied from interface:ClientRequestContext
Sets the amount of time allowed until the initial write attempt of the currentRequest
succeeds. This value is initially set fromClientOptions.WRITE_TIMEOUT_MILLIS
.- Specified by:
setWriteTimeoutMillis
in interfaceClientRequestContext
-
setWriteTimeout
Description copied from interface:ClientRequestContext
Sets the amount of time allowed until the initial write attempt of the currentRequest
succeeds. This value is initially set fromClientOptions.WRITE_TIMEOUT_MILLIS
.- Specified by:
setWriteTimeout
in interfaceClientRequestContext
-
responseTimeoutMillis
public long responseTimeoutMillis()Description copied from interface:ClientRequestContext
Returns the amount of time allowed until receiving theResponse
completely since the transfer of theResponse
started or theRequest
was fully sent. This value is initially set fromClientOptions.RESPONSE_TIMEOUT_MILLIS
.- Specified by:
responseTimeoutMillis
in interfaceClientRequestContext
-
clearResponseTimeout
public void clearResponseTimeout()Description copied from interface:ClientRequestContext
Clears the previously scheduled response timeout, if any. Note that calling this will prevent the response from ever being timed out.- Specified by:
clearResponseTimeout
in interfaceClientRequestContext
-
setResponseTimeoutMillis
Description copied from interface:ClientRequestContext
Schedules the response timeout that is triggered when theResponse
is not fully received within the specifiedTimeoutMode
and the specifiedresponseTimeoutMillis
since theResponse
started orRequest
was fully sent. This value is initially set fromClientOptions.RESPONSE_TIMEOUT_MILLIS
.timeout mode description Timeout mode description TimeoutMode.SET_FROM_NOW
Sets a given amount of timeout from the current time. TimeoutMode.SET_FROM_START
Sets a given amount of timeout since the current Response
began processing.TimeoutMode.EXTEND
Extends the previously scheduled timeout by the given amount of timeout. For example:
ClientRequestContext ctx = ...; // Schedules a timeout from the start time of the response ctx.setResponseTimeoutMillis(TimeoutMode.SET_FROM_START, 2000); assert ctx.responseTimeoutMillis() == 2000; ctx.setResponseTimeoutMillis(TimeoutMode.SET_FROM_START, 1000); assert ctx.responseTimeoutMillis() == 1000; // Schedules timeout after 3 seconds from now. ctx.setResponseTimeoutMillis(TimeoutMode.SET_FROM_NOW, 3000); // Extends the previously scheduled timeout. long oldResponseTimeoutMillis = ctx.responseTimeoutMillis(); ctx.setResponseTimeoutMillis(TimeoutMode.EXTEND, 1000); assert ctx.responseTimeoutMillis() == oldResponseTimeoutMillis + 1000; ctx.extendResponseTimeoutMillis(TimeoutMode.EXTEND, -500); assert ctx.responseTimeoutMillis() == oldResponseTimeoutMillis + 500;
- Specified by:
setResponseTimeoutMillis
in interfaceClientRequestContext
-
setResponseTimeout
Description copied from interface:ClientRequestContext
Schedules the response timeout that is triggered when theResponse
is not fully received within the specifiedTimeoutMode
and the specifiedresponseTimeoutMillis
since theResponse
started orRequest
was fully sent. This value is initially set fromClientOptions.RESPONSE_TIMEOUT_MILLIS
.timeout mode description Timeout mode description TimeoutMode.SET_FROM_NOW
Sets a given amount of timeout from the current time. TimeoutMode.SET_FROM_START
Sets a given amount of timeout since the current Response
began processing.TimeoutMode.EXTEND
Extends the previously scheduled timeout by the given amount of timeout. For example:
ClientRequestContext ctx = ...; // Schedules a timeout from the start time of the response ctx.setResponseTimeoutMillis(TimeoutMode.SET_FROM_START, Duration.ofSeconds(2)); assert ctx.responseTimeoutMillis() == 2000; ctx.setResponseTimeoutMillis(TimeoutMode.SET_FROM_START, Duration.ofSeconds(1)); assert ctx.responseTimeoutMillis() == 1000; // Schedules timeout after 3 seconds from now. ctx.setResponseTimeoutMillis(TimeoutMode.SET_FROM_NOW, Duration.ofSeconds(3)); // Extends the previously scheduled timeout. long oldResponseTimeoutMillis = ctx.responseTimeoutMillis(); ctx.setResponseTimeoutMillis(TimeoutMode.EXTEND, Duration.ofSeconds(1)); assert ctx.responseTimeoutMillis() == oldResponseTimeoutMillis + 1000; ctx.setResponseTimeoutMillis(TimeoutMode.EXTEND, Duration.ofMillis(-500)); assert ctx.responseTimeoutMillis() == oldResponseTimeoutMillis + 500;
- Specified by:
setResponseTimeout
in interfaceClientRequestContext
-
maxResponseLength
public long maxResponseLength()Description copied from interface:ClientRequestContext
Returns the maximum length of the receivedResponse
. This value is initially set fromClientOptions.MAX_RESPONSE_LENGTH
.- Specified by:
maxResponseLength
in interfaceClientRequestContext
- Returns:
- the maximum length of the response.
0
if unlimited. - See Also:
ContentTooLargeException
-
setMaxResponseLength
public void setMaxResponseLength(long maxResponseLength)Description copied from interface:ClientRequestContext
Sets the maximum length of the receivedResponse
. This value is initially set fromClientOptions.MAX_RESPONSE_LENGTH
. Specify0
to disable the limit of the length of a response.- Specified by:
setMaxResponseLength
in interfaceClientRequestContext
- See Also:
ContentTooLargeException
-
additionalRequestHeaders
Description copied from interface:ClientRequestContext
- Specified by:
additionalRequestHeaders
in interfaceClientRequestContext
-
setAdditionalRequestHeader
Description copied from interface:ClientRequestContext
Sets a header with the specifiedname
andvalue
. This will remove all previous values associated with the specifiedname
. The header will be included when aClient
sends anHttpRequest
.- Specified by:
setAdditionalRequestHeader
in interfaceClientRequestContext
-
addAdditionalRequestHeader
Description copied from interface:ClientRequestContext
Adds a header with the specifiedname
andvalue
. The header will be included when aClient
sends anHttpRequest
.- Specified by:
addAdditionalRequestHeader
in interfaceClientRequestContext
-
mutateAdditionalRequestHeaders
Description copied from interface:ClientRequestContext
- Specified by:
mutateAdditionalRequestHeaders
in interfaceClientRequestContext
- Parameters:
mutator
- theConsumer
that mutates the additional request headers
-
log
Description copied from interface:RequestContext
Returns theRequestLogAccess
that provides the access to theRequestLog
, which contains the information collected while processing the currentRequest
.- Specified by:
log
in interfaceRequestContext
-
logBuilder
Description copied from interface:RequestContext
Returns theRequestLogBuilder
that collects the information about the currentRequest
.- Specified by:
logBuilder
in interfaceRequestContext
-
cancel
Description copied from interface:RequestContext
- Specified by:
cancel
in interfaceRequestContext
-
cancellationCause
Description copied from interface:RequestContext
Returns the cause of cancellation,null
if the request has not been cancelled.- Specified by:
cancellationCause
in interfaceRequestContext
-
whenResponseCancelling
Description copied from interface:ClientRequestContext
Returns aCompletableFuture
which is completed with aThrowable
cancellation cause when theClientRequestContext
is about to get cancelled. If the response is handled successfully without cancellation, theCompletableFuture
won't complete.- Specified by:
whenResponseCancelling
in interfaceClientRequestContext
-
whenResponseCancelled
Description copied from interface:ClientRequestContext
Returns aCompletableFuture
which is completed with aThrowable
cancellation cause after theClientRequestContext
has been cancelled.RequestContext.isCancelled()
will always returntrue
when the returnedCompletableFuture
is completed. If the response is handled successfully without cancellation, theCompletableFuture
won't complete.- Specified by:
whenResponseCancelled
in interfaceClientRequestContext
-
whenResponseTimingOut
Deprecated.Description copied from interface:ClientRequestContext
Returns aCompletableFuture
which is completed when theClientRequestContext
is about to get timed out. If the response is handled successfully or not cancelled by timeout, theCompletableFuture
won't complete.- Specified by:
whenResponseTimingOut
in interfaceClientRequestContext
-
whenResponseTimedOut
Deprecated.Description copied from interface:ClientRequestContext
Returns aCompletableFuture
which is completed after theClientRequestContext
has been timed out.RequestContext.isTimedOut()
will always returntrue
when the returnedCompletableFuture
is completed. If the response is handled successfully or not cancelled by timeout, theCompletableFuture
won't complete.- Specified by:
whenResponseTimedOut
in interfaceClientRequestContext
-
toString
-