io.netty.handler.codec.http2.Http2PingFrame Java Examples

The following examples show how to use io.netty.handler.codec.http2.Http2PingFrame. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: H2ParentConnectionContext.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
@Override
public final void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof Http2SettingsFrame) {
        if (ackSettings(ctx, (Http2SettingsFrame) msg)) {
            ctx.writeAndFlush(Http2SettingsAckFrame.INSTANCE);
        }
    } else if (msg instanceof Http2GoAwayFrame) {
        Http2GoAwayFrame goAwayFrame = (Http2GoAwayFrame) msg;
        goAwayFrame.release();
        parentContext.onClosing.onComplete();

        // We trigger the graceful close process here (with no timeout) to make sure the socket is closed once
        // the existing streams are closed. The MultiplexCodec may simulate a GOAWAY when the stream IDs are
        // exhausted so we shouldn't rely upon our peer to close the transport.
        parentContext.keepAliveManager.initiateGracefulClose(parentContext.onClosing::onComplete);
    } else if (msg instanceof Http2PingFrame) {
        parentContext.keepAliveManager.pingReceived((Http2PingFrame) msg);
    } else {
        ctx.fireChannelRead(msg);
    }
}
 
Example #2
Source File: KeepAliveManager.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
void pingReceived(final Http2PingFrame pingFrame) {
    assert channel.eventLoop().inEventLoop();

    if (pingFrame.ack()) {
        long pingAckContent = pingFrame.content();
        if (pingAckContent == GRACEFUL_CLOSE_PING_CONTENT) {
            LOGGER.debug("channel={}, graceful close ping ack received.", channel);
            cancelIfStateIsAFuture(gracefulCloseState);
            gracefulCloseWriteSecondGoAway();
        } else if (pingAckContent == KEEP_ALIVE_PING_CONTENT) {
            cancelIfStateIsAFuture(keepAliveState);
            keepAliveState = null;
        }
    } else {
        // Send an ack for the received ping
        channel.writeAndFlush(new DefaultHttp2PingFrame(pingFrame.content(), true));
    }
}
 
Example #3
Source File: KeepAliveManagerTest.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
@Test
public void keepAlivePingAckReceived() {
    KeepAliveManager manager = newManager(false);
    addActiveStream(manager);
    manager.channelIdle();
    Http2PingFrame ping = verifyWrite(instanceOf(Http2PingFrame.class));
    ScheduledTask ackTimeoutTask = verifyPingAckTimeoutScheduled();

    manager.pingReceived(new DefaultHttp2PingFrame(ping.content(), true));
    assertThat("Ping ack timeout task not cancelled.", ackTimeoutTask.promise.isCancelled(), is(true));

    ackTimeoutTask.task.run();
    verifyNoWrite();
    verifyNoScheduledTasks();
    assertThat("Channel unexpectedly closed.", channel.isOpen(), is(true));
}
 
Example #4
Source File: ServerNotRespondingTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, Http2PingFrame msg) {
    if (channelIds[0].equals(ctx.channel().id().asShortText()) && !ackPingOnFirstChannel) {
        // Not respond if this is the first request
        LOGGER.info(() -> "yolo" + ctx.channel());
    } else {
        ctx.writeAndFlush(new DefaultHttp2PingFrame(msg.content(), true));
    }
}
 
Example #5
Source File: Http2PingHandler.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, Http2PingFrame frame) {
    if (frame.ack()) {
        log.debug(() -> "Received PING ACK from channel " + ctx.channel());
        lastPingAckTime = System.currentTimeMillis();
    } else {
        ctx.fireChannelRead(frame);
    }
}
 
Example #6
Source File: KeepAliveManagerTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
private Http2PingFrame initiateGracefulCloseVerifyGoAwayAndPing(final KeepAliveManager manager) {
    manager.initiateGracefulClose(() -> { });
    Http2GoAwayFrame firstGoAway = verifyWrite(instanceOf(Http2GoAwayFrame.class));
    assertThat("Unexpected error in go_away", firstGoAway.errorCode(), is(Http2Error.NO_ERROR.code()));
    Http2PingFrame pingFrame = verifyWrite(instanceOf(Http2PingFrame.class));
    verifyNoWrite();
    return pingFrame;
}
 
Example #7
Source File: KeepAliveManagerTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
private void sendGracefulClosePingAckAndVerifySecondGoAway(final KeepAliveManager manager,
                                                           final Http2PingFrame pingFrame) throws Exception {
    ScheduledTask pingAckTimeoutTask = scheduledTasks.take();
    manager.pingReceived(new DefaultHttp2PingFrame(pingFrame.content(), true));
    assertThat("Ping ack task not cancelled.", pingAckTimeoutTask.promise.isCancelled(), is(true));
    verifySecondGoAway();

    pingAckTimeoutTask.task.run();

    verifyNoWrite();
    verifyNoScheduledTasks();
}
 
Example #8
Source File: KeepAliveManagerTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void channelClosedDuringPing() {
    KeepAliveManager manager = newManager(false);
    addActiveStream(manager);
    manager.channelIdle();
    verifyWrite(instanceOf(Http2PingFrame.class));
    ScheduledTask ackTimeoutTask = verifyPingAckTimeoutScheduled();

    manager.channelClosed();
    assertThat("Keep alive ping ack timeout not cancelled.", ackTimeoutTask.promise.isCancelled(),
            is(true));

    verifyNoOtherActionPostClose(manager);
}
 
Example #9
Source File: KeepAliveManagerTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void pingsAreAcked() {
    KeepAliveManager manager = newManager(false);
    long pingContent = ThreadLocalRandom.current().nextLong();
    manager.pingReceived(new DefaultHttp2PingFrame(pingContent, false));
    Http2PingFrame pingFrame = verifyWrite(instanceOf(Http2PingFrame.class));
    assertThat("Unexpected ping ack content.", pingFrame.content(), is(pingContent));
    assertThat("Unexpected ping ack content.", pingFrame.ack(), is(true));
}
 
Example #10
Source File: KeepAliveManagerTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void gracefulClosePendingPingsCloseConnection() throws Exception {
    KeepAliveManager manager = newManager(false);
    addActiveStream(manager);
    Http2PingFrame pingFrame = initiateGracefulCloseVerifyGoAwayAndPing(manager);

    sendGracefulClosePingAckAndVerifySecondGoAway(manager, pingFrame);
    assertThat("Channel closed.", channel.isOpen(), is(true));

    manager.channelIdle();
    verifyWrite(instanceOf(Http2PingFrame.class));
    verifyChannelCloseOnMissingPingAck(verifyPingAckTimeoutScheduled());
}
 
Example #11
Source File: KeepAliveManagerTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void gracefulCloseWithActiveStreams() throws Exception {
    KeepAliveManager manager = newManager(false);
    EmbeddedChannel activeStream = addActiveStream(manager);
    Http2PingFrame pingFrame = initiateGracefulCloseVerifyGoAwayAndPing(manager);

    sendGracefulClosePingAckAndVerifySecondGoAway(manager, pingFrame);

    assertThat("Channel not closed.", channel.isOpen(), is(true));
    activeStream.close().sync().await();

    channel.closeFuture().sync().await();
}
 
Example #12
Source File: KeepAliveManagerTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void gracefulCloseNoActiveStreams() throws Exception {
    KeepAliveManager manager = newManager(false);
    Http2PingFrame pingFrame = initiateGracefulCloseVerifyGoAwayAndPing(manager);

    sendGracefulClosePingAckAndVerifySecondGoAway(manager, pingFrame);

    channel.closeFuture().sync().await();
}
 
Example #13
Source File: KeepAliveManagerTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void keepAliveMissingPingAck() {
    KeepAliveManager manager = newManager(false);
    addActiveStream(manager);
    manager.channelIdle();
    verifyWrite(instanceOf(Http2PingFrame.class));
    verifyChannelCloseOnMissingPingAck(verifyPingAckTimeoutScheduled());
}
 
Example #14
Source File: KeepAliveManagerTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void keepAlivePingAckWithUnknownContent() {
    KeepAliveManager manager = newManager(false);
    addActiveStream(manager);
    manager.channelIdle();
    Http2PingFrame ping = verifyWrite(instanceOf(Http2PingFrame.class));
    ScheduledTask ackTimeoutTask = verifyPingAckTimeoutScheduled();

    manager.pingReceived(new DefaultHttp2PingFrame(ping.content() + 1, true));
    assertThat("Ping ack timeout task cancelled.", ackTimeoutTask.promise.isCancelled(), is(false));

    verifyChannelCloseOnMissingPingAck(ackTimeoutTask);
}
 
Example #15
Source File: KeepAliveManagerTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void keepAliveWithActiveStreams() {
    KeepAliveManager manager = newManager(false);
    addActiveStream(manager);
    manager.channelIdle();
    verifyWrite(instanceOf(Http2PingFrame.class));
}
 
Example #16
Source File: Http2PingHandlerTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, Http2PingFrame msg) {
    caughtPings.add(msg);
}
 
Example #17
Source File: KeepAliveManagerTest.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
@Test
public void keepAliveAllowedWithNoActiveStreams() {
    KeepAliveManager manager = newManager(true);
    manager.channelIdle();
    verifyWrite(instanceOf(Http2PingFrame.class));
}