Skip to content

Commit 732749f

Browse files
committed
Make the PING health check period configurable
1 parent bd349d9 commit 732749f

File tree

5 files changed

+308
-1
lines changed

5 files changed

+308
-1
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
package software.amazon.awssdk.http.nio.netty;
1717

18+
import java.time.Duration;
19+
1820
import software.amazon.awssdk.annotations.SdkPublicApi;
1921
import software.amazon.awssdk.utils.Validate;
2022
import software.amazon.awssdk.utils.builder.CopyableBuilder;
@@ -27,10 +29,12 @@
2729
public final class Http2Configuration implements ToCopyableBuilder<Http2Configuration.Builder, Http2Configuration> {
2830
private final Long maxStreams;
2931
private final Integer initialWindowSize;
32+
private final Duration healthCheckPingPeriod;
3033

3134
private Http2Configuration(DefaultBuilder builder) {
3235
this.maxStreams = builder.maxStreams;
3336
this.initialWindowSize = builder.initialWindowSize;
37+
this.healthCheckPingPeriod = builder.healthCheckPingPeriod;
3438
}
3539

3640
/**
@@ -47,6 +51,13 @@ public Integer initialWindowSize() {
4751
return initialWindowSize;
4852
}
4953

54+
/**
55+
* @return The health check period for an HTTP/2 connection.
56+
*/
57+
public Duration healthCheckPingPeriod() {
58+
return healthCheckPingPeriod;
59+
}
60+
5061
@Override
5162
public Builder toBuilder() {
5263
return new DefaultBuilder(this);
@@ -106,18 +117,31 @@ public interface Builder extends CopyableBuilder<Builder, Http2Configuration> {
106117
* @return This builder for method chaining.
107118
*/
108119
Builder initialWindowSize(Integer initialWindowSize);
120+
121+
/**
122+
* Sets the period that the Netty client will send {@code PING} frames to the remote endpoint to check the
123+
* health of the connection. The default value is {@link
124+
* software.amazon.awssdk.http.nio.netty.internal.NettyConfiguration#HTTP2_CONNECTION_PING_TIMEOUT_SECONDS}. To
125+
* disable this feature, set a duration of {@link Integer#MAX_VALUE} milliseconds or greater.
126+
*
127+
* @param healthCheckPingPeriod The ping period.
128+
* @return This builder for method chaining.
129+
*/
130+
Builder healthCheckPingPeriod(Duration healthCheckPingPeriod);
109131
}
110132

111133
private static final class DefaultBuilder implements Builder {
112134
private Long maxStreams;
113135
private Integer initialWindowSize;
136+
private Duration healthCheckPingPeriod;
114137

115138
private DefaultBuilder() {
116139
}
117140

118141
private DefaultBuilder(Http2Configuration http2Configuration) {
119142
this.maxStreams = http2Configuration.maxStreams;
120143
this.initialWindowSize = http2Configuration.initialWindowSize;
144+
this.healthCheckPingPeriod = http2Configuration.healthCheckPingPeriod;
121145
}
122146

123147
@Override
@@ -140,6 +164,16 @@ public void setInitialWindowSize(Integer initialWindowSize) {
140164
initialWindowSize(initialWindowSize);
141165
}
142166

167+
@Override
168+
public Builder healthCheckPingPeriod(Duration healthCheckPingPeriod) {
169+
this.healthCheckPingPeriod = healthCheckPingPeriod;
170+
return this;
171+
}
172+
173+
public void setHealthCheckPingPeriod(Duration healthCheckPingPeriod) {
174+
healthCheckPingPeriod(healthCheckPingPeriod);
175+
}
176+
143177
@Override
144178
public Http2Configuration build() {
145179
return new Http2Configuration(this);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ private NettyNioAsyncHttpClient(DefaultBuilder builder, AttributeMap serviceDefa
106106
.protocol(protocol)
107107
.maxStreams(maxStreams)
108108
.initialWindowSize(initialWindowSize)
109+
.healthCheckPingPeriod(resolveHealthCheckPingPeriod(http2Configuration))
109110
.sdkEventLoopGroup(sdkEventLoopGroup)
110111
.sslProvider(resolveSslProvider(builder))
111112
.proxyConfiguration(builder.proxyConfiguration)
@@ -176,6 +177,13 @@ private int resolveInitialWindowSize(Http2Configuration http2Configuration) {
176177
return http2Configuration.initialWindowSize();
177178
}
178179

180+
private Duration resolveHealthCheckPingPeriod(Http2Configuration http2Configuration) {
181+
if (http2Configuration != null) {
182+
return http2Configuration.healthCheckPingPeriod();
183+
}
184+
return null;
185+
}
186+
179187
private SdkEventLoopGroup nonManagedEventLoopGroup(SdkEventLoopGroup eventLoopGroup) {
180188
return SdkEventLoopGroup.create(new NonManagedEventLoopGroup(eventLoopGroup.eventLoopGroup()),
181189
eventLoopGroup.channelFactory());

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.net.InetSocketAddress;
3232
import java.net.URI;
3333
import java.net.URISyntaxException;
34+
import java.time.Duration;
3435
import java.util.Collection;
3536
import java.util.Map;
3637
import java.util.concurrent.CompletableFuture;
@@ -81,6 +82,7 @@ public void channelCreated(Channel ch) throws Exception {
8182
private final NettyConfiguration configuration;
8283
private final Protocol protocol;
8384
private final long maxStreams;
85+
private final Duration healthCheckPingPeriod;
8486
private final int initialWindowSize;
8587
private final SslProvider sslProvider;
8688
private final ProxyConfiguration proxyConfiguration;
@@ -91,6 +93,7 @@ private AwaitCloseChannelPoolMap(Builder builder) {
9193
this.configuration = builder.configuration;
9294
this.protocol = builder.protocol;
9395
this.maxStreams = builder.maxStreams;
96+
this.healthCheckPingPeriod = builder.healthCheckPingPeriod;
9497
this.initialWindowSize = builder.initialWindowSize;
9598
this.sslProvider = builder.sslProvider;
9699
this.proxyConfiguration = builder.proxyConfiguration;
@@ -118,6 +121,7 @@ protected SimpleChannelPoolAwareChannelPool newPool(URI key) {
118121
sslContext,
119122
maxStreams,
120123
initialWindowSize,
124+
healthCheckPingPeriod,
121125
channelPoolRef,
122126
configuration,
123127
key);
@@ -297,6 +301,7 @@ public static class Builder {
297301
private Protocol protocol;
298302
private long maxStreams;
299303
private int initialWindowSize;
304+
private Duration healthCheckPingPeriod;
300305
private SslProvider sslProvider;
301306
private ProxyConfiguration proxyConfiguration;
302307

@@ -333,6 +338,11 @@ public Builder initialWindowSize(int initialWindowSize) {
333338
return this;
334339
}
335340

341+
public Builder healthCheckPingPeriod(Duration healthCheckPingPeriod) {
342+
this.healthCheckPingPeriod = healthCheckPingPeriod;
343+
return this;
344+
}
345+
336346
public Builder sslProvider(SslProvider sslProvider) {
337347
this.sslProvider = sslProvider;
338348
return this;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import io.netty.handler.ssl.SslContext;
3636
import io.netty.handler.ssl.SslHandler;
3737
import java.net.URI;
38+
import java.time.Duration;
3839
import java.util.concurrent.CompletableFuture;
3940
import java.util.concurrent.atomic.AtomicReference;
4041
import javax.net.ssl.SSLEngine;
@@ -54,6 +55,7 @@ public final class ChannelPipelineInitializer extends AbstractChannelPoolHandler
5455
private final SslContext sslCtx;
5556
private final long clientMaxStreams;
5657
private final int clientInitialWindowSize;
58+
private final Duration healthCheckPingPeriod;
5759
private final AtomicReference<ChannelPool> channelPoolRef;
5860
private final NettyConfiguration configuration;
5961
private final URI poolKey;
@@ -62,13 +64,15 @@ public ChannelPipelineInitializer(Protocol protocol,
6264
SslContext sslCtx,
6365
long clientMaxStreams,
6466
int clientInitialWindowSize,
67+
Duration healthCheckPingPeriod,
6568
AtomicReference<ChannelPool> channelPoolRef,
6669
NettyConfiguration configuration,
6770
URI poolKey) {
6871
this.protocol = protocol;
6972
this.sslCtx = sslCtx;
7073
this.clientMaxStreams = clientMaxStreams;
7174
this.clientInitialWindowSize = clientInitialWindowSize;
75+
this.healthCheckPingPeriod = healthCheckPingPeriod;
7276
this.channelPoolRef = channelPoolRef;
7377
this.configuration = configuration;
7478
this.poolKey = poolKey;
@@ -141,7 +145,11 @@ private void configureHttp2(Channel ch, ChannelPipeline pipeline) {
141145
pipeline.addLast(codec);
142146
pipeline.addLast(new Http2MultiplexHandler(new NoOpChannelInitializer()));
143147
pipeline.addLast(new Http2SettingsFrameHandler(ch, clientMaxStreams, channelPoolRef));
144-
pipeline.addLast(new Http2PingHandler(HTTP2_CONNECTION_PING_TIMEOUT_SECONDS * 1_000));
148+
if (healthCheckPingPeriod == null) {
149+
pipeline.addLast(new Http2PingHandler(HTTP2_CONNECTION_PING_TIMEOUT_SECONDS * 1_000));
150+
} else if (healthCheckPingPeriod.toMillis() <= Integer.MAX_VALUE) {
151+
pipeline.addLast(new Http2PingHandler((int) healthCheckPingPeriod.toMillis()));
152+
}
145153
}
146154

147155
private void configureHttp11(Channel ch, ChannelPipeline pipeline) {

0 commit comments

Comments
 (0)