gpt4 book ai didi

rust - 如何优雅地关闭 warp 服务器?

转载 作者:行者123 更新时间:2023-12-05 03:28:27 24 4
gpt4 key购买 nike

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

我已经尝试了很多例子,但没有任何效果。最有希望的似乎来自 this issue但我似乎无法让它工作甚至无法编译。我怀疑自从回答这个问题后情况发生了变化。

# Cargo.toml
[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`

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

最佳答案

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

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(())
}

关于rust - 如何优雅地关闭 warp 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71258916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com