Search in sources :

Example 1 with NonChunkedHttpRequest

use of com.couchbase.client.core.msg.NonChunkedHttpRequest in project couchbase-jvm-clients by couchbase.

the class NonChunkedHttpMessageHandler method write.

/**
 * Writes a given request and encodes it.
 *
 * @param ctx the channel handler context.
 * @param msg the msg to write.
 * @param promise the promise that will be passed along.
 */
@Override
@SuppressWarnings("unchecked")
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
    // toes.
    if (currentRequest != null) {
        RetryOrchestrator.maybeRetry(endpointContext, (Request<? extends Response>) msg, RetryReason.NOT_PIPELINED_REQUEST_IN_FLIGHT);
        if (endpoint != null) {
            endpoint.decrementOutstandingRequests();
        }
        return;
    }
    if (msg instanceof NonChunkedHttpRequest) {
        try {
            currentRequest = (NonChunkedHttpRequest<Response>) msg;
            FullHttpRequest encoded = ((NonChunkedHttpRequest<Response>) msg).encode();
            encoded.headers().set(HttpHeaderNames.HOST, remoteHost);
            encoded.headers().set(HttpHeaderNames.USER_AGENT, endpointContext.environment().userAgent().formattedLong());
            dispatchTimingStart = System.nanoTime();
            if (currentRequest.requestSpan() != null) {
                RequestTracer tracer = endpointContext.environment().requestTracer();
                currentDispatchSpan = tracer.requestSpan(TracingIdentifiers.SPAN_DISPATCH, currentRequest.requestSpan());
                if (!CbTracing.isInternalTracer(tracer)) {
                    setCommonDispatchSpanAttributes(currentDispatchSpan, ctx.channel().attr(ChannelAttributes.CHANNEL_ID_KEY).get(), ioContext.localHostname(), ioContext.localPort(), endpoint.remoteHostname(), endpoint.remotePort(), currentRequest.operationId());
                }
            }
            ctx.write(encoded, promise);
        } catch (Throwable t) {
            currentRequest.response().completeExceptionally(t);
            if (endpoint != null) {
                endpoint.decrementOutstandingRequests();
            }
        }
    } else {
        if (endpoint != null) {
            endpoint.decrementOutstandingRequests();
        }
        eventBus.publish(new InvalidRequestDetectedEvent(ioContext, serviceType, msg));
        ctx.channel().close().addListener(f -> eventBus.publish(new ChannelClosedProactivelyEvent(ioContext, ChannelClosedProactivelyEvent.Reason.INVALID_REQUEST_DETECTED)));
    }
}
Also used : FullHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse) Response(com.couchbase.client.core.msg.Response) FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) ChannelClosedProactivelyEvent(com.couchbase.client.core.cnc.events.io.ChannelClosedProactivelyEvent) NonChunkedHttpRequest(com.couchbase.client.core.msg.NonChunkedHttpRequest) RequestTracer(com.couchbase.client.core.cnc.RequestTracer) InvalidRequestDetectedEvent(com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent)

Aggregations

RequestTracer (com.couchbase.client.core.cnc.RequestTracer)1 ChannelClosedProactivelyEvent (com.couchbase.client.core.cnc.events.io.ChannelClosedProactivelyEvent)1 InvalidRequestDetectedEvent (com.couchbase.client.core.cnc.events.io.InvalidRequestDetectedEvent)1 FullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest)1 FullHttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpResponse)1 NonChunkedHttpRequest (com.couchbase.client.core.msg.NonChunkedHttpRequest)1 Response (com.couchbase.client.core.msg.Response)1