use of com.linecorp.armeria.common.util.SafeCloseable in project zipkin by openzipkin.
the class ScribeInboundHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object payload) {
assert payload instanceof ByteBuf;
HttpRequest request = HttpRequest.of(THRIFT_HEADERS, HttpData.wrap((ByteBuf) payload));
ServiceRequestContextBuilder requestContextBuilder = ServiceRequestContext.builder(request).service(scribeService).alloc(ctx.alloc());
if (ctx.executor() instanceof EventLoop) {
requestContextBuilder.eventLoop((EventLoop) ctx.executor());
}
ServiceRequestContext requestContext = requestContextBuilder.build();
final HttpResponse response;
try (SafeCloseable unused = requestContext.push()) {
response = HttpResponse.of(scribeService.serve(requestContext, request));
} catch (Throwable t) {
propagateIfFatal(t);
exceptionCaught(ctx, t);
return;
}
int responseIndex = nextResponseIndex++;
response.aggregateWithPooledObjects(ctx.executor(), ctx.alloc()).handle((msg, t) -> {
if (t != null) {
exceptionCaught(ctx, t);
return null;
}
try (HttpData content = msg.content()) {
ByteBuf returned = ctx.alloc().buffer(content.length() + 4);
returned.writeInt(content.length());
returned.writeBytes(content.byteBuf());
if (responseIndex == previouslySentResponseIndex + 1) {
ctx.writeAndFlush(returned);
previouslySentResponseIndex++;
flushResponses(ctx);
} else {
pendingResponses.put(responseIndex, returned);
}
}
return null;
});
}
use of com.linecorp.armeria.common.util.SafeCloseable in project zipkin by openzipkin.
the class HttpCall method sendRequest.
CompletableFuture<AggregatedHttpResponse> sendRequest() {
final HttpResponse response;
try (SafeCloseable ignored = Clients.withContextCustomizer(ctx -> ctx.logBuilder().name(name))) {
HttpRequestWriter httpRequest = HttpRequest.streaming(request.headers());
response = httpClient.execute(httpRequest);
request.writeBody(httpRequest::tryWrite);
httpRequest.close();
}
CompletableFuture<AggregatedHttpResponse> responseFuture = RequestContext.mapCurrent(ctx -> response.aggregateWithPooledObjects(ctx.eventLoop(), ctx.alloc()), // This should never be used in practice since the module runs in an Armeria server.
response::aggregate);
responseFuture = responseFuture.exceptionally(t -> {
if (t instanceof UnprocessedRequestException) {
Throwable cause = t.getCause();
// Go ahead and reduce the output in logs since this is usually a configuration or
// infrastructure issue and the Armeria stack trace won't help debugging that.
Exceptions.clearTrace(cause);
String message = cause.getMessage();
if (message == null)
message = cause.getClass().getSimpleName();
throw new RejectedExecutionException(message, cause);
} else {
Exceptions.throwUnsafely(t);
}
return null;
});
this.responseFuture = responseFuture;
return responseFuture;
}
Aggregations