1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.http.nio.netty.internal.http2; 17 18 import io.netty.buffer.ByteBuf; 19 import io.netty.channel.Channel; 20 import io.netty.handler.codec.http2.Http2ConnectionAdapter; 21 import io.netty.handler.codec.http2.Http2GoAwayFrame; 22 import java.nio.charset.StandardCharsets; 23 import software.amazon.awssdk.annotations.SdkInternalApi; 24 import software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey; 25 import software.amazon.awssdk.http.nio.netty.internal.utils.NettyClientLogger; 26 27 /** 28 * Handles {@link Http2GoAwayFrame}s sent on a connection. This will pass the frame along to the connection's 29 * {@link Http2MultiplexedChannelPool#handleGoAway(Channel, int, GoAwayException)}. 30 */ 31 @SdkInternalApi 32 public final class Http2GoAwayEventListener extends Http2ConnectionAdapter { 33 private static final NettyClientLogger log = NettyClientLogger.getLogger(Http2GoAwayEventListener.class); 34 35 private final Channel parentChannel; 36 Http2GoAwayEventListener(Channel parentChannel)37 public Http2GoAwayEventListener(Channel parentChannel) { 38 this.parentChannel = parentChannel; 39 } 40 41 42 @Override onGoAwayReceived(int lastStreamId, long errorCode, ByteBuf debugData)43 public void onGoAwayReceived(int lastStreamId, long errorCode, ByteBuf debugData) { 44 Http2MultiplexedChannelPool channelPool = parentChannel.attr(ChannelAttributeKey.HTTP2_MULTIPLEXED_CHANNEL_POOL).get(); 45 GoAwayException exception = new GoAwayException(errorCode, debugData.toString(StandardCharsets.UTF_8)); 46 if (channelPool != null) { 47 channelPool.handleGoAway(parentChannel, lastStreamId, exception); 48 } else { 49 log.warn(parentChannel, () -> "GOAWAY received on a connection (" + parentChannel + ") not associated with any " 50 + "multiplexed " 51 + "channel pool."); 52 parentChannel.pipeline().fireExceptionCaught(exception); 53 } 54 } 55 } 56