Skip to content

Commit

Permalink
fix: retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Wybxc committed May 12, 2023
1 parent 3f5f4de commit f4ac941
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 27 deletions.
1 change: 1 addition & 0 deletions chocho_login/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#![deny(missing_docs)]
#![feature(never_type)]
#![feature(try_blocks)]

use anyhow::Result;
use login::reconnect;
Expand Down
35 changes: 8 additions & 27 deletions chocho_login/src/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,9 @@ pub(crate) async fn reconnect(
client: &Arc<ricq::Client>,
account_data_folder: &Path,
) -> Result<JoinHandle<()>> {
retry(
10,
|| async {
// 如果不是网络原因掉线,不重连(服务端强制下线/被踢下线/用户手动停止)
let mut retry_count = 10;
loop {
match try {
if client.get_status() != (NetworkStatus::NetworkOffline as u8) {
bail!("客户端因非网络原因下线,不再重连");
}
Expand Down Expand Up @@ -176,34 +175,16 @@ pub(crate) async fn reconnect(
after_login(client).await;

tracing::info!("客户端重连成功");
Ok(Ok(alive))
},
|e, c| async move {
tracing::error!("客户端重连失败,原因:{},剩余尝试 {} 次", e, c);
},
)
.await?
}

/// 自动重试直到得到 `Ok(..)`。
pub async fn retry<F, T, D, E>(
mut max_count: usize,
mut f: impl FnMut() -> F,
mut on_retry: impl FnMut(E, usize) -> D,
) -> Result<T, E>
where
F: Future<Output = Result<T, E>>,
D: Future<Output = ()>,
{
loop {
match f().await {
alive
} {
Ok(t) => return Ok(t),
Err(e) => {
if max_count == 0 {
if retry_count == 0 {
return Err(e);
}
max_count -= 1;
on_retry(e, max_count).await;
retry_count -= 1;
tracing::error!("客户端重连失败,原因:{},剩余尝试 {} 次", e, retry_count);
tokio::task::yield_now().await;
}
}
Expand Down

0 comments on commit f4ac941

Please sign in to comment.