haesli/amqp_core/src/queue.rs
2022-03-10 19:18:26 +01:00

67 lines
1.6 KiB
Rust

use std::{
borrow::Borrow,
collections::HashMap,
fmt::{Debug, Display, Formatter},
sync::{atomic::AtomicUsize, Arc},
};
use parking_lot::Mutex;
use tokio::sync::mpsc;
use crate::{
consumer::{Consumer, ConsumerId},
message::Message,
newtype, newtype_id, ChannelId,
};
pub type Queue = Arc<QueueInner>;
#[derive(Debug)]
pub enum QueueEvent {
PublishMessage(Message),
Shutdown,
}
pub type QueueEventSender = mpsc::Sender<QueueEvent>;
pub type QueueEventReceiver = mpsc::Receiver<QueueEvent>;
newtype_id!(pub QueueId);
newtype!(
/// The name of a queue. A newtype wrapper around `Arc<str>`, which guarantees cheap clones.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub QueueName: Arc<str>
);
impl Borrow<str> for QueueName {
fn borrow(&self) -> &str {
std::borrow::Borrow::borrow(&self.0)
}
}
impl Display for QueueName {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f)
}
}
#[derive(Debug)]
pub struct QueueInner {
pub id: QueueId,
pub name: QueueName,
pub messages: amqp_datastructure::MessageQueue<Message>,
pub durable: bool,
pub exclusive: Option<ChannelId>,
/// Whether the queue will automatically be deleted when no consumers uses it anymore.
/// The queue can always be manually deleted.
/// If auto-delete is enabled, it keeps track of the consumer count.
pub deletion: QueueDeletion,
pub consumers: Mutex<HashMap<ConsumerId, Consumer>>,
pub event_send: QueueEventSender,
}
#[derive(Debug)]
pub enum QueueDeletion {
Auto(AtomicUsize),
Manual,
}