use super::{
config::SinkConfig,
connection::{RabbitMq, Role, close},
convert::{properties_to_amqp, short_string},
record::RabbitMqPublish,
};
use crate::{
adapters::pending::PendingLimit,
codec::Encoder,
error::{BoxError, Context, Error, ensure},
sink::{Completion, Sink},
};
use lapin::{
BasicProperties, Channel, Confirmation,
options::{BasicPublishOptions, ConfirmSelectOptions},
types::ShortString,
};
use std::{
fmt,
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
};
use tokio::sync::Mutex;
#[derive(Clone)]
pub struct RabbitMqPrepared {
routing_key: ShortString,
properties: BasicProperties,
payload: Vec<u8>,
}
impl RabbitMqPrepared {
pub fn routing_key(&self) -> &str {
self.routing_key.as_str()
}
pub fn payload(&self) -> &[u8] {
&self.payload
}
}
impl fmt::Debug for RabbitMqPrepared {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RabbitMqPrepared")
.field("routing_key", &self.routing_key.as_str())
.field("properties", &self.properties)
.field("payload_len", &self.payload.len())
.finish()
}
}
struct State {
channel: Mutex<Option<Channel>>,
closed: AtomicBool,
pending: PendingLimit,
}
impl State {
fn new(max_pending: usize) -> Arc<Self> {
Arc::new(Self {
channel: Mutex::new(None),
closed: AtomicBool::new(false),
pending: PendingLimit::new(max_pending, "RabbitMQ"),
})
}
}
pub struct RabbitMqSink<C> {
rabbit: RabbitMq,
config: SinkConfig,
codec: Arc<C>,
state: Arc<State>,
}
impl<C> Clone for RabbitMqSink<C> {
fn clone(&self) -> Self {
Self {
rabbit: self.rabbit.clone(),
config: self.config.clone(),
codec: self.codec.clone(),
state: self.state.clone(),
}
}
}
impl<C> RabbitMqSink<C> {
pub(super) fn new(rabbit: RabbitMq, config: SinkConfig, codec: C) -> Self {
Self {
rabbit,
state: State::new(config.max_pending),
config,
codec: Arc::new(codec),
}
}
pub fn persistent(mut self, persistent: bool) -> Self {
self.config.persistent = persistent;
self
}
pub fn mandatory(mut self, mandatory: bool) -> Self {
self.config.mandatory = mandatory;
self
}
pub fn max_pending(mut self, max_pending: usize) -> Self {
self.config.max_pending = max_pending;
self.state = State::new(max_pending);
self
}
pub fn validate(&self) -> Result<(), Error> {
self.rabbit.validate()?;
self.config.validate()
}
async fn channel(&self) -> Result<Channel, BoxError> {
let mut slot = self.state.channel.lock().await;
ensure!(
!self.state.closed.load(Ordering::Acquire),
Error::closed,
"RabbitMQ sink is closed"
);
if let Some(channel) = slot.as_ref() {
if channel.status().connected() {
return Ok(channel.clone());
}
*slot = None;
}
self.validate()?;
let channel = self.rabbit.channel(Role::Publish).await?;
if let Err(error) = channel
.confirm_select(ConfirmSelectOptions::default())
.await
.context("enabling RabbitMQ publisher confirms")
{
let _ = close(&channel).await;
return Err(error.into());
}
*slot = Some(channel.clone());
Ok(channel)
}
}
impl<C> Sink<RabbitMqPublish<C::Value>> for RabbitMqSink<C>
where
C: Encoder<Value: Send + Sync + 'static>,
{
type Prepared = RabbitMqPrepared;
fn prepare(&self, output: RabbitMqPublish<C::Value>) -> Result<Self::Prepared, BoxError> {
let routing_key = output
.routing_key
.as_deref()
.unwrap_or(&self.config.routing_key);
Ok(RabbitMqPrepared {
routing_key: short_string("routing key", routing_key)?,
properties: properties_to_amqp(
&output.properties,
&output.headers,
self.config.persistent,
)?,
payload: self.codec.encode(&output.value)?,
})
}
async fn publish(&self, output: &Self::Prepared) -> Result<(), BoxError> {
self.submit(output).await?.wait().await
}
async fn submit(&self, output: &Self::Prepared) -> Result<Completion, BoxError> {
let permit = self.state.pending.acquire().await?;
let channel = self.channel().await?;
let options = BasicPublishOptions {
mandatory: self.config.mandatory,
immediate: false,
};
let confirm = channel
.basic_publish(
short_string("exchange name", &self.config.exchange)?,
output.routing_key.clone(),
options,
&output.payload,
output.properties.clone(),
)
.await
.context("publishing to RabbitMQ")?;
Ok(Completion::pending(async move {
let _permit = permit;
confirmed(
confirm
.await
.context("waiting for a RabbitMQ publisher confirmation")?,
)
}))
}
async fn close(&self) -> Result<(), BoxError> {
self.state.closed.store(true, Ordering::Release);
self.state.pending.close();
let channel = self.state.channel.lock().await.take();
if let Some(channel) = channel {
let _ = channel.wait_for_confirms().await;
close(&channel).await?;
}
Ok(())
}
}
fn confirmed(confirmation: Confirmation) -> Result<(), BoxError> {
match confirmation {
Confirmation::Ack(None) | Confirmation::NotRequested => Ok(()),
Confirmation::Ack(Some(returned)) => Err(Error::msg(format!(
"RabbitMQ returned an unroutable message: {} {}",
returned.reply_code, returned.reply_text
))
.into()),
Confirmation::Nack(_) => {
Err(Error::msg("RabbitMQ did not accept a published message").into())
}
}
}