Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ampq #15

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/rsjudge-rabbitmq/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rsjudge_traits::Config;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RabbitMqConfig {
pub uri: String,
pub queue_name: String,
}

#[cfg(feature = "serde")]
Expand Down
53 changes: 49 additions & 4 deletions crates/rsjudge-rabbitmq/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

use amqprs::{
callbacks::{DefaultChannelCallback, DefaultConnectionCallback},
connection::{Connection, OpenConnectionArguments},
callbacks::{ DefaultChannelCallback, DefaultConnectionCallback}, channel::{BasicAckArguments, BasicConsumeArguments, BasicPublishArguments, BasicQosArguments, Channel, QueueDeclareArguments}, connection::{Connection, OpenConnectionArguments}, BasicProperties, Deliver
};

use crate::config::RabbitMqConfig;
Expand All @@ -13,17 +12,63 @@ mod error;

pub async fn register(config: RabbitMqConfig) -> Result<()> {
// Build arguments for new connection.
let args = OpenConnectionArguments::try_from(&*config.uri)?;
let connection = Connection::open(&args).await?;
let conn_args = OpenConnectionArguments::try_from(&*config.uri)?;
let connection = Connection::open(&conn_args).await?;
connection
.register_callback(DefaultConnectionCallback)
.await?;

let channel = connection.open_channel(None).await?;
channel.register_callback(DefaultChannelCallback).await?;
channel.flow(true).await?;
channel.basic_qos(BasicQosArguments::default().prefetch_count(1).finish()).await?;

let queue_name = config.queue_name;
let queue_args = QueueDeclareArguments::new(&queue_name).durable(true).finish();
channel.queue_declare(queue_args).await?;

let consumer_args = BasicConsumeArguments::new("rpc_queue", "");
let (_, mut rx) = channel.basic_consume_rx(consumer_args).await?;

while let Some(message) = rx.recv().await {
if let Some(payload) = message.content {
on_request(
&channel,
message.deliver.unwrap(),
message.basic_properties.unwrap(),
payload,
)
.await?;
}
}

// Gracefully shutdown.
channel.close().await?;
connection.close().await?;
Ok(())
}

async fn on_request(channel: &Channel, method: Deliver, props: BasicProperties, payload: Vec<u8>) -> Result<()>{
let content = std::str::from_utf8(&payload).unwrap_or("").to_string();
let response = process(content).await.into_bytes();

let publish_args = BasicPublishArguments::new("", props.reply_to().unwrap_or(&"".to_string()));

let properties = BasicProperties::default()
.with_correlation_id(props.correlation_id().unwrap_or(&"".to_string()))
.finish();

channel
.basic_publish(properties, response, publish_args)
.await?;

channel
.basic_ack(BasicAckArguments::new(method.delivery_tag(), false))
.await?;

Ok(())
}

async fn process(s: String) -> String {
s
}
3 changes: 2 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ mod tests {
},
#[cfg(feature = "rabbitmq")]
rabbitmq: RabbitMqConfig {
uri: "amqp://user:bitnami@localhost".to_owned()
uri: "amqp://user:bitnami@localhost".to_owned(),
queue_name: "rpc_queue".to_owned(),
},
})?
);
Expand Down