Skip to content

Support a handler for checking connection status using Ping frame in HTTP/2 #3612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: 1.2.x
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
11f27c0
Support configuring the Ping frame interval in the HTTP/2 protocol
raccoonback Feb 2, 2025
332bab0
Add a handler to check connection status via Ping frame health checks…
raccoonback Feb 2, 2025
362a54b
Update doc related with HTTP/2
raccoonback Feb 2, 2025
5fc0e8e
Ensure backward compatibility while adding support for configuring th…
raccoonback Feb 2, 2025
e795731
Fix unnecessary warnings in test code
raccoonback Feb 2, 2025
54a186b
Http2ChannelDuplexHandler -> ChannelDuplexHandler
raccoonback Feb 2, 2025
68cf596
Reduce the scope of access control
raccoonback Feb 2, 2025
7e27e22
Fix broken test code
raccoonback Feb 2, 2025
fd2a918
Change comparison type for PING frame
raccoonback Feb 4, 2025
adb5fb5
Fix broken test
raccoonback Feb 5, 2025
5de6219
Add configuration for HTTP/2 PING scheduler interval and retry threshold
raccoonback Feb 6, 2025
c3dc94b
Support HTTP health check in IdleTimeoutHandler via HTTP/2 PING
raccoonback Mar 14, 2025
2efb7ff
Rollback previous Http2ConnectionLivenessHandler settings
raccoonback Mar 14, 2025
b49b5d6
Fix checkstyle
raccoonback Mar 14, 2025
2368444
Rollback docs related with HTTP2 Ping
raccoonback Mar 14, 2025
ed8e74b
Fix broken test
raccoonback Mar 16, 2025
34c7052
Update docs about HTTP2 Ping frame and IdleTimeout
raccoonback Mar 17, 2025
9570a38
Add override annotation
raccoonback Mar 17, 2025
666aa21
Disable checkstyle about channel close
raccoonback Mar 17, 2025
bcf82f4
Fix broken docs
raccoonback Mar 17, 2025
8c75204
Stop scheduler when data is received, considering the connection active
raccoonback Mar 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions docs/modules/ROOT/pages/http-client.adoc
Original file line number Diff line number Diff line change
@@ -680,6 +680,50 @@ include::{examples-dir}/channeloptions/Application.java[lines=18..54]
<4> Configures the time between individual `keepalive` probes to 1 minute.
<5> Configures the maximum number of TCP `keepalive` probes to 8.


==== IdleTimeout
The maximum time (resolution: ms) that this connection stays opened and waits for HTTP request. Once the timeout is reached, the connection is closed.
Copy link
Member

@violetagg violetagg Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The maximum time (resolution: ms) that this connection stays opened and waits for HTTP request

Can you please elaborate more on this?

This is HttpClient so it initiates the HTTP request. (May be you mean the connection stays idle in the connection pool?) As part of the connection pool we already have a configuration for dropping the idle connections so this new configuration seems to be not needed? (check Http2Pool)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@violetagg
This describes configuring IdleTimeout on the HttpClient side using IdleTimeoutHandler, independently of the connection pool settings.

From my understanding, since the connection pool already manages idle connections, applying an additional IdleTimeoutHandler at the HttpClient level may be redundant.
Could you please confirm if this understanding is accurate?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I think it is redundant

By default, `idleTimeout` is not specified, this indicates no timeout (i.e. infinite), which means the connection is closed only if one of the peers decides to close it explicitly.

NOTE: It is always a good practice to configure an idle timeout.

To customize the default settings, you can configure `HttpClient` as follows:

{examples-link}/idle/timeout/Application.java
----
include::{examples-dir}/idle/timeout/Application.java[lines=26..44]
----
<1> Configures the default idle timeout to 1 second.

===== Configuring Ping Frame for HTTP/2 Health Check
When using the HTTP/2 protocol, it is recommended to configure a *PING* frame to maintain the connection and ensure timely health checks.
The HttpClient in Reactor Netty allows setting up a *PING* frame to prevent idle connections from being prematurely closed when *idleTimeout* is configured.

[NOTE]
====
To enable HTTP/2 PING frame-based health checks, you must configure `idleTimeout`.
Without `idleTimeout`, the connection may remain open indefinitely, preventing proper detection of inactive or unresponsive connections.
Setting an appropriate `idleTimeout` ensures that PING-based health checks can effectively terminate unresponsive connections.
====

*Benefits of Using PING Frames*

- Actively monitors connection health by checking real-time responses to *PING* frames.
- Ensures that health checks detect unresponsive connections quickly.
- Helps maintain long-lived connections in an efficient manner.

To enable *PING* frames for HTTP/2 connections, configure the HttpClient as follows:

{examples-link}/liveness/Application.java
----
include::{examples-dir}/liveness/Application.java[lines=26..49]
----
<1> To set up a health check using HTTP2 Ping frame, `idleTimeout` must be set first.
<2> Sets the interval for sending `HTTP/2` `PING` frames and receiving `ACK` responses
<3> Sets the execution interval for the scheduler that sends `HTTP/2` `PING frames and periodically checks for `ACK` responses
<4> Sets the threshold for retrying `HTTP/2` `PING` frame transmissions.


[[ssl-tls-timeout]]
==== SSL/TLS Timeout
`HttpClient` supports the SSL/TLS functionality provided by Netty.
29 changes: 29 additions & 0 deletions docs/modules/ROOT/pages/http-server.adoc
Original file line number Diff line number Diff line change
@@ -726,6 +726,35 @@ include::{examples-dir}/idle/timeout/Application.java[lines=18..35]
----
<1> Configures the default idle timeout to 1 second.

==== Configuring Ping Frame for HTTP/2 Health Check
When using the HTTP/2 protocol, it is recommended to configure a *PING* frame to maintain the connection and ensure timely health checks.
The HttpServer in Reactor Netty allows setting up a *PING* frame to prevent idle connections from being prematurely closed when *idleTimeout* is configured.

[NOTE]
====
To enable HTTP/2 PING frame-based health checks, you must configure `idleTimeout`.
Without `idleTimeout`, the connection may remain open indefinitely, preventing proper detection of inactive or unresponsive connections.
Setting an appropriate `idleTimeout` ensures that PING-based health checks can effectively terminate unresponsive connections.
====

*Benefits of Using PING Frames*

- Actively monitors connection health by checking real-time responses to *PING* frames.
- Ensures that health checks detect unresponsive connections quickly.
- Helps maintain long-lived connections in an efficient manner.

To enable *PING* frames for HTTP/2 connections, configure the HttpServer as follows:

{examples-link}/liveness/Application.java
----
include::{examples-dir}/liveness/Application.java[lines=26..50]
----
<1> To set up a health check using HTTP2 Ping frame, `idleTimeout` must be set first.
<2> Sets the interval for sending `HTTP/2` `PING` frames and receiving `ACK` responses
<3> Sets the execution interval for the scheduler that sends `HTTP/2` `PING frames and periodically checks for `ACK` responses
<4> Sets the threshold for retrying `HTTP/2` `PING` frame transmissions.


[[http-server-ssl-tls-timeout]]
=== SSL/TLS Timeout
`HttpServer` supports the SSL/TLS functionality provided by Netty.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2025 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty.examples.documentation.http.client.idle.timeout;

import io.netty.handler.codec.http.HttpHeaders;
import reactor.core.publisher.Mono;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.client.HttpClient;
import reactor.util.function.Tuple2;

import java.time.Duration;

public class Application {

public static void main(String[] args) {
HttpClient client =
HttpClient.create()
.protocol(HttpProtocol.H2)
.secure()
.idleTimeout(Duration.ofSeconds(1)); //<1>

Tuple2<String, HttpHeaders> response =
client.get()
.uri("https://example.com/")
.responseSingle((res, bytes) -> bytes.asString()
.zipWith(Mono.just(res.responseHeaders())))
.block();
System.out.println("Used stream ID: " + response.getT2().get("x-http2-stream-id"));
System.out.println("Response: " + response.getT1());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2025 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty.examples.documentation.http.client.liveness;

import io.netty.handler.codec.http.HttpHeaders;
import reactor.core.publisher.Mono;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.client.HttpClient;
import reactor.util.function.Tuple2;

import java.time.Duration;

public class Application {

public static void main(String[] args) {
HttpClient client =
HttpClient.create()
.protocol(HttpProtocol.H2)
.secure()
.idleTimeout(Duration.ofSeconds(1)) //<1>
.http2Settings(
builder -> builder.pingAckTimeout(Duration.ofMillis(600)) // <2>
.pingScheduleInterval(Duration.ofMillis(300)) // <3>
.pingAckDropThreshold(2) // <4>
);

Tuple2<String, HttpHeaders> response =
client.get()
.uri("https://example.com/")
.responseSingle((res, bytes) -> bytes.asString()
.zipWith(Mono.just(res.responseHeaders())))
.block();
System.out.println("Used stream ID: " + response.getT2().get("x-http2-stream-id"));
System.out.println("Response: " + response.getT1());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2025 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty.examples.documentation.http.server.liveness;

import reactor.netty.DisposableServer;
import reactor.netty.http.Http2SslContextSpec;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.server.HttpServer;

import java.io.File;
import java.time.Duration;

public class Application {

public static void main(String[] args) {
File cert = new File("certificate.crt");
File key = new File("private.key");

Http2SslContextSpec http2SslContextSpec = Http2SslContextSpec.forServer(cert, key);

DisposableServer server =
HttpServer.create()
.port(8080)
.protocol(HttpProtocol.H2)
.secure(spec -> spec.sslContext(http2SslContextSpec))
.idleTimeout(Duration.ofSeconds(1)) //<1>
.http2Settings(
builder -> builder.pingAckTimeout(Duration.ofMillis(600)) // <2>
.pingScheduleInterval(Duration.ofMillis(300)) // <3>
.pingAckDropThreshold(2) // <4>
)
.bindNow();

server.onDispose()
.block();
}
}
Loading