0

我正在用 Rust 编写一个带有 warp 的服务。当服务收到 SIGTERM 信号时,我想让它正常关闭,并可能做一些日志记录或其他工作。

我尝试了许多示例,但没有任何效果。最有希望的似乎是这个链接:

https://github.com/seanmonstar/warp/issues/195

但我似乎无法让它发挥作用。我怀疑自从回答这个问题后事情已经发生了变化。

这是我的货物文件:

[package]
name = "warp-shutdown"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = {version = "1", features = ["full"]}
warp = "0.3"
futures = "0.3"

这是我的 main.rs:

use warp::Filter;
use futures;

fn main() {
    let (tx, rx) = tokio::sync::oneshot::channel();
    tokio::run(futures::future::lazy(move || {
        let routes = warp::any().map(|| "Hello, World!");
        let (_, server) = warp::serve(routes)
            .bind_with_graceful_shutdown(([127, 0, 0, 1], 3030), rx);
        warp::spawn(server);
    }));

    println!("Exiting!");
}

在这一点上,它甚至没有编译。我收到几个看似无关的错误:

error[E0425]: cannot find function `run` in crate `tokio`
 --> src/main.rs:6:12
  |
6 |     tokio::run(futures::future::lazy(move || {
  |            ^^^ not found in `tokio`

error[E0425]: cannot find function `spawn` in crate `warp`
  --> src/main.rs:10:15
   |
10 |         warp::spawn(server);
   |               ^^^^^ not found in `warp`
   |
help: consider importing one of these items
   |
1  | use std::thread::spawn;
   |
1  | use tokio::spawn;
   |

error[E0593]: closure is expected to take 1 argument, but it takes 0 arguments
 --> src/main.rs:6:16
  |
6 |     tokio::run(futures::future::lazy(move || {
  |                ^^^^^^^^^^^^^^^^^^^^^ ------- takes 0 arguments
  |                |
  |                expected closure that takes 1 argument
  |
help: consider changing the closure to take and ignore the expected argument
  |
6 |     tokio::run(futures::future::lazy(move |_| {
  |                                           ~~~

error[E0271]: type mismatch resolving `<tokio::sync::oneshot::Receiver<_> as warp::Future>::Output == ()`
   --> src/main.rs:9:14
    |
9   |             .bind_with_graceful_shutdown(([127, 0, 0, 1], 3030), rx);
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found enum `Result`
    |
    = note: expected unit type `()`
                    found enum `Result<_, tokio::sync::oneshot::error::RecvError>`
note: required by a bound in `warp::Server::<F>::bind_with_graceful_shutdown`
   --> /Users/stephen.gibson/.cargo/registry/src/github.com-1ecc6299db9ec823/warp-0.3.2/src/server.rs:281:29
    |
281 |         signal: impl Future<Output = ()> + Send + 'static,
    |                             ^^^^^^^^^^^ required by this bound in `warp::Server::<F>::bind_with_graceful_shutdown`

Some errors have detailed explanations: E0271, E0425, E0593.

任何建议或更好的更新代码将不胜感激。

谢谢。

4

2 回答 2

0

这是有效的示例代码。我受到bind_with_graceful_shutdown的包装文档的启发

use tokio::sync::oneshot;
use warp::Filter;

#[tokio::main]
async fn main() {
    let routes = warp::any().map(|| "Hello, World!");

    let (tx, rx) = oneshot::channel();

    let (_addr, server) =
        warp::serve(routes).bind_with_graceful_shutdown(([127, 0, 0, 1], 3030), async {
            rx.await.ok();
        });

    // Spawn the server into a runtime
    tokio::task::spawn(server);

    // Later, start the shutdown...
    let _ = tx.send(());
}
于 2022-02-25T00:33:29.517 回答
0

感谢大家的想法。这是最终以我想要的方式工作的代码:

use warp::Filter;
use tokio::signal::unix::{signal, SignalKind};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let routes = warp::any().map(|| "Hello, World!");

    let mut stream = signal(SignalKind::terminate())?;

    let (_, server) = warp::serve(routes)
         .bind_with_graceful_shutdown(([127, 0, 0, 1], 3030), async move {
             println!("waiting for signal");
             stream.recv().await;
             println!("done waiting for signal");
         });

    match tokio::join!(tokio::task::spawn(server)).0 {
        Ok(()) => println!("serving"),
        Err(e) => println!("ERROR: Thread join error {}", e)
    };

    println!("terminating");
    Ok(())
}
于 2022-02-26T19:07:50.880 回答