use std::sync::atomic::{AtomicU64, Ordering};
use parking_lot::Mutex;
#[derive(Debug)]
pub struct RetryQueue<T> {
inner: Mutex<std::collections::VecDeque<T>>,
capacity: usize,
dropped: AtomicU64,
}
impl<T> RetryQueue<T> {
#[must_use]
pub fn new(capacity: usize) -> Self {
Self {
inner: Mutex::new(std::collections::VecDeque::with_capacity(capacity)),
capacity,
dropped: AtomicU64::new(0),
}
}
pub fn push(&self, item: T) -> bool {
let mut inner = self.inner.lock();
if inner.len() >= self.capacity {
self.dropped.fetch_add(1, Ordering::Relaxed);
return false;
}
inner.push_back(item);
true
}
pub fn pop(&self) -> Option<T> {
self.inner.lock().pop_front()
}
#[allow(dead_code)]
#[must_use]
pub fn dropped(&self) -> u64 {
self.dropped.load(Ordering::Relaxed)
}
#[must_use]
pub fn depth(&self) -> usize {
self.inner.lock().len()
}
}