连接管理
2025年7月17日小于 1 分钟hyperlanewebrustusage-introductionconnection
提示
hyperlane
框架提供了完整的连接状态管理功能,包括连接的中止、关闭状态控制,以及 Keep-Alive
连接支持。
连接状态管理
获取连接状态
let aborted: bool = ctx.get_aborted().await;
let closed: bool = ctx.get_closed().await;
设置连接状态
ctx.set_aborted(true).await;
ctx.set_closed(true).await;
快捷方法
ctx.aborted().await; // 中止连接
ctx.closed().await; // 关闭连接
ctx.cancel_aborted().await; // 取消中止
ctx.cancel_closed().await; // 取消关闭
Keep-Alive 连接
检查是否启用 Keep-Alive
let keep_alive: bool = ctx.is_enable_keep_alive().await;
基本使用示例
连接状态检查
if ctx.get_closed().await {
return;
}
长连接处理
while !ctx.get_closed().await && !ctx.get_aborted().await {
let _ = ctx.http_from_stream(8192).await;
if !ctx.is_enable_keep_alive().await {
ctx.closed().await;
break;
}
}