use of com.linecorp.armeria.server.ServiceRequestContext 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;
});
}
Aggregations