Skip to content

Commit

Permalink
Add recv_async_timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
haixuanTao committed Dec 7, 2023
1 parent 0cd5281 commit 1ad35fa
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions apis/rust/node/src/event_stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,26 @@ impl EventStream {

/// wait for the next event on the events stream.
pub fn recv(&mut self) -> Option<Event> {
futures::executor::block_on(self.recv_async(None))
futures::executor::block_on(self.recv_async())
}

/// wait for the next event on the events stream until timeout
pub fn recv_timeout(&mut self, dur: Duration) -> Option<Event> {
futures::executor::block_on(self.recv_async(Some(dur)))
futures::executor::block_on(self.recv_async_timeout(dur))
}

pub async fn recv_async(&mut self, dur: Option<Duration>) -> Option<Event> {
let receive_event = self.receiver.next();
match dur {
None => receive_event.await,
Some(dur) => match select(Delay::new(dur), receive_event).await {
pub async fn recv_async(&mut self) -> Option<Event> {

Check failure on line 119 in apis/rust/node/src/event_stream/mod.rs

View workflow job for this annotation

GitHub Actions / Formatting

Diff in /home/runner/work/dora/dora/apis/rust/node/src/event_stream/mod.rs
self.receiver.next().await.map(Self::convert_event_item)
}

pub async fn recv_async_timeout(&mut self, dur: Duration) -> Option<Event> {
let next_event = match select(Delay::new(dur), self.receiver.next()).await {
Either::Left((_elapsed, _)) => {
Some(EventItem::TimeoutError(eyre!("Receiver timed out")))
}
Either::Right((event, _)) => event,
},
}
};
next_event
.map(Self::convert_event_item)
}

Expand Down

0 comments on commit 1ad35fa

Please sign in to comment.