Search in sources :

Example 1 with SafeCloseable

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;
    });
}
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)

Example 2 with SafeCloseable

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;
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor) AggregatedHttpRequest(com.linecorp.armeria.common.AggregatedHttpRequest) OBJECT_MAPPER(zipkin2.elasticsearch.internal.JsonSerializers.OBJECT_MAPPER) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HttpStatus(com.linecorp.armeria.common.HttpStatus) WebClient(com.linecorp.armeria.client.WebClient) Clients(com.linecorp.armeria.client.Clients) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) Callback(zipkin2.Callback) HttpRequestWriter(com.linecorp.armeria.common.HttpRequestWriter) Exceptions(com.linecorp.armeria.common.util.Exceptions) UnprocessedRequestException(com.linecorp.armeria.client.UnprocessedRequestException) SafeCloseable(com.linecorp.armeria.common.util.SafeCloseable) HttpData(com.linecorp.armeria.common.HttpData) HttpResponse(com.linecorp.armeria.common.HttpResponse) JsonParser(com.fasterxml.jackson.core.JsonParser) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) FileNotFoundException(java.io.FileNotFoundException) HttpStatusClass(com.linecorp.armeria.common.HttpStatusClass) RequestContext(com.linecorp.armeria.common.RequestContext) HttpRequest(com.linecorp.armeria.common.HttpRequest) HttpHeaders(com.linecorp.armeria.common.HttpHeaders) JSON_FACTORY(zipkin2.elasticsearch.internal.JsonSerializers.JSON_FACTORY) Call(zipkin2.Call) InputStream(java.io.InputStream) UnprocessedRequestException(com.linecorp.armeria.client.UnprocessedRequestException) HttpRequestWriter(com.linecorp.armeria.common.HttpRequestWriter) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) HttpResponse(com.linecorp.armeria.common.HttpResponse) SafeCloseable(com.linecorp.armeria.common.util.SafeCloseable) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

HttpData (com.linecorp.armeria.common.HttpData)2 HttpRequest (com.linecorp.armeria.common.HttpRequest)2 HttpResponse (com.linecorp.armeria.common.HttpResponse)2 SafeCloseable (com.linecorp.armeria.common.util.SafeCloseable)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Clients (com.linecorp.armeria.client.Clients)1 UnprocessedRequestException (com.linecorp.armeria.client.UnprocessedRequestException)1 WebClient (com.linecorp.armeria.client.WebClient)1 AggregatedHttpRequest (com.linecorp.armeria.common.AggregatedHttpRequest)1 AggregatedHttpResponse (com.linecorp.armeria.common.AggregatedHttpResponse)1 HttpHeaders (com.linecorp.armeria.common.HttpHeaders)1 HttpRequestWriter (com.linecorp.armeria.common.HttpRequestWriter)1 HttpStatus (com.linecorp.armeria.common.HttpStatus)1 HttpStatusClass (com.linecorp.armeria.common.HttpStatusClass)1 RequestContext (com.linecorp.armeria.common.RequestContext)1 RequestHeaders (com.linecorp.armeria.common.RequestHeaders)1 Exceptions (com.linecorp.armeria.common.util.Exceptions)1 ServiceRequestContext (com.linecorp.armeria.server.ServiceRequestContext)1 ServiceRequestContextBuilder (com.linecorp.armeria.server.ServiceRequestContextBuilder)1