forked from navos/sheepd
✨ async channel
This commit is contained in:
parent
f904603a8d
commit
060209827f
5 changed files with 78 additions and 561 deletions
|
@ -44,12 +44,17 @@ pub async fn handle_mqtt(topic: String, data: Vec<u8>) {
|
|||
let resp: ServerResponse = serde_json::from_slice(&dec.payload).unwrap();
|
||||
log::info!("Got response {:?}", resp);
|
||||
|
||||
let entry = crate::DISPATCH
|
||||
let (id, entry) = crate::DISPATCH
|
||||
.get()
|
||||
.unwrap()
|
||||
.get(&resp.id.to_string())
|
||||
.remove(&resp.id.to_string())
|
||||
.unwrap();
|
||||
entry.send(resp);
|
||||
|
||||
if entry.send(resp).is_err() {
|
||||
log::error!(
|
||||
"Could not send back response for action {id}. Probably due to timeout"
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
@ -57,13 +62,16 @@ pub async fn handle_mqtt(topic: String, data: Vec<u8>) {
|
|||
|
||||
pub struct TaskWaiter {
|
||||
pub id: ulid::Ulid,
|
||||
pub recv: crossbeam::channel::Receiver<ServerResponse>,
|
||||
pub recv: tokio::sync::oneshot::Receiver<ServerResponse>,
|
||||
}
|
||||
|
||||
impl TaskWaiter {
|
||||
pub async fn wait_for(&self, timeout: std::time::Duration) -> Option<ServerResponse> {
|
||||
// TODO tokio spawn blocking?
|
||||
self.recv.recv_timeout(timeout).ok()
|
||||
pub async fn wait_for(self, timeout: std::time::Duration) -> Option<ServerResponse> {
|
||||
if let Ok(in_time) = tokio::time::timeout(timeout, self.recv).await {
|
||||
return in_time.ok();
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -89,7 +97,7 @@ pub async fn send_msg(
|
|||
.await
|
||||
.unwrap();
|
||||
|
||||
let (sender, recv) = crossbeam::channel::bounded(100);
|
||||
let (sender, recv) = tokio::sync::oneshot::channel();
|
||||
crate::DISPATCH
|
||||
.get()
|
||||
.unwrap()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue