Skip to content

Commit

Permalink
fix: graceful shutdown after switch to hyper 1.x (#779)
Browse files Browse the repository at this point in the history
For some reason it was removed after switch to `hyper 1.x` ([commit](0b6ca89))

<details>
<summary> Patch affected the behavior </summary>

```patch
-    let mut conn = Http::new()
-        .serve_connection(socket, service)
-        .with_upgrades();
+    let builder = auto::Builder::new(hyper_util::rt::TokioExecutor::new());
+    let conn =
+        builder.serve_connection_with_upgrades(hyper_util::rt::TokioIo::new(socket), service);
+    futures_util::pin_mut!(conn);

     tokio::select! {
-        _ = &mut conn => {
+        _ = conn => {
             // Connection completed successfully.
             return;
         },
@@ -366,10 +368,4 @@
         }
         _ = server_graceful_shutdown_token.cancelled() => {}
     }
-
-    // Init graceful shutdown for connection (`GOAWAY` for `HTTP/2` or disabling `keep-alive` for `HTTP/1`)
-    Pin::new(&mut conn).graceful_shutdown();
-
-    // Continue awaiting after graceful-shutdown is initiated to handle existed requests.
-    let _ = conn.await;
```

</details>
  • Loading branch information
DDtKey authored Mar 27, 2024
1 parent a08032e commit 6484545
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion poem/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,17 @@ async fn serve_connection(
futures_util::pin_mut!(conn);

tokio::select! {
_ = conn => {
_ = &mut conn => {
// Connection completed successfully.
},
_ = connection_shutdown_token.cancelled() => {
tracing::info!(remote_addr=%remote_addr, "closing connection due to inactivity");
}
_ = server_graceful_shutdown_token.cancelled() => {}
}

// Init graceful shutdown for connection
conn.as_mut().graceful_shutdown();
// Continue awaiting after graceful-shutdown is initiated to handle existed requests.
let _ = conn.await;
}

0 comments on commit 6484545

Please sign in to comment.