mirror of
https://github.com/chatmail/core.git
synced 2026-04-28 10:56:29 +03:00
api: add event channel overflow event
This commit is contained in:
@@ -71,16 +71,13 @@ impl EventEmitter {
|
||||
/// [`try_recv`]: Self::try_recv
|
||||
pub async fn recv(&self) -> Option<Event> {
|
||||
let mut lock = self.0.lock().await;
|
||||
loop {
|
||||
match lock.recv().await {
|
||||
Err(async_broadcast::RecvError::Overflowed(_)) => {
|
||||
// Some events have been lost,
|
||||
// but the channel is not closed.
|
||||
continue;
|
||||
}
|
||||
Err(async_broadcast::RecvError::Closed) => return None,
|
||||
Ok(event) => return Some(event),
|
||||
}
|
||||
match lock.recv().await {
|
||||
Err(async_broadcast::RecvError::Overflowed(n)) => Some(Event {
|
||||
id: 0,
|
||||
typ: EventType::EventChannelOverflow { n },
|
||||
}),
|
||||
Err(async_broadcast::RecvError::Closed) => None,
|
||||
Ok(event) => Some(event),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,17 +93,18 @@ impl EventEmitter {
|
||||
// to avoid blocking
|
||||
// in case there is a concurrent call to `recv`.
|
||||
let mut lock = self.0.try_lock()?;
|
||||
loop {
|
||||
match lock.try_recv() {
|
||||
Err(async_broadcast::TryRecvError::Overflowed(_)) => {
|
||||
// Some events have been lost,
|
||||
// but the channel is not closed.
|
||||
continue;
|
||||
}
|
||||
res @ (Err(async_broadcast::TryRecvError::Empty)
|
||||
| Err(async_broadcast::TryRecvError::Closed)
|
||||
| Ok(_)) => return Ok(res?),
|
||||
match lock.try_recv() {
|
||||
Err(async_broadcast::TryRecvError::Overflowed(n)) => {
|
||||
// Some events have been lost,
|
||||
// but the channel is not closed.
|
||||
Ok(Event {
|
||||
id: 0,
|
||||
typ: EventType::EventChannelOverflow { n },
|
||||
})
|
||||
}
|
||||
res @ (Err(async_broadcast::TryRecvError::Empty)
|
||||
| Err(async_broadcast::TryRecvError::Closed)
|
||||
| Ok(_)) => Ok(res?),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,4 +315,10 @@ pub enum EventType {
|
||||
/// Event for using in tests, e.g. as a fence between normally generated events.
|
||||
#[cfg(test)]
|
||||
Test,
|
||||
|
||||
/// Inform than some events have been skipped due to event channel overflow.
|
||||
EventChannelOverflow {
|
||||
/// Number of events skipped.
|
||||
n: u64,
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user