Cancel certain async callback on disconnect
This commit is contained in:
3 files changed
+22
-14
No files matched your search
@@ -232,6 +232,8 @@ struct Poller {
|
||||
|
||||
impl Poller {
|
||||
async fn run(&mut self) -> Result<()> {
|
||||
let cancel = CancellationToken::new();
|
||||
let _dropguard = cancel.clone().drop_guard();
|
||||
while let Some(command) = self.reciever.next().await {
|
||||
// Clean Up subscribers
|
||||
self.subscribers
|
||||
@@ -270,12 +272,18 @@ impl Poller {
|
||||
// Move this on another thread coz I have NO idea
|
||||
// how long the callback will run for
|
||||
// and we must NOT hang
|
||||
let cancel = cancel.clone();
|
||||
tokio::task::spawn(async move {
|
||||
if let Some(reply) = occ(&response).await {
|
||||
assert!(reply.meta.msg_num == response.meta.msg_num);
|
||||
sink.send(Ok(reply)).await?;
|
||||
tokio::select! {
|
||||
_ = cancel.cancelled() => Result::Ok(()),
|
||||
v = occ(&response) => {
|
||||
if let Some(reply) = v {
|
||||
assert!(reply.meta.msg_num == response.meta.msg_num);
|
||||
sink.send(Ok(reply)).await?;
|
||||
}
|
||||
Result::Ok(())
|
||||
}
|
||||
}
|
||||
Result::Ok(())
|
||||
});
|
||||
log::trace!("Called ID callback");
|
||||
}
|
||||
@@ -321,10 +329,7 @@ impl Poller {
|
||||
&msg_id
|
||||
);
|
||||
}
|
||||
let sender = sender.clone();
|
||||
// tokio::task::spawn(async move {
|
||||
let _ = sender.send(Ok(response)).await;
|
||||
// });
|
||||
} else {
|
||||
debug!(
|
||||
"Ignoring uninteresting message id {} (number: {})",
|
||||
@@ -383,11 +388,9 @@ impl Poller {
|
||||
occ_entry.insert(tx);
|
||||
} else {
|
||||
// log::error!("Failed to subscribe in bcconn to {:?} for {:?}", msg_num, msg_id);
|
||||
// tokio::task::spawn(async move {
|
||||
let _ = tx
|
||||
.send(Err(Error::SimultaneousSubscription { msg_num }))
|
||||
.await;
|
||||
// });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -401,9 +401,8 @@ impl StreamData {
|
||||
AnyResult::Ok(())
|
||||
},
|
||||
v = async {
|
||||
watchdog_rx.recv().await; // Wait forever for the first feed
|
||||
let mut check_timeout = timeout(Duration::from_secs(15), watchdog_rx.recv()).await; // Wait lonfer for the first feed
|
||||
loop {
|
||||
let check_timeout = timeout(Duration::from_secs(3), watchdog_rx.recv()).await;
|
||||
if let Err(_)| Ok(None) = check_timeout {
|
||||
// Timeout
|
||||
// Reply with Ok to trigger the restart
|
||||
@@ -411,6 +410,7 @@ impl StreamData {
|
||||
sleep(Duration::from_secs(1)).await;
|
||||
break AnyResult::Ok(());
|
||||
}
|
||||
check_timeout = timeout(Duration::from_secs(4), watchdog_rx.recv()).await;
|
||||
}
|
||||
} => v,
|
||||
result = instance.run_passive_task(|camera| {
|
||||
|
||||
+8
-3
@@ -1055,7 +1055,9 @@ async fn make_factory(
|
||||
// Need and enough are fired when we
|
||||
// call push_buffer which is inside an
|
||||
// async context so blocking_send is forbiddon
|
||||
tokio::task::spawn(vid_enough_tx.send(StreamData::Enough { ts }));
|
||||
tokio::task::spawn(async move {
|
||||
vid_enough_tx.send(StreamData::Enough { ts }).await
|
||||
});
|
||||
})
|
||||
.need_data(move |_, _| {
|
||||
log::debug!("need_data:");
|
||||
@@ -1064,7 +1066,9 @@ async fn make_factory(
|
||||
// Need and enough are fired when we
|
||||
// call push_buffer which is inside an
|
||||
// async context so blocking_send is forbiddon
|
||||
tokio::task::spawn(vid_need_tx.send(StreamData::Need { ts }));
|
||||
tokio::task::spawn(async move {
|
||||
vid_need_tx.send(StreamData::Need { ts }).await
|
||||
});
|
||||
})
|
||||
.build(),
|
||||
);
|
||||
@@ -1205,9 +1209,10 @@ async fn handle_data<T: Stream<Item = Result<StreamData, E>> + Unpin, E>(
|
||||
last_ft = Some(ft_i);
|
||||
|
||||
// Sync ft to rt if > 1500ms difference
|
||||
const MAX_DELTA_T: Duration = Duration::from_millis(1500);
|
||||
const MAX_DELTA_T: Duration = Duration::from_millis(200);
|
||||
let delta_t = if rt > ft { rt - ft } else { ft - rt };
|
||||
if delta_t > MAX_DELTA_T {
|
||||
log::debug!("delta_t > MAX_DELTA_T = {delta_t:?}, {MAX_DELTA_T:?}");
|
||||
ft = rt;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user