Skip to content
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

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

Open
wants to merge 21 commits into
base: 1.2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3193c8f
Support configuring the Ping frame interval in the HTTP/2 protocol
raccoonback Feb 2, 2025
594a914
Add a handler to check connection status via Ping frame health checks…
raccoonback Feb 2, 2025
94301bd
Update doc related with HTTP/2
raccoonback Feb 2, 2025
b046066
Ensure backward compatibility while adding support for configuring th…
raccoonback Feb 2, 2025
c6b9206
Fix unnecessary warnings in test code
raccoonback Feb 2, 2025
09da411
Http2ChannelDuplexHandler -> ChannelDuplexHandler
raccoonback Feb 2, 2025
868456a
Reduce the scope of access control
raccoonback Feb 2, 2025
a31ddef
Fix broken test code
raccoonback Feb 2, 2025
9575177
Change comparison type for PING frame
raccoonback Feb 4, 2025
c3d91fb
Fix broken test
raccoonback Feb 5, 2025
8c4cdbc
Add configuration for HTTP/2 PING scheduler interval and retry threshold
raccoonback Feb 6, 2025
9da0299
Support HTTP health check in IdleTimeoutHandler via HTTP/2 PING
raccoonback Mar 14, 2025
e06e6f0
Rollback previous Http2ConnectionLivenessHandler settings
raccoonback Mar 14, 2025
355be4e
Fix checkstyle
raccoonback Mar 14, 2025
6d42da4
Rollback docs related with HTTP2 Ping
raccoonback Mar 14, 2025
fd15454
Fix broken test
raccoonback Mar 16, 2025
e5e9df7
Update docs about HTTP2 Ping frame and IdleTimeout
raccoonback Mar 17, 2025
e79694d
Add override annotation
raccoonback Mar 17, 2025
d5a0263
Disable checkstyle about channel close
raccoonback Mar 17, 2025
965513d
Fix broken docs
raccoonback Mar 17, 2025
4bf0930
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
Expand Up @@ -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.
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.
Expand Down
29 changes: 29 additions & 0 deletions docs/modules/ROOT/pages/http-server.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
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