use of io.netty.handler.codec.http.HttpRequest in project riposte by Nike-Inc.
the class RequestStateCleanerHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpRequest) {
// New request incoming - setup/clear *all* state objects for new requests
for (ProcessingStateClassAndKeyPair<? extends ProcessingState> stateClassAndKeyPair : PROCESSING_STATE_ATTRIBUTE_KEYS) {
// See if we have an existing state object for this channel for the given state type.
@SuppressWarnings("unchecked") AttributeKey<ProcessingState> attrKey = (AttributeKey<ProcessingState>) stateClassAndKeyPair.getRight();
Attribute<ProcessingState> processingStateAttr = ctx.channel().attr(attrKey);
ProcessingState processingState = processingStateAttr.get();
if (processingState == null) {
// We don't already have one for this channel, so create one and register it.
processingState = stateClassAndKeyPair.getLeft().newInstance();
processingStateAttr.set(processingState);
}
// Clean the state for the new request.
processingState.cleanStateForNewRequest();
}
// send request received event
if (metricsListener != null) {
metricsListener.onEvent(ServerMetricsEvent.REQUEST_RECEIVED, ChannelAttributes.getHttpProcessingStateForChannel(ctx).get());
}
// Remove the idle channel timeout handler (if there is one) so that it doesn't kill this new request if the
// endpoint takes longer to complete than the idle timeout value - the idle channel timeout is only for
// timing out channels that are idle *in-between* requests.
ChannelPipeline pipeline = ctx.pipeline();
ChannelHandler idleChannelTimeoutHandler = pipeline.get(HttpChannelInitializer.IDLE_CHANNEL_TIMEOUT_HANDLER_NAME);
if (idleChannelTimeoutHandler != null)
pipeline.remove(idleChannelTimeoutHandler);
// last chunk when the timeout hits.
if (incompleteHttpCallTimeoutMillis > 0 && !(msg instanceof LastHttpContent)) {
IncompleteHttpCallTimeoutHandler newHandler = new IncompleteHttpCallTimeoutHandler(incompleteHttpCallTimeoutMillis);
ChannelHandler existingHandler = pipeline.get(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
if (existingHandler == null) {
pipeline.addFirst(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME, newHandler);
} else {
logger.error("Handling HttpRequest for new request and found an IncompleteHttpCallTimeoutHandler " + "already in the pipeline. This should not be possible. A new " + "IncompleteHttpCallTimeoutHandler will replace the old one. worker_channel_id={}", ctx.channel().toString());
pipeline.replace(existingHandler, INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME, newHandler);
}
}
} else if (msg instanceof LastHttpContent) {
// The HTTP call is complete, so we can remove the IncompleteHttpCallTimeoutHandler.
ChannelPipeline pipeline = ctx.pipeline();
ChannelHandler existingHandler = pipeline.get(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
if (existingHandler != null)
pipeline.remove(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
}
// Continue on the pipeline processing.
super.channelRead(ctx, msg);
}
use of io.netty.handler.codec.http.HttpRequest in project riposte by Nike-Inc.
the class StreamingAsyncHttpClient method streamDownstreamCall.
/**
* TODO: Fully document me.
* <br/>
* NOTE: The returned CompletableFuture will only be completed successfully if the connection to the downstream
* server was successful and the initialRequestChunk was successfully written out. This has implications for
* initialRequestChunk regarding releasing its reference count (i.e. calling {@link
* io.netty.util.ReferenceCountUtil#release(Object)} and passing in initialRequestChunk). If the returned
* CompletableFuture is successful it means initialRequestChunk's reference count will already be reduced by one
* relative to when this method was called because it will have been passed to a successful {@link
* ChannelHandlerContext#writeAndFlush(Object)} method call.
* <p/>
* Long story short - assume initialRequestChunk is an object with a reference count of x:
* <ul>
* <li>
* If the returned CompletableFuture is successful, then when it completes successfully
* initialRequestChunk's reference count will be x - 1
* </li>
* <li>
* If the returned CompletableFuture is *NOT* successful, then when it completes initialRequestChunk's
* reference count will still be x
* </li>
* </ul>
*/
public CompletableFuture<StreamingChannel> streamDownstreamCall(String downstreamHost, int downstreamPort, HttpRequest initialRequestChunk, boolean isSecureHttpsCall, boolean relaxedHttpsValidation, StreamingCallback callback, long downstreamCallTimeoutMillis, boolean performSubSpanAroundDownstreamCalls, boolean addTracingHeadersToDownstreamCall, ChannelHandlerContext ctx) {
CompletableFuture<StreamingChannel> streamingChannel = new CompletableFuture<>();
// set host header. include port in value when it is a non-default port
boolean isDefaultPort = (downstreamPort == 80 && !isSecureHttpsCall) || (downstreamPort == 443 && isSecureHttpsCall);
String hostHeaderValue = (isDefaultPort) ? downstreamHost : downstreamHost + ":" + downstreamPort;
initialRequestChunk.headers().set(HttpHeaders.Names.HOST, hostHeaderValue);
long beforeConnectionStartTimeNanos = System.nanoTime();
// Create a connection to the downstream server.
ChannelPool pool = getPooledChannelFuture(downstreamHost, downstreamPort);
Future<Channel> channelFuture = pool.acquire();
// Add a listener that kicks off the downstream call once the connection is completed.
channelFuture.addListener(future -> {
Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null;
try {
long connectionSetupTimeNanos = System.nanoTime() - beforeConnectionStartTimeNanos;
HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
if (httpProcessingState != null) {
RequestInfo<?> requestInfo = httpProcessingState.getRequestInfo();
if (requestInfo != null) {
requestInfo.addRequestAttribute(DOWNSTREAM_CALL_CONNECTION_SETUP_TIME_NANOS_REQUEST_ATTR_KEY, connectionSetupTimeNanos);
}
}
// Setup tracing and MDC so our log messages have the correct distributed trace info, etc.
originalThreadInfo = linkTracingAndMdcToCurrentThread(ctx);
if (logger.isDebugEnabled()) {
logger.debug("CONNECTION SETUP TIME NANOS: {}", connectionSetupTimeNanos);
}
if (!future.isSuccess()) {
try {
// We did not connect to the downstream host successfully. Notify the callback.
streamingChannel.completeExceptionally(new WrapperException("Unable to connect to downstream host: " + downstreamHost, future.cause()));
} finally {
Channel ch = channelFuture.getNow();
if (ch != null) {
// We likely will never reach here since the channel future was not successful, however if
// we *do* manage to get here somehow, then mark the channel broken and release it back
// to the pool.
markChannelAsBroken(ch);
pool.release(ch);
}
}
return;
}
// noinspection ConstantConditions
if (performSubSpanAroundDownstreamCalls) {
// Add the subspan.
String spanName = getSubspanSpanName(initialRequestChunk.getMethod().name(), downstreamHost + ":" + downstreamPort + initialRequestChunk.getUri());
if (Tracer.getInstance().getCurrentSpan() == null) {
// There is no parent span to start a subspan from, so we have to start a new span for this call
// rather than a subspan.
// TODO: Set this to CLIENT once we have that ability in the wingtips API for request root spans
Tracer.getInstance().startRequestWithRootSpan(spanName);
} else {
// There was at least one span on the stack, so we can start a subspan for this call.
Tracer.getInstance().startSubSpan(spanName, Span.SpanPurpose.CLIENT);
}
}
Deque<Span> distributedSpanStackToUse = Tracer.getInstance().getCurrentSpanStackCopy();
Map<String, String> mdcContextToUse = MDC.getCopyOfContextMap();
Span spanForDownstreamCall = (distributedSpanStackToUse == null) ? null : distributedSpanStackToUse.peek();
// Add distributed trace headers to the downstream call if desired and we have a current span.
if (addTracingHeadersToDownstreamCall && spanForDownstreamCall != null) {
HttpRequestTracingUtils.propagateTracingHeaders((headerKey, headerValue) -> {
if (headerValue != null) {
initialRequestChunk.headers().set(headerKey, headerValue);
}
}, spanForDownstreamCall);
}
Channel ch = channelFuture.getNow();
if (logger.isDebugEnabled())
logger.debug("Channel ID of the Channel pulled from the pool: {}", ch.toString());
// We may not be in the right thread to modify the channel pipeline and write data. If we're in the
// wrong thread we can get deadlock type situations. By running the relevant bits in the channel's
// event loop we're guaranteed it will be run in the correct thread.
ch.eventLoop().execute(runnableWithTracingAndMdc(() -> {
BiConsumer<String, Throwable> prepChannelErrorHandler = (errorMessage, cause) -> {
try {
streamingChannel.completeExceptionally(new WrapperException(errorMessage, cause));
} finally {
// This channel may be permanently busted depending on the error, so mark it broken and let
// the pool close it and clean it up.
markChannelAsBroken(ch);
pool.release(ch);
}
};
try {
ObjectHolder<Boolean> callActiveHolder = new ObjectHolder<>();
callActiveHolder.heldObject = true;
ObjectHolder<Boolean> lastChunkSentDownstreamHolder = new ObjectHolder<>();
lastChunkSentDownstreamHolder.heldObject = false;
// noinspection ConstantConditions
prepChannelForDownstreamCall(pool, ch, callback, distributedSpanStackToUse, mdcContextToUse, isSecureHttpsCall, relaxedHttpsValidation, performSubSpanAroundDownstreamCalls, downstreamCallTimeoutMillis, callActiveHolder, lastChunkSentDownstreamHolder);
logInitialRequestChunk(initialRequestChunk, downstreamHost, downstreamPort);
// Send the HTTP request.
ChannelFuture writeFuture = ch.writeAndFlush(initialRequestChunk);
// After the initial chunk has been sent we'll open the floodgates
// for any further chunk streaming
writeFuture.addListener(completedWriteFuture -> {
if (completedWriteFuture.isSuccess())
streamingChannel.complete(new StreamingChannel(ch, pool, callActiveHolder, lastChunkSentDownstreamHolder, distributedSpanStackToUse, mdcContextToUse));
else {
prepChannelErrorHandler.accept("Writing the first HttpRequest chunk to the downstream service failed.", completedWriteFuture.cause());
// noinspection UnnecessaryReturnStatement
return;
}
});
} catch (SSLException | NoSuchAlgorithmException | KeyStoreException ex) {
prepChannelErrorHandler.accept("Error setting up SSL context for downstream call", ex);
// noinspection UnnecessaryReturnStatement
return;
} catch (Throwable t) {
// If we don't catch and handle this here it gets swallowed since we're in a Runnable
prepChannelErrorHandler.accept("An unexpected error occurred while prepping the channel pipeline for the downstream call", t);
// noinspection UnnecessaryReturnStatement
return;
}
}, ctx));
} catch (Throwable ex) {
try {
String errorMsg = "Error occurred attempting to send first chunk (headers/etc) downstream";
Exception errorToFire = new WrapperException(errorMsg, ex);
logger.warn(errorMsg, errorToFire);
streamingChannel.completeExceptionally(errorToFire);
} finally {
Channel ch = channelFuture.getNow();
if (ch != null) {
// Depending on where the error was thrown the channel may or may not exist. If it does exist,
// then assume it's unusable, mark it as broken, and let the pool close it and remove it.
markChannelAsBroken(ch);
pool.release(ch);
}
}
} finally {
// Unhook the tracing and MDC stuff from this thread now that we're done.
unlinkTracingAndMdcFromCurrentThread(originalThreadInfo);
}
});
return streamingChannel;
}
use of io.netty.handler.codec.http.HttpRequest in project riposte by Nike-Inc.
the class RequestInfoSetterHandlerTest method doChannelRead_uses_existing_RequestInfo_on_state_if_available_and_does_not_recreate_it.
@Test
public void doChannelRead_uses_existing_RequestInfo_on_state_if_available_and_does_not_recreate_it() {
// given
HttpRequest msgMock = mock(HttpRequest.class);
String uri = "/some/url";
HttpHeaders headers = new DefaultHttpHeaders();
doReturn(uri).when(msgMock).getUri();
doReturn(headers).when(msgMock).headers();
doReturn(HttpVersion.HTTP_1_1).when(msgMock).getProtocolVersion();
doReturn(requestInfo).when(stateMock).getRequestInfo();
// when
PipelineContinuationBehavior result = handler.doChannelRead(ctxMock, msgMock);
// then
verify(stateMock, never()).setRequestInfo(any(RequestInfo.class));
assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
use of io.netty.handler.codec.http.HttpRequest in project riposte by Nike-Inc.
the class RequestInfoSetterHandlerTest method doChannelRead_HttpRequest_throws_exception_when_failed_decoder_result.
@Test
public void doChannelRead_HttpRequest_throws_exception_when_failed_decoder_result() {
// given
HttpRequest msgMock = mock(HttpRequest.class);
DecoderResult decoderResult = mock(DecoderResult.class);
doReturn(true).when(decoderResult).isFailure();
doReturn(decoderResult).when(msgMock).getDecoderResult();
doReturn(null).when(stateMock).getRequestInfo();
// when
Throwable thrownException = Assertions.catchThrowable(() -> handler.doChannelRead(ctxMock, msgMock));
// then
assertThat(thrownException).isExactlyInstanceOf(InvalidHttpRequestException.class);
}
use of io.netty.handler.codec.http.HttpRequest in project riposte by Nike-Inc.
the class RoutingHandlerTest method doChannelRead_HttpRequest_throws_exception_when_failed_decoder_result.
@Test
public void doChannelRead_HttpRequest_throws_exception_when_failed_decoder_result() {
// given
HttpRequest msgMock = mock(HttpRequest.class);
DecoderResult decoderResult = mock(DecoderResult.class);
doReturn(true).when(decoderResult).isFailure();
doReturn(decoderResult).when(msgMock).getDecoderResult();
doReturn(null).when(stateMock).getRequestInfo();
// when
Throwable thrownException = Assertions.catchThrowable(() -> handlerSpy.doChannelRead(ctxMock, msgMock));
// then
assertThat(thrownException).isExactlyInstanceOf(InvalidHttpRequestException.class);
}
Aggregations