add exchanges to internal model

This commit is contained in:
nora 2022-03-20 17:52:41 +01:00
parent 504757b324
commit 58de7f1e2d
15 changed files with 169 additions and 40 deletions

View file

@ -10,7 +10,7 @@ haesli_datastructure = { path = "../haesli_datastructure" }
bytes = "1.1.0"
parking_lot = "0.12.0"
rand = "0.8.5"
smallvec = { version = "1.8.0", features = ["union"] }
tinyvec = { version = "1.5.1", features = ["alloc", "rustc_1_55"] }
thiserror = "1.0.30"
tokio = { version = "1.17.0", features = ["sync"] }
uuid = "0.8.2"

View file

@ -7,7 +7,7 @@ use std::{
use bytes::Bytes;
use parking_lot::Mutex;
use smallvec::SmallVec;
use tinyvec::TinyVec;
use tokio::sync::mpsc;
use crate::{
@ -67,7 +67,7 @@ pub struct ConnectionInner {
pub enum ConnectionEvent {
Shutdown,
Method(ChannelNum, Box<Method>),
MethodContent(ChannelNum, Box<Method>, ContentHeader, SmallVec<[Bytes; 1]>),
MethodContent(ChannelNum, Box<Method>, ContentHeader, TinyVec<[Bytes; 1]>),
}
pub type ConEventSender = mpsc::Sender<ConnectionEvent>;

View file

@ -1,13 +1,88 @@
use std::{borrow::Borrow, collections::HashMap, sync::Arc};
use crate::{newtype, Queue};
#[derive(Debug)]
pub enum ExchangeType {
/// Routes a message to a queue if the routing-keys are equal
Direct,
Direct { bindings: HashMap<String, Queue> },
/// Always routes the message to a queue
Fanout,
Fanout { bindings: Vec<Queue> },
/// Routes a message to a queue if the routing key matches the pattern
Topic,
Topic { bindings: Vec<(String, Queue)> },
/// Is bound with a table of headers and values, and matches if the message headers
/// match up with the binding headers
///
/// Unsupported for now.
Headers,
/// The message is sent to the server system service with the name of the routing-key
///
/// Unsupported for now.
System,
}
newtype!(
/// The name of a queue. A newtype wrapper around `Arc<str>`, which guarantees cheap clones.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub ExchangeName: Arc<str>
);
impl Borrow<str> for ExchangeName {
fn borrow(&self) -> &str {
Borrow::borrow(&self.0)
}
}
#[derive(Debug)]
pub struct Exchange {
pub name: ExchangeName,
pub kind: ExchangeType,
}
#[derive(Debug)]
pub struct Binding {}
pub fn default_exchanges() -> HashMap<ExchangeName, Exchange> {
// 3.1.3 - The spec requires a few default exchanges to exist
let empty_name = ExchangeName::new("".to_owned().into());
let empty = Exchange {
name: empty_name.clone(),
kind: ExchangeType::Direct {
bindings: HashMap::new(),
},
};
let direct_name = ExchangeName::new("amqp.direct".to_owned().into());
let direct = Exchange {
name: direct_name.clone(),
kind: ExchangeType::Direct {
bindings: HashMap::new(),
},
};
let fanout_name = ExchangeName::new("amqp.fanout".to_owned().into());
let fanout = Exchange {
name: fanout_name.clone(),
kind: ExchangeType::Fanout {
bindings: Vec::new(),
},
};
let topic_name = ExchangeName::new("amqp.topic".to_owned().into());
let topic = Exchange {
name: topic_name.clone(),
kind: ExchangeType::Topic {
bindings: Vec::new(),
},
};
// we don't implement headers (yet), so don't provide the default exchange for it
HashMap::from([
(empty_name, empty),
(direct_name, direct),
(fanout_name, fanout),
(topic_name, topic),
])
}

View file

@ -21,10 +21,12 @@ use uuid::Uuid;
use crate::{
connection::{Channel, Connection},
exchange::{Exchange, ExchangeName},
queue::{Queue, QueueName},
};
#[derive(Clone)]
// todo: what if this was downstream?
pub struct GlobalData {
inner: Arc<Mutex<GlobalDataInner>>,
}
@ -42,6 +44,7 @@ impl Default for GlobalData {
connections: HashMap::new(),
channels: HashMap::new(),
queues: HashMap::new(),
exchanges: exchange::default_exchanges(),
default_exchange: HashMap::new(),
})),
}
@ -59,6 +62,7 @@ pub struct GlobalDataInner {
pub connections: HashMap<ConnectionId, Connection>,
pub channels: HashMap<ChannelId, Channel>,
pub queues: HashMap<QueueName, Queue>,
pub exchanges: HashMap<ExchangeName, Exchange>,
/// Todo: This is just for testing and will be removed later!
pub default_exchange: HashMap<String, Queue>,
}

View file

@ -63,7 +63,7 @@ macro_rules! newtype {
where
$ty: std::fmt::Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}

View file

@ -1,7 +1,7 @@
use std::sync::Arc;
use bytes::Bytes;
use smallvec::SmallVec;
use tinyvec::TinyVec;
use crate::{connection::ContentHeader, newtype_id};
@ -14,7 +14,7 @@ pub struct MessageInner {
pub id: MessageId,
pub header: ContentHeader,
pub routing: RoutingInformation,
pub content: SmallVec<[Bytes; 1]>,
pub content: TinyVec<[Bytes; 1]>,
}
#[derive(Debug)]

View file

@ -1,7 +1,7 @@
use std::{
borrow::Borrow,
collections::HashMap,
fmt::{Debug, Formatter},
fmt::Debug,
sync::{atomic::AtomicUsize, Arc},
};