mirror of
https://github.com/Noratrieb/haesli.git
synced 2026-01-15 20:25:02 +01:00
add exchanges to internal model
This commit is contained in:
parent
504757b324
commit
58de7f1e2d
15 changed files with 169 additions and 40 deletions
|
|
@ -5,5 +5,6 @@ use haesli_core::error::ProtocolError;
|
|||
|
||||
pub mod methods;
|
||||
mod queue_worker;
|
||||
mod routing;
|
||||
|
||||
type Result<T> = std::result::Result<T, ProtocolError>;
|
||||
|
|
|
|||
|
|
@ -2,15 +2,14 @@ mod consume;
|
|||
mod publish;
|
||||
mod queue;
|
||||
|
||||
use haesli_core::{amqp_todo, connection::Channel, message::Message, methods::Method};
|
||||
use haesli_core::{amqp_todo, connection::Channel, methods::Method};
|
||||
pub use publish::publish;
|
||||
use tracing::info;
|
||||
|
||||
use crate::Result;
|
||||
|
||||
pub fn handle_basic_publish(channel_handle: Channel, message: Message) -> Result<()> {
|
||||
publish::publish(channel_handle, message)
|
||||
}
|
||||
|
||||
/// This is the entrypoint of methods not handled by the connection itself.
|
||||
/// Note that Basic.Publish is *not* sent here, but to [`handle_basic_publish`](crate::handle_basic_publish)
|
||||
pub async fn handle_method(channel_handle: Channel, method: Method) -> Result<Method> {
|
||||
info!(?method, "Handling method");
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use haesli_core::{
|
|||
};
|
||||
use tracing::{debug, error};
|
||||
|
||||
use crate::Result;
|
||||
use crate::{routing, Result};
|
||||
|
||||
pub fn publish(channel_handle: Channel, message: Message) -> Result<()> {
|
||||
debug!(?message, "Publishing message");
|
||||
|
|
@ -22,9 +22,14 @@ pub fn publish(channel_handle: Channel, message: Message) -> Result<()> {
|
|||
|
||||
let global_data = global_data.lock();
|
||||
|
||||
let queue = global_data
|
||||
.queues
|
||||
.get(routing.routing_key.as_str())
|
||||
let exchange = &message.routing.exchange;
|
||||
|
||||
let exchange = global_data
|
||||
.exchanges
|
||||
.get(exchange.as_str())
|
||||
.ok_or(ChannelException::NotFound)?;
|
||||
|
||||
let queue = routing::route_message(exchange, &message.routing.routing_key)
|
||||
.ok_or(ChannelException::NotFound)?;
|
||||
|
||||
queue
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ use parking_lot::Mutex;
|
|||
use tokio::sync::mpsc;
|
||||
use tracing::debug;
|
||||
|
||||
use crate::{queue_worker::QueueTask, Result};
|
||||
use crate::{queue_worker::QueueTask, routing, Result};
|
||||
|
||||
pub fn declare(channel: Channel, queue_declare: QueueDeclare) -> Result<Method> {
|
||||
let QueueDeclare {
|
||||
|
|
@ -75,7 +75,7 @@ pub fn declare(channel: Channel, queue_declare: QueueDeclare) -> Result<Method>
|
|||
.or_insert_with(|| queue.clone());
|
||||
}
|
||||
|
||||
bind_queue(global_data.clone(), (), queue_name.clone().into_inner())?;
|
||||
bind_queue(global_data.clone(), (), queue_name.to_string())?;
|
||||
|
||||
let queue_task = QueueTask::new(global_data, event_recv, queue);
|
||||
|
||||
|
|
@ -92,18 +92,22 @@ pub async fn bind(_channel_handle: Channel, _queue_bind: QueueBind) -> Result<Me
|
|||
amqp_todo!();
|
||||
}
|
||||
|
||||
fn bind_queue(global_data: GlobalData, _exchange: (), routing_key: Arc<str>) -> Result<()> {
|
||||
fn bind_queue(global_data: GlobalData, _exchange: (), routing_key: String) -> Result<()> {
|
||||
let mut global_data = global_data.lock();
|
||||
|
||||
// todo: don't
|
||||
let queue = global_data
|
||||
.queues
|
||||
.get(&QueueName::new(routing_key.clone()))
|
||||
.get(&QueueName::new(routing_key.clone().into()))
|
||||
.unwrap()
|
||||
.clone();
|
||||
global_data
|
||||
.default_exchange
|
||||
.insert(routing_key.to_string(), queue);
|
||||
|
||||
let exchange = global_data
|
||||
.exchanges
|
||||
.get_mut("")
|
||||
.expect("default empty exchange");
|
||||
|
||||
routing::bind(exchange, routing_key, queue);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
30
haesli_messaging/src/routing.rs
Normal file
30
haesli_messaging/src/routing.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
use haesli_core::{
|
||||
exchange::{Exchange, ExchangeType},
|
||||
queue::Queue,
|
||||
};
|
||||
|
||||
pub fn bind(exchange: &mut Exchange, routing_key: String, queue: Queue) {
|
||||
match &mut exchange.kind {
|
||||
ExchangeType::Direct { bindings } => {
|
||||
bindings.insert(routing_key, queue);
|
||||
}
|
||||
ExchangeType::Fanout { bindings } => bindings.push(queue),
|
||||
ExchangeType::Topic { bindings } => bindings.push((routing_key, queue)),
|
||||
ExchangeType::Headers => {} // unsupported
|
||||
ExchangeType::System => {} // unsupported
|
||||
}
|
||||
}
|
||||
|
||||
/// Route a message to a queue. Returns the queue to send it to, or `None` if it can't be matched
|
||||
pub fn route_message(exchange: &Exchange, routing_key: &str) -> Option<Queue> {
|
||||
match &exchange.kind {
|
||||
ExchangeType::Direct { bindings } => {
|
||||
// 3.1.3.1 - routing-key = routing-key
|
||||
bindings.get(routing_key).cloned()
|
||||
}
|
||||
ExchangeType::Fanout { .. } => None,
|
||||
ExchangeType::Topic { .. } => None,
|
||||
ExchangeType::Headers => None, // unsupported
|
||||
ExchangeType::System => None, // unsupported
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue