use super::{channel::Channel, connection::Connection};
use crate::api::Result;
use crate::frame::Cancel;
use crate::{
frame::{Ack, Close, CloseChannel, Nack, Return},
BasicProperties,
};
use async_trait::async_trait;
#[cfg(feature = "traces")]
use tracing::{error, info, warn};
#[async_trait]
pub trait ConnectionCallback {
async fn close(&mut self, connection: &Connection, close: Close) -> Result<()>;
async fn blocked(&mut self, connection: &Connection, reason: String);
async fn unblocked(&mut self, connection: &Connection);
}
pub struct DefaultConnectionCallback;
#[async_trait]
impl ConnectionCallback for DefaultConnectionCallback {
async fn close(&mut self, connection: &Connection, close: Close) -> Result<()> {
#[cfg(feature = "traces")]
error!(
"handle close request for connection {}, cause: {}",
connection, close
);
Ok(())
}
async fn blocked(&mut self, connection: &Connection, reason: String) {
#[cfg(feature = "traces")]
info!(
"handle blocked notification for connection {}, reason: {}",
connection, reason
);
}
async fn unblocked(&mut self, connection: &Connection) {
#[cfg(feature = "traces")]
info!(
"handle unblocked notification for connection {}",
connection
);
}
}
#[async_trait]
pub trait ChannelCallback {
async fn close(&mut self, channel: &Channel, close: CloseChannel) -> Result<()>;
async fn cancel(&mut self, channel: &Channel, cancel: Cancel) -> Result<()>;
async fn flow(&mut self, channel: &Channel, active: bool) -> Result<bool>;
async fn publish_ack(&mut self, channel: &Channel, ack: Ack);
async fn publish_nack(&mut self, channel: &Channel, nack: Nack);
async fn publish_return(
&mut self,
channel: &Channel,
ret: Return,
basic_properties: BasicProperties,
content: Vec<u8>,
);
}
pub struct DefaultChannelCallback;
#[async_trait]
impl ChannelCallback for DefaultChannelCallback {
async fn close(&mut self, channel: &Channel, close: CloseChannel) -> Result<()> {
#[cfg(feature = "traces")]
error!(
"handle close request for channel {}, cause: {}",
channel, close
);
Ok(())
}
async fn cancel(&mut self, channel: &Channel, cancel: Cancel) -> Result<()> {
#[cfg(feature = "traces")]
warn!(
"handle cancel request for consumer {} on channel {}",
cancel.consumer_tag(),
channel
);
Ok(())
}
async fn flow(&mut self, channel: &Channel, active: bool) -> Result<bool> {
#[cfg(feature = "traces")]
info!(
"handle flow request active={} for channel {}",
active, channel
);
Ok(true)
}
async fn publish_ack(&mut self, channel: &Channel, ack: Ack) {
#[cfg(feature = "traces")]
info!(
"handle publish ack delivery_tag={} on channel {}",
ack.delivery_tag(),
channel
);
}
async fn publish_nack(&mut self, channel: &Channel, nack: Nack) {
#[cfg(feature = "traces")]
warn!(
"handle publish nack delivery_tag={} on channel {}",
nack.delivery_tag(),
channel
);
}
async fn publish_return(
&mut self,
channel: &Channel,
ret: Return,
_basic_properties: BasicProperties,
_content: Vec<u8>,
) {
#[cfg(feature = "traces")]
warn!("handle publish return {} on channel {}", ret, channel);
}
}