mirror of
https://github.com/Noratrieb/haesli.git
synced 2026-01-15 20:25:02 +01:00
rename lol
This commit is contained in:
parent
c68cd04af7
commit
543e39f129
70 changed files with 283 additions and 266 deletions
60
haesli_messaging/src/methods/consume.rs
Normal file
60
haesli_messaging/src/methods/consume.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use haesli_core::{
|
||||
connection::Channel,
|
||||
consumer::{Consumer, ConsumerId},
|
||||
error::ChannelException,
|
||||
haesli_todo,
|
||||
methods::{BasicConsume, BasicConsumeOk, Method},
|
||||
};
|
||||
use tracing::info;
|
||||
|
||||
use crate::Result;
|
||||
|
||||
pub fn consume(channel: Channel, basic_consume: BasicConsume) -> Result<Method> {
|
||||
let BasicConsume {
|
||||
queue: queue_name,
|
||||
consumer_tag,
|
||||
no_local,
|
||||
no_ack,
|
||||
exclusive,
|
||||
no_wait,
|
||||
..
|
||||
} = basic_consume;
|
||||
|
||||
if no_wait || no_local || exclusive || no_ack {
|
||||
haesli_todo!();
|
||||
}
|
||||
|
||||
let global_data = channel.global_data.clone();
|
||||
|
||||
let consumer_tag = if consumer_tag.is_empty() {
|
||||
haesli_core::random_uuid().to_string()
|
||||
} else {
|
||||
consumer_tag
|
||||
};
|
||||
|
||||
let mut global_data = global_data.lock();
|
||||
|
||||
let queue = global_data
|
||||
.queues
|
||||
.get_mut(queue_name.as_str())
|
||||
.ok_or(ChannelException::NotFound)?;
|
||||
|
||||
let consumer = Consumer {
|
||||
id: ConsumerId::random(),
|
||||
tag: consumer_tag.clone(),
|
||||
channel: Arc::clone(&channel),
|
||||
queue: Arc::clone(queue),
|
||||
};
|
||||
|
||||
queue.consumers.lock().insert(consumer.id, consumer.clone());
|
||||
|
||||
channel.connection.consuming.lock().push(consumer);
|
||||
|
||||
info!(%queue_name, %consumer_tag, "Consumer started consuming");
|
||||
|
||||
let method = Method::BasicConsumeOk(BasicConsumeOk { consumer_tag });
|
||||
|
||||
Ok(method)
|
||||
}
|
||||
61
haesli_messaging/src/methods/mod.rs
Normal file
61
haesli_messaging/src/methods/mod.rs
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
mod consume;
|
||||
mod publish;
|
||||
mod queue;
|
||||
|
||||
use haesli_core::{connection::Channel, haesli_todo, message::Message, methods::Method};
|
||||
use tracing::info;
|
||||
|
||||
use crate::Result;
|
||||
|
||||
pub fn handle_basic_publish(channel_handle: Channel, message: Message) -> Result<()> {
|
||||
publish::publish(channel_handle, message)
|
||||
}
|
||||
|
||||
pub async fn handle_method(channel_handle: Channel, method: Method) -> Result<Method> {
|
||||
info!(?method, "Handling method");
|
||||
|
||||
let response = match method {
|
||||
Method::ExchangeDeclare(_) => haesli_todo!(),
|
||||
Method::ExchangeDeclareOk(_) => haesli_todo!(),
|
||||
Method::ExchangeDelete(_) => haesli_todo!(),
|
||||
Method::ExchangeDeleteOk(_) => haesli_todo!(),
|
||||
Method::QueueDeclare(queue_declare) => queue::declare(channel_handle, queue_declare)?,
|
||||
Method::QueueDeclareOk { .. } => haesli_todo!(),
|
||||
Method::QueueBind(queue_bind) => queue::bind(channel_handle, queue_bind).await?,
|
||||
Method::QueueBindOk(_) => haesli_todo!(),
|
||||
Method::QueueUnbind { .. } => haesli_todo!(),
|
||||
Method::QueueUnbindOk(_) => haesli_todo!(),
|
||||
Method::QueuePurge { .. } => haesli_todo!(),
|
||||
Method::QueuePurgeOk { .. } => haesli_todo!(),
|
||||
Method::QueueDelete { .. } => haesli_todo!(),
|
||||
Method::QueueDeleteOk { .. } => haesli_todo!(),
|
||||
Method::BasicQos { .. } => haesli_todo!(),
|
||||
Method::BasicQosOk(_) => haesli_todo!(),
|
||||
Method::BasicConsume(consume) => consume::consume(channel_handle, consume)?,
|
||||
Method::BasicConsumeOk { .. } => haesli_todo!(),
|
||||
Method::BasicCancel { .. } => haesli_todo!(),
|
||||
Method::BasicCancelOk { .. } => haesli_todo!(),
|
||||
Method::BasicReturn { .. } => haesli_todo!(),
|
||||
Method::BasicDeliver { .. } => haesli_todo!(),
|
||||
Method::BasicGet { .. } => haesli_todo!(),
|
||||
Method::BasicGetOk { .. } => haesli_todo!(),
|
||||
Method::BasicGetEmpty { .. } => haesli_todo!(),
|
||||
Method::BasicAck { .. } => haesli_todo!(),
|
||||
Method::BasicReject { .. } => haesli_todo!(),
|
||||
Method::BasicRecoverAsync { .. } => haesli_todo!(),
|
||||
Method::BasicRecover { .. } => haesli_todo!(),
|
||||
Method::BasicRecoverOk(_) => haesli_todo!(),
|
||||
Method::TxSelect(_)
|
||||
| Method::TxSelectOk(_)
|
||||
| Method::TxCommit(_)
|
||||
| Method::TxCommitOk(_)
|
||||
| Method::TxRollback(_)
|
||||
| Method::TxRollbackOk(_) => haesli_todo!(),
|
||||
Method::BasicPublish { .. } => {
|
||||
unreachable!("Basic.Publish is handled somewhere else because it has a body")
|
||||
}
|
||||
_ => unreachable!("Method handled by transport layer"),
|
||||
};
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
39
haesli_messaging/src/methods/publish.rs
Normal file
39
haesli_messaging/src/methods/publish.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
use haesli_core::{
|
||||
connection::Channel,
|
||||
error::{ChannelException, ConException},
|
||||
haesli_todo,
|
||||
message::Message,
|
||||
queue::QueueEvent,
|
||||
};
|
||||
use tracing::{debug, error};
|
||||
|
||||
use crate::Result;
|
||||
|
||||
pub fn publish(channel_handle: Channel, message: Message) -> Result<()> {
|
||||
debug!(?message, "Publishing message");
|
||||
|
||||
let global_data = channel_handle.global_data.clone();
|
||||
|
||||
let routing = &message.routing;
|
||||
|
||||
if !routing.exchange.is_empty() {
|
||||
haesli_todo!();
|
||||
}
|
||||
|
||||
let global_data = global_data.lock();
|
||||
|
||||
let queue = global_data
|
||||
.queues
|
||||
.get(routing.routing_key.as_str())
|
||||
.ok_or(ChannelException::NotFound)?;
|
||||
|
||||
queue
|
||||
.event_send
|
||||
.try_send(QueueEvent::PublishMessage(message))
|
||||
.map_err(|err| {
|
||||
error!(?err, "Failed to send message to queue event queue");
|
||||
ConException::InternalError
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
99
haesli_messaging/src/methods/queue.rs
Normal file
99
haesli_messaging/src/methods/queue.rs
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
use std::sync::{atomic::AtomicUsize, Arc};
|
||||
|
||||
use haesli_core::{
|
||||
connection::Channel,
|
||||
haesli_todo,
|
||||
methods::{Method, QueueBind, QueueDeclare, QueueDeclareOk},
|
||||
queue::{QueueDeletion, QueueId, QueueInner, QueueName},
|
||||
GlobalData,
|
||||
};
|
||||
use parking_lot::Mutex;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::{queue_worker::QueueTask, Result};
|
||||
|
||||
pub fn declare(channel: Channel, queue_declare: QueueDeclare) -> Result<Method> {
|
||||
let QueueDeclare {
|
||||
queue: queue_name,
|
||||
passive,
|
||||
durable,
|
||||
exclusive,
|
||||
auto_delete,
|
||||
no_wait,
|
||||
arguments,
|
||||
..
|
||||
} = queue_declare;
|
||||
|
||||
let queue_name = QueueName::new(queue_name.into());
|
||||
|
||||
if !arguments.is_empty() {
|
||||
haesli_todo!();
|
||||
}
|
||||
|
||||
// todo: durable is technically spec-compliant, the spec doesn't really require it, but it's a todo
|
||||
// not checked here because it's the default for amqplib which is annoying
|
||||
if passive || no_wait {
|
||||
haesli_todo!();
|
||||
}
|
||||
|
||||
let global_data = channel.global_data.clone();
|
||||
|
||||
let (event_send, event_recv) = mpsc::channel(10);
|
||||
|
||||
let id = QueueId::random();
|
||||
let queue = Arc::new(QueueInner {
|
||||
id,
|
||||
name: queue_name.clone(),
|
||||
messages: haesli_datastructure::MessageQueue::new(),
|
||||
durable,
|
||||
exclusive: exclusive.then(|| channel.id),
|
||||
deletion: if auto_delete {
|
||||
QueueDeletion::Auto(AtomicUsize::default())
|
||||
} else {
|
||||
QueueDeletion::Manual
|
||||
},
|
||||
consumers: Mutex::default(),
|
||||
event_send,
|
||||
});
|
||||
|
||||
{
|
||||
let mut global_data_lock = global_data.lock();
|
||||
|
||||
global_data_lock
|
||||
.queues
|
||||
.entry(queue_name.clone())
|
||||
.or_insert_with(|| queue.clone());
|
||||
}
|
||||
|
||||
bind_queue(global_data.clone(), (), queue_name.clone().into_inner())?;
|
||||
|
||||
let queue_task = QueueTask::new(global_data, event_recv, queue);
|
||||
|
||||
tokio::spawn(async move { queue_task.start().await });
|
||||
|
||||
Ok(Method::QueueDeclareOk(QueueDeclareOk {
|
||||
queue: queue_name.to_string(),
|
||||
message_count: 0,
|
||||
consumer_count: 0,
|
||||
}))
|
||||
}
|
||||
|
||||
pub async fn bind(_channel_handle: Channel, _queue_bind: QueueBind) -> Result<Method> {
|
||||
haesli_todo!();
|
||||
}
|
||||
|
||||
fn bind_queue(global_data: GlobalData, _exchange: (), routing_key: Arc<str>) -> Result<()> {
|
||||
let mut global_data = global_data.lock();
|
||||
|
||||
// todo: don't
|
||||
let queue = global_data
|
||||
.queues
|
||||
.get(&QueueName::new(routing_key.clone()))
|
||||
.unwrap()
|
||||
.clone();
|
||||
global_data
|
||||
.default_exchange
|
||||
.insert(routing_key.to_string(), queue);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue