fix: Query online, remove loop retry (#9326)

Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
fufesou
2024-09-11 17:17:32 +08:00
committed by GitHub
parent cbca0eb340
commit 2e81bcb447
2 changed files with 44 additions and 63 deletions

View File

@@ -2057,18 +2057,18 @@ pub mod sessions {
pub(super) mod async_tasks {
use hbb_common::{
bail,
tokio::{
self, select,
sync::mpsc::{unbounded_channel, UnboundedSender},
},
tokio::{self, select},
ResultType,
};
use std::{
collections::HashMap,
sync::{Arc, Mutex},
sync::{
mpsc::{sync_channel, SyncSender},
Arc, Mutex,
},
};
type TxQueryOnlines = UnboundedSender<Vec<String>>;
type TxQueryOnlines = SyncSender<Vec<String>>;
lazy_static::lazy_static! {
static ref TX_QUERY_ONLINES: Arc<Mutex<Option<TxQueryOnlines>>> = Default::default();
}
@@ -2085,20 +2085,18 @@ pub(super) mod async_tasks {
#[tokio::main(flavor = "current_thread")]
async fn start_flutter_async_runner_() {
let (tx_onlines, mut rx_onlines) = unbounded_channel::<Vec<String>>();
// Only one task is allowed to run at the same time.
let (tx_onlines, rx_onlines) = sync_channel::<Vec<String>>(1);
TX_QUERY_ONLINES.lock().unwrap().replace(tx_onlines);
loop {
select! {
ids = rx_onlines.recv() => {
match ids {
Some(_ids) => {
crate::client::peer_online::query_online_states(_ids, handle_query_onlines).await
}
None => {
break;
}
}
match rx_onlines.recv() {
Ok(ids) => {
crate::client::peer_online::query_online_states(ids, handle_query_onlines).await
}
_ => {
// unreachable!
break;
}
}
}
@@ -2106,7 +2104,8 @@ pub(super) mod async_tasks {
pub fn query_onlines(ids: Vec<String>) -> ResultType<()> {
if let Some(tx) = TX_QUERY_ONLINES.lock().unwrap().as_ref() {
let _ = tx.send(ids)?;
// Ignore if the channel is full.
let _ = tx.try_send(ids)?;
} else {
bail!("No tx_query_onlines");
}