feat(storage): 添加写入重试逻辑
This commit is contained in:
parent
69a8ec2775
commit
3ffa8c41f7
@ -5,6 +5,7 @@ pub mod message;
|
|||||||
pub use error::StorageError;
|
pub use error::StorageError;
|
||||||
|
|
||||||
use sqlx::{Pool, Row, Sqlite, SqlitePool};
|
use sqlx::{Pool, Row, Sqlite, SqlitePool};
|
||||||
|
use tokio::time::{sleep, Duration};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
pub struct Storage {
|
pub struct Storage {
|
||||||
@ -334,4 +335,29 @@ impl Storage {
|
|||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 追加消息,带重试逻辑
|
||||||
|
/// 重试 3 次(100/200/300ms 退避),仍失败返回错误
|
||||||
|
pub async fn append_message_with_retry(
|
||||||
|
&self,
|
||||||
|
session_id: &str,
|
||||||
|
msg: &crate::storage::message::MessageMeta,
|
||||||
|
) -> Result<i64, StorageError> {
|
||||||
|
let delays = [100, 200, 300];
|
||||||
|
|
||||||
|
for (i, delay) in delays.iter().enumerate() {
|
||||||
|
match self.append_message(session_id, msg).await {
|
||||||
|
Ok(seq) => return Ok(seq),
|
||||||
|
Err(e) if i < delays.len() - 1 => {
|
||||||
|
sleep(Duration::from_millis(*delay)).await;
|
||||||
|
tracing::warn!("Storage write failed, retrying: {}", e);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Storage write failed after retries: {}", e);
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user