Search in sources :

Example 1 with ServiceRequestContext

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;
    });
}
Also used : HttpRequest(com.linecorp.armeria.common.HttpRequest) ServiceRequestContextBuilder(com.linecorp.armeria.server.ServiceRequestContextBuilder) EventLoop(io.netty.channel.EventLoop) ServiceRequestContext(com.linecorp.armeria.server.ServiceRequestContext) HttpData(com.linecorp.armeria.common.HttpData) HttpResponse(com.linecorp.armeria.common.HttpResponse) ByteBuf(io.netty.buffer.ByteBuf) SafeCloseable(com.linecorp.armeria.common.util.SafeCloseable)

Aggregations

HttpData (com.linecorp.armeria.common.HttpData)1 HttpRequest (com.linecorp.armeria.common.HttpRequest)1 HttpResponse (com.linecorp.armeria.common.HttpResponse)1 SafeCloseable (com.linecorp.armeria.common.util.SafeCloseable)1 ServiceRequestContext (com.linecorp.armeria.server.ServiceRequestContext)1 ServiceRequestContextBuilder (com.linecorp.armeria.server.ServiceRequestContextBuilder)1 ByteBuf (io.netty.buffer.ByteBuf)1 EventLoop (io.netty.channel.EventLoop)1