feat(http2): add HTTP2 keep-alive support for client and server
This adds HTTP2 keep-alive support to client and server connections based losely on GRPC keep-alive. When enabled, after no data has been received for some configured interval, an HTTP2 PING frame is sent. If the PING is not acknowledged with a configured timeout, the connection is closed. Clients have an additional option to enable keep-alive while the connection is otherwise idle. When disabled, keep-alive PINGs are only used while there are open request/response streams. If enabled, PINGs are sent even when there are no active streams. For now, since these features use `tokio::time::Delay`, the `runtime` cargo feature is required to use them.
This commit is contained in:
		| @@ -933,6 +933,7 @@ impl Builder { | ||||
|         self.pool_config.max_idle_per_host = max_idle; | ||||
|         self | ||||
|     } | ||||
|  | ||||
|     // HTTP/1 options | ||||
|  | ||||
|     /// Set whether HTTP/1 connections should try to use vectored writes, | ||||
| @@ -1036,6 +1037,59 @@ impl Builder { | ||||
|         self | ||||
|     } | ||||
|  | ||||
|     /// Sets an interval for HTTP2 Ping frames should be sent to keep a | ||||
|     /// connection alive. | ||||
|     /// | ||||
|     /// Pass `None` to disable HTTP2 keep-alive. | ||||
|     /// | ||||
|     /// Default is currently disabled. | ||||
|     /// | ||||
|     /// # Cargo Feature | ||||
|     /// | ||||
|     /// Requires the `runtime` cargo feature to be enabled. | ||||
|     #[cfg(feature = "runtime")] | ||||
|     pub fn http2_keep_alive_interval( | ||||
|         &mut self, | ||||
|         interval: impl Into<Option<Duration>>, | ||||
|     ) -> &mut Self { | ||||
|         self.conn_builder.http2_keep_alive_interval(interval); | ||||
|         self | ||||
|     } | ||||
|  | ||||
|     /// Sets a timeout for receiving an acknowledgement of the keep-alive ping. | ||||
|     /// | ||||
|     /// If the ping is not acknowledged within the timeout, the connection will | ||||
|     /// be closed. Does nothing if `http2_keep_alive_interval` is disabled. | ||||
|     /// | ||||
|     /// Default is 20 seconds. | ||||
|     /// | ||||
|     /// # Cargo Feature | ||||
|     /// | ||||
|     /// Requires the `runtime` cargo feature to be enabled. | ||||
|     #[cfg(feature = "runtime")] | ||||
|     pub fn http2_keep_alive_timeout(&mut self, timeout: Duration) -> &mut Self { | ||||
|         self.conn_builder.http2_keep_alive_timeout(timeout); | ||||
|         self | ||||
|     } | ||||
|  | ||||
|     /// Sets whether HTTP2 keep-alive should apply while the connection is idle. | ||||
|     /// | ||||
|     /// If disabled, keep-alive pings are only sent while there are open | ||||
|     /// request/responses streams. If enabled, pings are also sent when no | ||||
|     /// streams are active. Does nothing if `http2_keep_alive_interval` is | ||||
|     /// disabled. | ||||
|     /// | ||||
|     /// Default is `false`. | ||||
|     /// | ||||
|     /// # Cargo Feature | ||||
|     /// | ||||
|     /// Requires the `runtime` cargo feature to be enabled. | ||||
|     #[cfg(feature = "runtime")] | ||||
|     pub fn http2_keep_alive_while_idle(&mut self, enabled: bool) -> &mut Self { | ||||
|         self.conn_builder.http2_keep_alive_while_idle(enabled); | ||||
|         self | ||||
|     } | ||||
|  | ||||
|     /// Set whether to retry requests that get disrupted before ever starting | ||||
|     /// to write. | ||||
|     /// | ||||
|   | ||||
		Reference in New Issue
	
	Block a user