Search in sources :

Example 56 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project netty by netty.

the class DefaultHttp2ConnectionEncoderTest method infoHeadersAndTrailersWithData.

private void infoHeadersAndTrailersWithData(boolean eos, int infoHeaderCount) {
    writeAllFlowControlledFrames();
    final int streamId = 6;
    Http2Headers infoHeaders = informationalHeaders();
    for (int i = 0; i < infoHeaderCount; ++i) {
        encoder.writeHeaders(ctx, streamId, infoHeaders, 0, false, newPromise());
    }
    Http2Stream stream = connection.stream(streamId);
    when(remoteFlow.hasFlowControlled(eq(stream))).thenReturn(true);
    ChannelPromise promise2 = newPromise();
    encoder.writeHeaders(ctx, streamId, EmptyHttp2Headers.INSTANCE, 0, false, promise2);
    ChannelPromise promise3 = newPromise();
    ChannelFuture future = encoder.writeHeaders(ctx, streamId, EmptyHttp2Headers.INSTANCE, 0, eos, promise3);
    assertTrue(future.isDone());
    assertEquals(eos, future.isSuccess());
    verify(writer, times(infoHeaderCount)).writeHeaders(eq(ctx), eq(streamId), eq(infoHeaders), eq(0), eq(false), any(ChannelPromise.class));
    verify(writer, times(1)).writeHeaders(eq(ctx), eq(streamId), eq(EmptyHttp2Headers.INSTANCE), eq(0), eq(false), eq(promise2));
    if (eos) {
        verify(writer, times(1)).writeHeaders(eq(ctx), eq(streamId), eq(EmptyHttp2Headers.INSTANCE), eq(0), eq(true), eq(promise3));
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise)

Example 57 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project netty by netty.

the class HttpProxyHandlerTest method testInitialMessage.

private static void testInitialMessage(InetSocketAddress socketAddress, String expectedUrl, String expectedHostHeader, HttpHeaders headers, boolean ignoreDefaultPortsInConnectHostHeader) throws Exception {
    InetSocketAddress proxyAddress = new InetSocketAddress(NetUtil.LOCALHOST, 8080);
    ChannelPromise promise = mock(ChannelPromise.class);
    verifyNoMoreInteractions(promise);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.connect(same(proxyAddress), isNull(InetSocketAddress.class), same(promise))).thenReturn(promise);
    HttpProxyHandler handler = new HttpProxyHandler(new InetSocketAddress(NetUtil.LOCALHOST, 8080), headers, ignoreDefaultPortsInConnectHostHeader);
    handler.connect(ctx, socketAddress, null, promise);
    FullHttpRequest request = (FullHttpRequest) handler.newInitialMessage(ctx);
    try {
        assertEquals(HttpVersion.HTTP_1_1, request.protocolVersion());
        assertEquals(expectedUrl, request.uri());
        HttpHeaders actualHeaders = request.headers();
        assertEquals(expectedHostHeader, actualHeaders.get(HttpHeaderNames.HOST));
        if (headers != null) {
            // The actual request header is a strict superset of the custom header
            for (String name : headers.names()) {
                assertEquals(headers.getAll(name), actualHeaders.getAll(name));
            }
        }
    } finally {
        request.release();
    }
    verify(ctx).connect(proxyAddress, null, promise);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) InetSocketAddress(java.net.InetSocketAddress) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext)

Example 58 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project netty by netty.

the class LocalChannel method doClose.

@Override
protected void doClose() throws Exception {
    final LocalChannel peer = this.peer;
    State oldState = state;
    try {
        if (oldState != State.CLOSED) {
            // Update all internal state before the closeFuture is notified.
            if (localAddress != null) {
                if (parent() == null) {
                    LocalChannelRegistry.unregister(localAddress);
                }
                localAddress = null;
            }
            // State change must happen before finishPeerRead to ensure writes are released either in doWrite or
            // channelRead.
            state = State.CLOSED;
            // Preserve order of event and force a read operation now before the close operation is processed.
            if (writeInProgress && peer != null) {
                finishPeerRead(peer);
            }
            ChannelPromise promise = connectPromise;
            if (promise != null) {
                // Use tryFailure() instead of setFailure() to avoid the race against cancel().
                promise.tryFailure(new ClosedChannelException());
                connectPromise = null;
            }
        }
        if (peer != null) {
            this.peer = null;
            // Always call peer.eventLoop().execute() even if peer.eventLoop().inEventLoop() is true.
            // This ensures that if both channels are on the same event loop, the peer's channelInActive
            // event is triggered *after* this peer's channelInActive event
            EventLoop peerEventLoop = peer.eventLoop();
            final boolean peerIsActive = peer.isActive();
            try {
                peerEventLoop.execute(new Runnable() {

                    @Override
                    public void run() {
                        peer.tryClose(peerIsActive);
                    }
                });
            } catch (Throwable cause) {
                logger.warn("Releasing Inbound Queues for channels {}-{} because exception occurred!", this, peer, cause);
                if (peerEventLoop.inEventLoop()) {
                    peer.releaseInboundBuffers();
                } else {
                    // inboundBuffers is a SPSC so we may leak if the event loop is shutdown prematurely or
                    // rejects the close Runnable but give a best effort.
                    peer.close();
                }
                PlatformDependent.throwException(cause);
            }
        }
    } finally {
        // Release all buffers if the Channel was already registered in the past and if it was not closed before.
        if (oldState != null && oldState != State.CLOSED) {
            // We need to release all the buffers that may be put into our inbound queue since we closed the Channel
            // to ensure we not leak any memory. This is fine as it basically gives the same guarantees as TCP which
            // means even if the promise was notified before its not really guaranteed that the "remote peer" will
            // see the buffer at all.
            releaseInboundBuffers();
        }
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) EventLoop(io.netty.channel.EventLoop) SingleThreadEventLoop(io.netty.channel.SingleThreadEventLoop) ChannelPromise(io.netty.channel.ChannelPromise)

Example 59 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project netty by netty.

the class AbstractNioChannel method doClose.

@Override
protected void doClose() throws Exception {
    ChannelPromise promise = connectPromise;
    if (promise != null) {
        // Use tryFailure() instead of setFailure() to avoid the race against cancel().
        promise.tryFailure(new ClosedChannelException());
        connectPromise = null;
    }
    Future<?> future = connectTimeoutFuture;
    if (future != null) {
        future.cancel(false);
        connectTimeoutFuture = null;
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelPromise(io.netty.channel.ChannelPromise)

Example 60 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project netty by netty.

the class WebSocketClientProtocolHandshakeHandler method applyHandshakeTimeout.

private void applyHandshakeTimeout() {
    final ChannelPromise localHandshakePromise = handshakePromise;
    if (handshakeTimeoutMillis <= 0 || localHandshakePromise.isDone()) {
        return;
    }
    final Future<?> timeoutFuture = ctx.executor().schedule(new Runnable() {

        @Override
        public void run() {
            if (localHandshakePromise.isDone()) {
                return;
            }
            if (localHandshakePromise.tryFailure(new WebSocketClientHandshakeException("handshake timed out"))) {
                ctx.flush().fireUserEventTriggered(ClientHandshakeStateEvent.HANDSHAKE_TIMEOUT).close();
            }
        }
    }, handshakeTimeoutMillis, TimeUnit.MILLISECONDS);
    // Cancel the handshake timeout when handshake is finished.
    localHandshakePromise.addListener(new FutureListener<Void>() {

        @Override
        public void operationComplete(Future<Void> f) throws Exception {
            timeoutFuture.cancel(false);
        }
    });
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise)

Aggregations

ChannelPromise (io.netty.channel.ChannelPromise)223 Test (org.junit.jupiter.api.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)63 ChannelFuture (io.netty.channel.ChannelFuture)62 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)58 ByteBuf (io.netty.buffer.ByteBuf)56 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)30 Test (org.junit.Test)25 Channel (io.netty.channel.Channel)23 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)22 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 ClosedChannelException (java.nio.channels.ClosedChannelException)20 ChannelFutureListener (io.netty.channel.ChannelFutureListener)19 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)18 InvocationOnMock (org.mockito.invocation.InvocationOnMock)18 AsciiString (io.netty.util.AsciiString)15 IOException (java.io.IOException)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 Bootstrap (io.netty.bootstrap.Bootstrap)12