超时中间件
2025年6月21日小于 1 分钟hyperlanewebrustmiddlewaretimeout
提示
hyperlane
框架支持超时中间件,用于处理请求超时的情况。
超时中间件
async fn timeout_middleware(ctx: Context) {
spawn(async move {
timeout(Duration::from_millis(100), async move {
ctx.aborted().await;
ctx.set_response_status_code(200)
.await
.set_response_body("timeout")
.unwrap();
})
.await
.unwrap();
});
}
async fn index(ctx: Context) {
sleep(Duration::from_secs(1)).await;
ctx.set_response_status_code(200)
.await
.set_response_body("Hello, world!")
.await;
}
async fn response_middleware(ctx: Context) {
ctx.send().await.unwrap();
}
#[tokio::main]
async fn main() {
Server::new()
.request_middleware(timeout_middleware)
.await
.response_middleware(response_middleware)
.await
.route("/", index)
.await
.run()
.await
.unwrap();
}