client: added timeout
- also created a new basiccalloptions that includes message versioning and such server: now message versioning works.
This commit is contained in:
parent
755cf6467d
commit
ee1bb1abdf
|
@ -1,7 +1,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
fmt::{Debug, Display},
|
fmt::{Debug, Display},
|
||||||
str::from_utf8,
|
str::from_utf8,
|
||||||
time::Instant,
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
|
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
|
@ -10,6 +10,7 @@ use lapin::{
|
||||||
ConnectionProperties,
|
ConnectionProperties,
|
||||||
};
|
};
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
use tokio::time::timeout;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::ResultHeader;
|
use crate::ResultHeader;
|
||||||
|
@ -47,7 +48,7 @@ impl Client {
|
||||||
pub async fn rpc_call<T: RPCClientTask + Send + Debug>(
|
pub async fn rpc_call<T: RPCClientTask + Send + Debug>(
|
||||||
&self,
|
&self,
|
||||||
data: T,
|
data: T,
|
||||||
queue_name: &str,
|
options: BasicCallOptions,
|
||||||
) -> Result<Result<T::Result, T::ErroredResult>, RpcClientError>
|
) -> Result<Result<T::Result, T::ErroredResult>, RpcClientError>
|
||||||
where
|
where
|
||||||
T: Serialize + DeserializeOwned,
|
T: Serialize + DeserializeOwned,
|
||||||
|
@ -94,7 +95,7 @@ impl Client {
|
||||||
match channel
|
match channel
|
||||||
.basic_publish(
|
.basic_publish(
|
||||||
"",
|
"",
|
||||||
queue_name,
|
format!("{}-{}", &options.queue_name, options.message_version).as_str(),
|
||||||
BasicPublishOptions::default(),
|
BasicPublishOptions::default(),
|
||||||
serde_json::to_string(&data).unwrap().as_bytes(),
|
serde_json::to_string(&data).unwrap().as_bytes(),
|
||||||
BasicProperties::default()
|
BasicProperties::default()
|
||||||
|
@ -112,18 +113,18 @@ impl Client {
|
||||||
}
|
}
|
||||||
Ok(confirmation) => {
|
Ok(confirmation) => {
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
"Sent RPC job of type {} to channel {} Ack: {}",
|
"Sent RPC job of type {} to channel {} Ack: {} Ver: {}",
|
||||||
std::any::type_name::<T>(),
|
std::any::type_name::<T>(),
|
||||||
queue_name,
|
options.queue_name,
|
||||||
confirmation.is_ack()
|
confirmation.is_ack(),
|
||||||
|
options.message_version
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO implement timeout
|
|
||||||
tracing::debug!("Awaiting response from callback queue");
|
tracing::debug!("Awaiting response from callback queue");
|
||||||
let del = loop {
|
let listen = async move {
|
||||||
match consumer.next().await {
|
match consumer.next().await {
|
||||||
None => {
|
None => {
|
||||||
tracing::error!("Received empty data after {:?}", now.elapsed());
|
tracing::error!("Received empty data after {:?}", now.elapsed());
|
||||||
|
@ -141,12 +142,26 @@ impl Client {
|
||||||
}
|
}
|
||||||
Ok(del) => {
|
Ok(del) => {
|
||||||
tracing::debug!("Received response after {:?}", now.elapsed());
|
tracing::debug!("Received response after {:?}", now.elapsed());
|
||||||
break del;
|
return Ok(del);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let del = match options.timeout {
|
||||||
|
None => listen.await?,
|
||||||
|
Some(dur) => match timeout(dur, listen).await {
|
||||||
|
Err(elapsed) => {
|
||||||
|
tracing::warn!("RPC job has reached timeout after: {}", elapsed);
|
||||||
|
return Err(RpcClientError::TimeoutReached);
|
||||||
|
}
|
||||||
|
Ok(r) => match r {
|
||||||
|
Err(error) => return Err(error),
|
||||||
|
Ok(r) => r,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
// TODO better implementation of this
|
// TODO better implementation of this
|
||||||
tracing::debug!("Decoding headers");
|
tracing::debug!("Decoding headers");
|
||||||
let result_type = match del.properties.headers().to_owned() {
|
let result_type = match del.properties.headers().to_owned() {
|
||||||
|
@ -181,6 +196,7 @@ impl Client {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
tracing::debug!("Result type is: {result_type}, decoding...");
|
tracing::debug!("Result type is: {result_type}, decoding...");
|
||||||
let utf8 = match from_utf8(&del.data) {
|
let utf8 = match from_utf8(&del.data) {
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
|
@ -190,7 +206,7 @@ impl Client {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let _ = channel.close(0, "byebye").await;
|
let _ = channel.close(0, "byebye").await;
|
||||||
// acking for idk reason
|
|
||||||
let _ = del.ack(BasicAckOptions::default()).await;
|
let _ = del.ack(BasicAckOptions::default()).await;
|
||||||
match result_type {
|
match result_type {
|
||||||
ResultHeader::Error => match serde_json::from_str::<T::ErroredResult>(utf8) {
|
ResultHeader::Error => match serde_json::from_str::<T::ErroredResult>(utf8) {
|
||||||
|
@ -216,16 +232,9 @@ impl Client {
|
||||||
}
|
}
|
||||||
// ack message
|
// ack message
|
||||||
}
|
}
|
||||||
/// Sends a message to the queue
|
|
||||||
///
|
/// Sends a basic Task to the queue
|
||||||
/// # Examples
|
pub async fn call<T>(&self, data: T, options: BasicCallOptions) -> Result<(), ClientError>
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use bunbun_worker::client::Client;
|
|
||||||
/// let client = Client::new("amqp://127.0.0.1:5672");
|
|
||||||
/// let result = client.call(EmailJob::new("someone@example.com", "Hello there"), "email-emailjob-v1.0.0");
|
|
||||||
/// ```
|
|
||||||
pub async fn call<T>(&self, data: T, queue_name: &str) -> Result<(), ClientError>
|
|
||||||
where
|
where
|
||||||
T: Serialize + DeserializeOwned,
|
T: Serialize + DeserializeOwned,
|
||||||
{
|
{
|
||||||
|
@ -233,7 +242,7 @@ impl Client {
|
||||||
match channel
|
match channel
|
||||||
.basic_publish(
|
.basic_publish(
|
||||||
"",
|
"",
|
||||||
queue_name,
|
format!("{}-{}", &options.queue_name, options.message_version).as_str(),
|
||||||
BasicPublishOptions::default(),
|
BasicPublishOptions::default(),
|
||||||
serde_json::to_string(&data).unwrap().as_bytes(),
|
serde_json::to_string(&data).unwrap().as_bytes(),
|
||||||
BasicProperties::default(),
|
BasicProperties::default(),
|
||||||
|
@ -254,10 +263,11 @@ impl Client {
|
||||||
Ok(confirmation) => {
|
Ok(confirmation) => {
|
||||||
let _ = channel.close(0, "byebye").await;
|
let _ = channel.close(0, "byebye").await;
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
"Sent nonRPC job of type {} to channel {} Ack: {}",
|
"Sent nonRPC job of type {} to channel {} Ack: {} Ver: {}",
|
||||||
std::any::type_name::<T>(),
|
std::any::type_name::<T>(),
|
||||||
queue_name,
|
options.queue_name,
|
||||||
confirmation.is_ack()
|
confirmation.is_ack(),
|
||||||
|
options.message_version
|
||||||
);
|
);
|
||||||
tracing::debug!(
|
tracing::debug!(
|
||||||
"AMQP confirmed dispatch of job | Acknowledged? {}",
|
"AMQP confirmed dispatch of job | Acknowledged? {}",
|
||||||
|
@ -269,15 +279,40 @@ impl Client {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// A call option class that is used to control how calls are handled
|
||||||
|
/// You can define the timeout, and the message versions
|
||||||
|
pub struct BasicCallOptions {
|
||||||
|
timeout: Option<Duration>,
|
||||||
|
queue_name: String,
|
||||||
|
message_version: String,
|
||||||
|
}
|
||||||
|
impl BasicCallOptions {
|
||||||
|
pub fn default(queue_name: impl Into<String>) -> Self {
|
||||||
|
Self {
|
||||||
|
timeout: None,
|
||||||
|
queue_name: queue_name.into(),
|
||||||
|
message_version: "v1.0.0".into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn message_version(mut self, message_version: impl Into<String>) -> Self {
|
||||||
|
self.message_version = message_version.into();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
pub fn timeout(mut self, timeout: Duration) -> Self {
|
||||||
|
self.timeout = Some(timeout);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// An error that the client returns
|
/// An error that the client returns
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum RpcClientError {
|
pub enum RpcClientError {
|
||||||
NoReply, // TODO timeout
|
NoReply,
|
||||||
FailedDecode,
|
FailedDecode,
|
||||||
FailedToSend,
|
FailedToSend,
|
||||||
InvalidResponse,
|
InvalidResponse,
|
||||||
ServerPanicked,
|
ServerPanicked,
|
||||||
|
TimeoutReached,
|
||||||
}
|
}
|
||||||
/// An error for normal calls
|
/// An error for normal calls
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
99
src/lib.rs
99
src/lib.rs
|
@ -90,7 +90,7 @@ impl WorkerConfig {
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
/// * `custom_tls` - Optional TLSconfig (if none defaults to lapins choice)
|
/// * `custom_tls` - Optional TLSconfig (if none defaults to lapins choice)
|
||||||
pub fn enable_tls(&mut self, custom_tls: Option<TlsConfig>) {
|
pub fn enable_tls(mut self, custom_tls: Option<TlsConfig>) -> Self {
|
||||||
match custom_tls {
|
match custom_tls {
|
||||||
Some(tls) => {
|
Some(tls) => {
|
||||||
let tls = OwnedTLSConfig {
|
let tls = OwnedTLSConfig {
|
||||||
|
@ -104,6 +104,45 @@ impl WorkerConfig {
|
||||||
}
|
}
|
||||||
None => self.tls = OwnedTLSConfig::default().into(),
|
None => self.tls = OwnedTLSConfig::default().into(),
|
||||||
}
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A worker configuration
|
||||||
|
|
||||||
|
pub struct ListenerConfig {
|
||||||
|
prefetch_count: u16,
|
||||||
|
queue_name: String,
|
||||||
|
consumer_tag: String,
|
||||||
|
message_version: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ListenerConfig {
|
||||||
|
/// Create a new listener config
|
||||||
|
/// # Arguments
|
||||||
|
/// * `queue_name` - The name of the queue to listen to (e.g. service-serviceJobName-v1.0.0)
|
||||||
|
pub fn default(queue_name: impl Into<String>) -> Self {
|
||||||
|
Self {
|
||||||
|
prefetch_count: 0,
|
||||||
|
queue_name: queue_name.into(),
|
||||||
|
consumer_tag: "".into(),
|
||||||
|
message_version: "v1.0.0".into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Set the prefetch count for the listener
|
||||||
|
/// This serves as a maximum job count that can be processed at a time. (0 is unlimited)
|
||||||
|
pub fn set_prefetch_count(mut self, prefetch_count: u16) -> Self {
|
||||||
|
self.prefetch_count = prefetch_count;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
/// Set the consumer tag for the listener
|
||||||
|
pub fn set_consumer_tag(mut self, consumer_tag: impl Into<String>) -> Self {
|
||||||
|
self.consumer_tag = consumer_tag.into();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
pub fn set_message_version(mut self, version: impl Into<String>) -> Self {
|
||||||
|
self.message_version = version.into();
|
||||||
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,20 +193,24 @@ impl Worker {
|
||||||
/// Add a non-rpc listener to the worker object
|
/// Add a non-rpc listener to the worker object
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
/// * `queue_name` - A string slice that holds the name of the queue to listen to (e.g. service-serviceJobName-v1.0.0)
|
|
||||||
/// * `state` - An Arc of the state object that will be passed to the listener
|
/// * `state` - An Arc of the state object that will be passed to the listener
|
||||||
|
/// * `listener_config` - An Arc of the state object that will be passed to the listener
|
||||||
pub async fn add_non_rpc_consumer<J: Task + 'static + Send>(
|
pub async fn add_non_rpc_consumer<J: Task + 'static + Send>(
|
||||||
&mut self,
|
&mut self,
|
||||||
queue_name: &str,
|
|
||||||
state: Arc<J::State>,
|
state: Arc<J::State>,
|
||||||
|
listener_config: ListenerConfig,
|
||||||
) where
|
) where
|
||||||
<J as Task>::State: std::marker::Send + Sync,
|
<J as Task>::State: std::marker::Send + Sync,
|
||||||
{
|
{
|
||||||
let consumer = self
|
let consumer = self
|
||||||
.channel
|
.channel
|
||||||
.basic_consume(
|
.basic_consume(
|
||||||
queue_name,
|
format!(
|
||||||
"",
|
"{}-{}",
|
||||||
|
listener_config.queue_name, listener_config.message_version
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
&listener_config.consumer_tag,
|
||||||
BasicConsumeOptions::default(),
|
BasicConsumeOptions::default(),
|
||||||
FieldTable::default(),
|
FieldTable::default(),
|
||||||
)
|
)
|
||||||
|
@ -231,28 +274,33 @@ impl Worker {
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let server = BunBunWorker::new("amqp://localhost:5672", None).await;
|
/// let server = BunBunWorker::new("amqp://localhost:5672", None).await;
|
||||||
/// server.add_rpc_consumer::<MyRPCTask>("service-serviceJobName-v1.0.0", SomeState{} )).await;
|
/// server.add_rpc_consumer::<MyRPCTask>(ListenerConfig::default("service-jobname-v1.0.0") )).await;
|
||||||
/// server.start_all_listeners().await;
|
/// server.start_all_listeners().await;
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn add_rpc_consumer<J: RPCTask + 'static + Send>(
|
pub async fn add_rpc_consumer<J: RPCTask + 'static + Send>(
|
||||||
&mut self,
|
&mut self,
|
||||||
queue_name: &str,
|
|
||||||
state: Arc<J::State>,
|
state: Arc<J::State>,
|
||||||
|
listener_config: ListenerConfig,
|
||||||
) where
|
) where
|
||||||
<J as RPCTask>::State: std::marker::Send + Sync,
|
<J as RPCTask>::State: std::marker::Send + Sync,
|
||||||
<J as RPCTask>::Result: std::marker::Send + Sync,
|
<J as RPCTask>::Result: std::marker::Send + Sync,
|
||||||
<J as RPCTask>::ErroredResult: std::marker::Send + Sync,
|
<J as RPCTask>::ErroredResult: std::marker::Send + Sync,
|
||||||
{
|
{
|
||||||
let consumer = self
|
let consumer = create_consumer(
|
||||||
.channel
|
self.channel.clone(),
|
||||||
.basic_consume(
|
format!(
|
||||||
queue_name,
|
"{}-{}",
|
||||||
"",
|
listener_config.queue_name, listener_config.message_version
|
||||||
BasicConsumeOptions::default(),
|
)
|
||||||
FieldTable::default(),
|
.as_str(),
|
||||||
|
&listener_config.consumer_tag,
|
||||||
|
listener_config.prefetch_count,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.expect("basic_consume error");
|
.map_err(|e| {
|
||||||
|
tracing::error!("Failed to create consumer: {}", e);
|
||||||
|
})
|
||||||
|
.expect("Failed to create consumer");
|
||||||
|
|
||||||
let channel = self.channel.clone();
|
let channel = self.channel.clone();
|
||||||
let handler: Arc<
|
let handler: Arc<
|
||||||
|
@ -633,3 +681,24 @@ impl Display for ResultHeader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn create_consumer(
|
||||||
|
channel: Channel,
|
||||||
|
queue_name: &str,
|
||||||
|
consumer_tag: &str,
|
||||||
|
prefect_count: u16,
|
||||||
|
) -> Result<Consumer, lapin::Error> {
|
||||||
|
let channel = channel.clone();
|
||||||
|
channel
|
||||||
|
.basic_qos(prefect_count, BasicQosOptions::default())
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
channel
|
||||||
|
.basic_consume(
|
||||||
|
queue_name,
|
||||||
|
consumer_tag,
|
||||||
|
BasicConsumeOptions::default(),
|
||||||
|
FieldTable::default(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
|
@ -15,8 +15,8 @@ mod test {
|
||||||
use tracing_test::traced_test;
|
use tracing_test::traced_test;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
client::{Client, RPCClientTask},
|
client::{BasicCallOptions, Client, RPCClientTask},
|
||||||
RPCTask, Worker, WorkerConfig,
|
ListenerConfig, RPCTask, Worker, WorkerConfig,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -108,10 +108,10 @@ mod test {
|
||||||
.await;
|
.await;
|
||||||
listener
|
listener
|
||||||
.add_rpc_consumer::<EmailJob>(
|
.add_rpc_consumer::<EmailJob>(
|
||||||
"email-emailjob-v1.0.0",
|
|
||||||
Arc::new(State {
|
Arc::new(State {
|
||||||
something: "test".into(),
|
something: "test".into(),
|
||||||
}),
|
}),
|
||||||
|
ListenerConfig::default("emailjob").set_prefetch_count(100),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
tracing::debug!("Starting listener");
|
tracing::debug!("Starting listener");
|
||||||
|
@ -129,10 +129,10 @@ mod test {
|
||||||
.await;
|
.await;
|
||||||
listener
|
listener
|
||||||
.add_rpc_consumer::<PanickingEmailJob>(
|
.add_rpc_consumer::<PanickingEmailJob>(
|
||||||
"email-emailjob-v1.0.0",
|
|
||||||
Arc::new(State {
|
Arc::new(State {
|
||||||
something: "test".into(),
|
something: "test".into(),
|
||||||
}),
|
}),
|
||||||
|
ListenerConfig::default("emailjob").set_prefetch_count(100),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
tracing::debug!("Starting listener");
|
tracing::debug!("Starting listener");
|
||||||
|
@ -143,7 +143,7 @@ mod test {
|
||||||
#[traced_test]
|
#[traced_test]
|
||||||
async fn rpc_client() {
|
async fn rpc_client() {
|
||||||
//
|
//
|
||||||
let mut client = Client::new(env::var("AMQP_SERVER_URL").unwrap().as_str())
|
let client = Client::new(env::var("AMQP_SERVER_URL").unwrap().as_str())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let result = client
|
let result = client
|
||||||
|
@ -152,7 +152,31 @@ mod test {
|
||||||
send_to: "someone".into(),
|
send_to: "someone".into(),
|
||||||
contents: "something".into(),
|
contents: "something".into(),
|
||||||
},
|
},
|
||||||
"email-emailjob-v1.0.0",
|
BasicCallOptions::default("emailjob"),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
result,
|
||||||
|
Ok(EmailJobResult {
|
||||||
|
contents: "something".to_string()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
#[test(tokio::test)]
|
||||||
|
#[traced_test]
|
||||||
|
async fn rpc_client_timeout() {
|
||||||
|
//
|
||||||
|
let client = Client::new(env::var("AMQP_SERVER_URL").unwrap().as_str())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let result = client
|
||||||
|
.rpc_call::<EmailJob>(
|
||||||
|
EmailJob {
|
||||||
|
send_to: "someone".into(),
|
||||||
|
contents: "something".into(),
|
||||||
|
},
|
||||||
|
BasicCallOptions::default("emailjob").timeout(Duration::from_secs(3)),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -184,7 +208,7 @@ mod test {
|
||||||
send_to: "someone".into(),
|
send_to: "someone".into(),
|
||||||
contents: "something".into(),
|
contents: "something".into(),
|
||||||
},
|
},
|
||||||
"email-emailjob-v1.0.0",
|
BasicCallOptions::default("emailjob"),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
}));
|
}));
|
||||||
|
|
Loading…
Reference in a new issue