Skip to content

Commit a4102de

Browse files
authored
Enable auto reads on idling connections (#4655)
This change enables auto reads on idling connections, and disables them on in-use connections (no auto read for in-use connections is existing behavior). Auto reads while the channel is idling allows the channel to read and immediately handle events that are triggered by the remote end such as TLS close notifies. In particular this fixes issues where a channel is leased only for the SDK to find that the remote end has initiated a shutdown of the TLS connection.
1 parent 6a4a077 commit a4102de

File tree

6 files changed

+134
-3
lines changed

6 files changed

+134
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "Netty NIO HTTP Client",
4+
"contributor": "",
5+
"description": "Fix an issue where data received on a channel while it was idling was not handled until the channel was leased again for a request. This caused issues such as late notification of channel closes, manifesting as channels being closed at the beginning of a request."
6+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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;
17+
18+
import io.netty.channel.Channel;
19+
import io.netty.channel.ChannelHandler;
20+
import software.amazon.awssdk.annotations.SdkInternalApi;
21+
22+
/**
23+
* Disables auto read on in-use channels to allow upper layers to take care of flow control.
24+
*/
25+
@SdkInternalApi
26+
@ChannelHandler.Sharable
27+
public final class AutoReadDisableChannelPoolListener implements ListenerInvokingChannelPool.ChannelPoolListener {
28+
private static final AutoReadDisableChannelPoolListener INSTANCE = new AutoReadDisableChannelPoolListener();
29+
30+
private AutoReadDisableChannelPoolListener() {
31+
}
32+
33+
@Override
34+
public void channelAcquired(Channel channel) {
35+
channel.config().setAutoRead(false);
36+
}
37+
38+
public static AutoReadDisableChannelPoolListener create() {
39+
return INSTANCE;
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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;
17+
18+
import io.netty.channel.Channel;
19+
import io.netty.channel.ChannelHandler;
20+
import software.amazon.awssdk.annotations.SdkInternalApi;
21+
22+
/**
23+
* Enables auto read on idle channels so that any data that a service sends while it's idling can be handled.
24+
*/
25+
@SdkInternalApi
26+
@ChannelHandler.Sharable
27+
public final class AutoReadEnableChannelPoolListener implements ListenerInvokingChannelPool.ChannelPoolListener {
28+
private static final AutoReadEnableChannelPoolListener INSTANCE = new AutoReadEnableChannelPoolListener();
29+
30+
private AutoReadEnableChannelPoolListener() {
31+
}
32+
33+
@Override
34+
public void channelReleased(Channel channel) {
35+
channel.config().setAutoRead(true);
36+
}
37+
38+
public static AutoReadEnableChannelPoolListener create() {
39+
return INSTANCE;
40+
}
41+
}

http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/internal/AwaitCloseChannelPoolMap.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,19 @@ private SdkChannelPool wrapBaseChannelPool(Bootstrap bootstrap, ChannelPool chan
239239
configuration.maxConnections(),
240240
configuration);
241241

242+
242243
sdkChannelPool = new ListenerInvokingChannelPool(bootstrap.config().group(), sdkChannelPool, Arrays.asList(
244+
// Add a listener that disables auto reads on acquired connections.
245+
AutoReadDisableChannelPoolListener.create(),
246+
243247
// Add a listener that ensures acquired channels are marked IN_USE and thus not eligible for certain idle timeouts.
244248
InUseTrackingChannelPoolListener.create(),
245249

246250
// Add a listener that removes request-specific handlers with each request.
247-
HandlerRemovingChannelPoolListener.create()
251+
HandlerRemovingChannelPoolListener.create(),
252+
253+
// Add a listener that enables auto reads on released connections.
254+
AutoReadEnableChannelPoolListener.create()
248255
));
249256

250257
// Wrap the channel pool such that an individual channel can only be released to the underlying pool once.

http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/internal/NettyRequestExecutor.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import io.netty.buffer.ByteBuf;
3232
import io.netty.buffer.Unpooled;
3333
import io.netty.channel.Channel;
34-
import io.netty.channel.ChannelOption;
3534
import io.netty.channel.ChannelPipeline;
3635
import io.netty.handler.codec.DecoderResult;
3736
import io.netty.handler.codec.http.DefaultHttpContent;
@@ -199,7 +198,6 @@ private void configureChannel() {
199198
channel.attr(RESPONSE_CONTENT_LENGTH).set(null);
200199
channel.attr(RESPONSE_DATA_READ).set(null);
201200
channel.attr(CHANNEL_DIAGNOSTICS).get().incrementRequestCount();
202-
channel.config().setOption(ChannelOption.AUTO_READ, false);
203201
}
204202

205203
private void configurePipeline() throws IOException {

http-clients/netty-nio-client/src/test/java/software/amazon/awssdk/http/nio/netty/internal/AwaitCloseChannelPoolMapTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,42 @@ public void usesProvidedKeyManagersProvider() {
268268
verify(provider).keyManagers();
269269
}
270270

271+
@Test
272+
public void acquireChannel_autoReadDisabled() {
273+
channelPoolMap = AwaitCloseChannelPoolMap.builder()
274+
.sdkChannelOptions(new SdkChannelOptions())
275+
.sdkEventLoopGroup(SdkEventLoopGroup.builder().build())
276+
.configuration(new NettyConfiguration(GLOBAL_HTTP_DEFAULTS))
277+
.protocol(Protocol.HTTP1_1)
278+
.maxStreams(100)
279+
.sslProvider(SslProvider.OPENSSL)
280+
.build();
281+
282+
ChannelPool channelPool = channelPoolMap.newPool(URI.create("https://localhost:" + mockProxy.port()));
283+
284+
Channel channel = channelPool.acquire().awaitUninterruptibly().getNow();
285+
286+
assertThat(channel.config().isAutoRead()).isFalse();
287+
}
288+
289+
@Test
290+
public void releaseChannel_autoReadEnabled() {
291+
channelPoolMap = AwaitCloseChannelPoolMap.builder()
292+
.sdkChannelOptions(new SdkChannelOptions())
293+
.sdkEventLoopGroup(SdkEventLoopGroup.builder().build())
294+
.configuration(new NettyConfiguration(GLOBAL_HTTP_DEFAULTS))
295+
.protocol(Protocol.HTTP1_1)
296+
.maxStreams(100)
297+
.sslProvider(SslProvider.OPENSSL)
298+
.build();
299+
300+
ChannelPool channelPool = channelPoolMap.newPool(URI.create("https://localhost:" + mockProxy.port()));
301+
302+
Channel channel = channelPool.acquire().awaitUninterruptibly().getNow();
303+
304+
channelPool.release(channel).awaitUninterruptibly();
305+
306+
assertThat(channel.config().isAutoRead()).isTrue();
307+
}
308+
271309
}

0 commit comments

Comments
 (0)