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
Constructors Constructor Description DefaultClientRequestContext(MeterRegistry meterRegistry, SessionProtocol sessionProtocol, RequestId id, HttpMethod method, String path, String query, String fragment, ClientOptions options, HttpRequest req, RpcRequest rpcReq, long requestStartTimeNanos, long requestStartTimeMicros)
Creates a new instance. -
Method Summary
Modifier and Type Method Description void
addAdditionalRequestHeader(CharSequence name, Object value)
Adds a header with the specifiedname
andvalue
.HttpHeaders
additionalRequestHeaders()
ByteBufAllocator
alloc()
Returns theByteBufAllocator
for thisRequestContext
.protected Channel
channel()
Returns theChannel
that is handling this request, ornull
if the connection is not established yet.void
clearResponseTimeout()
Clears the previously scheduled response timeout, if any.Endpoint
endpoint()
EndpointGroup
endpointGroup()
Returns theEndpointGroup
used for the currentRequest
.EventLoop
eventLoop()
String
fragment()
Returns the fragment part of the URI of the currentRequest
, as defined in the section 3.5 of RFC3986.boolean
init(EndpointGroup endpointGroup)
Initializes this context with the specifiedEndpointGroup
.RequestLogAccess
log()
Returns theRequestLogAccess
that provides the access to theRequestLog
, which contains the information collected while processing the currentRequest
.RequestLogBuilder
logBuilder()
Returns theRequestLogBuilder
that collects the information about the currentRequest
.long
maxResponseLength()
Returns the maximum length of the receivedResponse
.void
mutateAdditionalRequestHeaders(Consumer<HttpHeadersBuilder> mutator)
ClientRequestContext
newDerivedContext(RequestId id, HttpRequest req, RpcRequest rpcReq, Endpoint endpoint)
Creates a newClientRequestContext
whose properties andAttribute
s are copied from thisClientRequestContext
, except having differentRequest
,Endpoint
and its ownRequestLog
.ClientOptions
options()
Returns theClientOptions
of the currentRequest
.Runnable
responseTimeoutHandler()
ReturnsResponse
timeout handler which is executed when theResponse
is not completely received within the allowedClientRequestContext.responseTimeoutMillis()
or the defaultClientOption.RESPONSE_TIMEOUT_MILLIS
.long
responseTimeoutMillis()
ServiceRequestContext
root()
Returns theServiceRequestContext
whoseService
invokes theClient
Request
which created thisClientRequestContext
, ornull
if this client request was not made in the context of a server request.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
setResponseTimeoutAtMillis(long responseTimeoutAtMillis)
Deprecated.void
setResponseTimeoutHandler(Runnable responseTimeoutHandler)
Sets a handler to run when the response times out.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)
Returns the amount of time allowed until the initial write attempt of the currentRequest
succeeds.void
setWriteTimeoutMillis(long writeTimeoutMillis)
Returns the amount of time allowed until the initial write attempt of the currentRequest
succeeds.SSLSession
sslSession()
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.String
toString()
protected void
validateHeaders(RequestHeaders headers)
Validates the specifiedRequestHeaders
.long
writeTimeoutMillis()
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, computeAttrIfAbsent, decodedPath, id, localAddress, meterRegistry, method, ownAttr, ownAttrs, path, query, remoteAddress, request, rpcRequest, sessionProtocol, setAttr, setAttrIfAbsent, 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
attr, attrs, extendResponseTimeout, extendResponseTimeoutMillis, newDerivedContext, ownAttr, ownAttrs, push, request, rpcRequest, setResponseTimeout, setResponseTimeout, setResponseTimeoutAfter, setResponseTimeoutAfterMillis, setResponseTimeoutAt, setResponseTimeoutMillis
Methods inherited from interface com.linecorp.armeria.common.RequestContext
computeAttrIfAbsent, contextAwareEventLoop, contextAwareExecutor, decodedPath, executor, id, localAddress, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, meterRegistry, method, path, query, remoteAddress, replace, sessionProtocol, setAttr, setAttrIfAbsent, updateRequest, updateRpcRequest
-
Constructor Details
-
DefaultClientRequestContext
public DefaultClientRequestContext(MeterRegistry meterRegistry, SessionProtocol sessionProtocol, RequestId id, HttpMethod method, String path, @Nullable String query, @Nullable String fragment, ClientOptions options, @Nullable HttpRequest req, @Nullable RpcRequest rpcReq, long requestStartTimeNanos, long requestStartTimeMicros)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
.
-
-
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.
-
root
Description copied from interface:ClientRequestContext
Returns theServiceRequestContext
whoseService
invokes theClient
Request
which created thisClientRequestContext
, ornull
if this client request was not made in the context of a server request.- Specified by:
root
in interfaceClientRequestContext
-
newDerivedContext
public ClientRequestContext newDerivedContext(RequestId id, @Nullable HttpRequest req, @Nullable RpcRequest rpcReq, 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
- 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 fromClientOption.WRITE_TIMEOUT_MILLIS
.- Specified by:
writeTimeoutMillis
in interfaceClientRequestContext
-
setWriteTimeoutMillis
public void setWriteTimeoutMillis(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 fromClientOption.WRITE_TIMEOUT_MILLIS
.- Specified by:
setWriteTimeoutMillis
in interfaceClientRequestContext
-
setWriteTimeout
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 fromClientOption.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. This value is initially set fromClientOption.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 fromClientOption.RESPONSE_TIMEOUT_MILLIS
.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
-
setResponseTimeoutAtMillis
Deprecated.Description copied from interface:ClientRequestContext
Schedules the response timeout that is triggered at the specified time represented as the number since the epoch (1970-01-01T00:00:00Z
). Note that the response will be timed out immediately if the specified time is before now. The initial timeout is set fromClientOption.RESPONSE_TIMEOUT_MILLIS
.For example:
ClientRequestContext ctx = ...; // Schedules timeout after 1 seconds from now. long responseTimeoutAt = Instant.now().plus(1, ChronoUnit.SECONDS).toEpochMilli(); ctx.setResponseTimeoutAtMillis(responseTimeoutAt);
- Specified by:
setResponseTimeoutAtMillis
in interfaceClientRequestContext
- Parameters:
responseTimeoutAtMillis
- the response timeout represented as the number of milliseconds since the epoch (1970-01-01T00:00:00Z
)
-
responseTimeoutHandler
Description copied from interface:ClientRequestContext
ReturnsResponse
timeout handler which is executed when theResponse
is not completely received within the allowedClientRequestContext.responseTimeoutMillis()
or the defaultClientOption.RESPONSE_TIMEOUT_MILLIS
.- Specified by:
responseTimeoutHandler
in interfaceClientRequestContext
-
setResponseTimeoutHandler
Description copied from interface:ClientRequestContext
Sets a handler to run when the response times out.responseTimeoutHandler
must abort the response, e.g., by callingStreamMessage.abort(Throwable)
. If not set, the response will be closed withResponseTimeoutException
.For example,
HttpResponseWriter res = HttpResponse.streaming(); ctx.setResponseTimeoutHandler(() -> { res.abort(new IllegalStateException("Server is in a bad state.")); }); ...
- Specified by:
setResponseTimeoutHandler
in interfaceClientRequestContext
-
maxResponseLength
public long maxResponseLength()Description copied from interface:ClientRequestContext
Returns the maximum length of the receivedResponse
. This value is initially set fromClientOption.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 fromClientOption.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
-
toString
-