rename lol

This commit is contained in:
nora 2022-03-19 14:27:30 +01:00
parent c68cd04af7
commit 543e39f129
70 changed files with 283 additions and 266 deletions

View file

@ -0,0 +1,9 @@
#![warn(rust_2018_idioms)]
#![deny(clippy::future_not_send)]
use haesli_core::error::ProtocolError;
pub mod methods;
mod queue_worker;
type Result<T> = std::result::Result<T, ProtocolError>;

View 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)
}

View 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)
}

View 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(())
}

View 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(())
}

View file

@ -0,0 +1,105 @@
use std::borrow::Borrow;
use haesli_core::{
connection::ConnectionEvent,
consumer::Consumer,
message::Message,
methods::{BasicDeliver, Method},
queue::{Queue, QueueEvent, QueueEventReceiver},
GlobalData,
};
use tracing::info;
#[derive(Debug)]
#[allow(dead_code)]
pub struct QueueTask {
global_data: GlobalData,
event_recv: QueueEventReceiver,
queue: Queue,
}
impl QueueTask {
fn show_name(&self) -> &str {
self.queue.name.borrow()
}
pub fn new(global_data: GlobalData, event_recv: QueueEventReceiver, queue: Queue) -> Self {
Self {
global_data,
event_recv,
queue,
}
}
#[tracing::instrument(skip(self), fields(name = self.show_name()))]
pub async fn start(mut self) {
info!("Started queue worker task");
loop {
let next_event = self.event_recv.recv().await;
match next_event {
Some(QueueEvent::PublishMessage(message)) => {
self.handle_publish_message(message).await
}
Some(QueueEvent::Shutdown) | None => {
self.cleanup().await;
return;
}
}
}
}
#[tracing::instrument(skip(self), fields(name = self.show_name()), level = "debug")]
async fn handle_publish_message(&mut self, message: Message) {
// todo: we just send it to the consumer directly and ignore it if the consumer doesn't exist
// consuming is hard, but this should work *for now*
let could_deliver = {
let consumers = self.queue.consumers.lock();
if let Some(consumer) = consumers.values().next() {
Self::try_deliver(&message, consumer)
} else {
Err(())
}
};
if let Err(()) = could_deliver {
self.queue_message(message).await;
}
}
#[tracing::instrument(skip(consumer), level = "trace")]
fn try_deliver(message: &Message, consumer: &Consumer) -> Result<(), ()> {
let routing = &message.routing;
let method = Box::new(Method::BasicDeliver(BasicDeliver {
consumer_tag: consumer.tag.clone(),
delivery_tag: 0,
redelivered: false,
exchange: routing.exchange.clone(),
routing_key: routing.routing_key.clone(),
}));
let result = consumer
.channel
.event_sender
.try_send(ConnectionEvent::MethodContent(
consumer.channel.num,
method,
message.header.clone(),
message.content.clone(),
));
result.map_err(drop)
}
#[tracing::instrument(skip(self), fields(name = self.show_name()), level = "trace")]
async fn queue_message(&mut self, message: Message) {
self.queue.messages.append(message);
}
async fn cleanup(&mut self) {
// do stuff or something like that id whatever
}
}