Search in sources :

Example 11 with TimeoutAsyncPoolHandle

use of com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle in project rest.li by linkedin.

the class TestHttp2ProtocolUpgradeHandler method testChannelCloseBeforeUpgrade.

@Test(timeOut = 10000)
@SuppressWarnings("unchecked")
public void testChannelCloseBeforeUpgrade() throws Exception {
    Http2UpgradeHandler handler = new Http2UpgradeHandler();
    EmbeddedChannel channel = new EmbeddedChannel(handler);
    // Reads the upgrade request from the outbound buffer to ensure nothing in the buffer
    Assert.assertEquals(channel.outboundMessages().size(), 1);
    Assert.assertNotNull(channel.readOutbound());
    Assert.assertTrue(channel.outboundMessages().isEmpty());
    RequestWithCallback request = Mockito.mock(RequestWithCallback.class);
    TimeoutAsyncPoolHandle handle = Mockito.mock(TimeoutAsyncPoolHandle.class);
    TimeoutTransportCallback callback = Mockito.mock(TimeoutTransportCallback.class);
    Mockito.when(request.handle()).thenReturn(handle);
    Mockito.when(request.callback()).thenReturn(callback);
    // Write should not succeed before upgrade completes
    Assert.assertFalse(channel.writeOutbound(request));
    Assert.assertFalse(channel.finish());
    // Synchronously waiting for channel to close
    channel.close().sync();
    Mockito.verify(request).handle();
    Mockito.verify(request).callback();
    Mockito.verify(handle).dispose();
    Mockito.verify(callback).onResponse(Mockito.any(TransportResponse.class));
}
Also used : RequestWithCallback(com.linkedin.r2.transport.common.bridge.common.RequestWithCallback) TimeoutTransportCallback(com.linkedin.r2.transport.http.client.TimeoutTransportCallback) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) TransportResponse(com.linkedin.r2.transport.common.bridge.common.TransportResponse) TimeoutAsyncPoolHandle(com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle) Test(org.testng.annotations.Test)

Example 12 with TimeoutAsyncPoolHandle

use of com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle in project rest.li by linkedin.

the class Http2FrameListener method onHeadersRead.

@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) throws Http2Exception {
    LOG.debug("Received HTTP/2 HEADERS frame, stream={}, end={}, headers={}, padding={}bytes", new Object[] { streamId, endOfStream, headers.size(), padding });
    // Ignores response for the upgrade request
    if (streamId == Http2CodecUtil.HTTP_UPGRADE_STREAM_ID) {
        return;
    }
    // Refactored duplicate code to new code pipeline.
    final StreamResponseBuilder builder = Http2MessageDecoders.ResponseDecoder.buildStreamResponse(headers);
    // Gets async pool handle from stream properties
    TimeoutAsyncPoolHandle<?> timeoutHandle = Http2PipelinePropertyUtil.remove(ctx, _connection, streamId, Http2ClientPipelineInitializer.CHANNEL_POOL_HANDLE_ATTR_KEY);
    if (timeoutHandle == null) {
        _lifecycleManager.onError(ctx, false, Http2Exception.connectionError(Http2Error.PROTOCOL_ERROR, "No channel pool handle is associated with this stream", streamId));
        return;
    }
    final StreamResponse response;
    if (endOfStream) {
        response = builder.build(EntityStreams.emptyStream());
        // Release the handle to put the channel back to the pool
        timeoutHandle.release();
    } else {
        // Associate an entity stream writer to the HTTP/2 stream
        final TimeoutBufferedWriter writer = new TimeoutBufferedWriter(ctx, streamId, _maxContentLength, timeoutHandle);
        if (_connection.stream(streamId).setProperty(_writerKey, writer) != null) {
            _lifecycleManager.onError(ctx, false, Http2Exception.connectionError(Http2Error.PROTOCOL_ERROR, "Another writer has already been associated with current stream ID", streamId));
            return;
        }
        // Prepares StreamResponse for the channel pipeline
        EntityStream entityStream = EntityStreams.newEntityStream(writer);
        response = builder.build(entityStream);
    }
    // Gets callback from stream properties
    TransportCallback<?> callback = Http2PipelinePropertyUtil.remove(ctx, _connection, streamId, Http2ClientPipelineInitializer.CALLBACK_ATTR_KEY);
    if (callback != null) {
        ctx.fireChannelRead(new ResponseWithCallback<Response, TransportCallback<?>>(response, callback));
    }
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Response(com.linkedin.r2.message.Response) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) StreamResponse(com.linkedin.r2.message.stream.StreamResponse)

Aggregations

RequestWithCallback (com.linkedin.r2.transport.common.bridge.common.RequestWithCallback)7 TimeoutAsyncPoolHandle (com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle)6 ChannelFuture (io.netty.channel.ChannelFuture)5 Test (org.testng.annotations.Test)5 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)3 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ByteString (com.linkedin.data.ByteString)2 Response (com.linkedin.r2.message.Response)2 StreamResponseBuilder (com.linkedin.r2.message.stream.StreamResponseBuilder)2 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)2 TransportResponse (com.linkedin.r2.transport.common.bridge.common.TransportResponse)2 TimeoutTransportCallback (com.linkedin.r2.transport.http.client.TimeoutTransportCallback)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 R2Constants (com.linkedin.r2.filter.R2Constants)1 Request (com.linkedin.r2.message.Request)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)1 ReadHandle (com.linkedin.r2.message.stream.entitystream.ReadHandle)1 Reader (com.linkedin.r2.message.stream.entitystream.Reader)1