use crate::error::{Error, ensure};
use crate::{adapters::pending::PendingLimit, retry::RetryPolicy};
use bytes::Bytes;
use magnetar::proto::{AuthError, AuthProvider, pb::command_subscribe::SubType};
use std::{fmt, sync::Arc, time::Duration};
#[derive(Clone)]
pub struct PulsarAuthentication {
pub name: String,
pub data: Vec<u8>,
}
impl fmt::Debug for PulsarAuthentication {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PulsarAuthentication")
.field("name", &self.name)
.finish_non_exhaustive()
}
}
impl PulsarAuthentication {
pub fn token(token: impl Into<Vec<u8>>) -> Self {
Self {
name: "token".into(),
data: token.into(),
}
}
pub(super) fn provider(&self) -> Arc<dyn AuthProvider> {
Arc::new(self.clone())
}
pub(super) fn validate(&self) -> Result<(), Error> {
ensure!(
!self.name.trim().is_empty(),
Error::config,
"Pulsar auth method is required"
);
ensure!(
!self.data.is_empty(),
Error::config,
"Pulsar auth data is required"
);
Ok(())
}
}
impl AuthProvider for PulsarAuthentication {
fn method(&self) -> &str {
&self.name
}
fn initial(&self) -> Result<Bytes, AuthError> {
Ok(Bytes::copy_from_slice(&self.data))
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum PulsarSubscriptionType {
Exclusive,
#[default]
Shared,
Failover,
KeyShared,
}
impl From<PulsarSubscriptionType> for SubType {
fn from(value: PulsarSubscriptionType) -> Self {
match value {
PulsarSubscriptionType::Exclusive => Self::Exclusive,
PulsarSubscriptionType::Shared => Self::Shared,
PulsarSubscriptionType::Failover => Self::Failover,
PulsarSubscriptionType::KeyShared => Self::KeyShared,
}
}
}
#[derive(Clone, Debug)]
pub(super) struct SourceConfig {
pub(super) topic: String,
pub(super) subscription: String,
pub(super) subscription_type: PulsarSubscriptionType,
pub(super) buffer_size: usize,
pub(super) empty_payload_is_tombstone: bool,
pub(super) ack_retry: RetryPolicy,
}
impl SourceConfig {
pub(super) fn new(topic: String, subscription: String) -> Self {
Self {
topic,
subscription,
subscription_type: PulsarSubscriptionType::Shared,
buffer_size: 100,
empty_payload_is_tombstone: true,
ack_retry: RetryPolicy {
max_attempts: 5,
initial_delay: Duration::from_millis(100),
max_delay: Duration::from_secs(2),
..RetryPolicy::default()
},
}
}
pub(super) fn validate(&self) -> Result<(), Error> {
validate_topic(&self.topic)?;
ensure!(
!self.subscription.trim().is_empty(),
Error::config,
"Pulsar subscription is required"
);
ensure!(
self.buffer_size > 0,
Error::config,
"Pulsar buffer size must be greater than zero"
);
self.ack_retry.validate()
}
}
#[derive(Clone, Debug)]
pub(super) struct SinkConfig {
pub(super) topic: String,
pub(super) producer_name: Option<String>,
pub(super) max_pending: usize,
pub(super) send_retry: RetryPolicy,
}
impl SinkConfig {
pub(super) fn new(topic: String) -> Self {
Self {
topic,
producer_name: None,
max_pending: 1000,
send_retry: RetryPolicy {
max_attempts: 10,
initial_delay: Duration::from_millis(100),
max_delay: Duration::from_secs(5),
..RetryPolicy::default()
},
}
}
pub(super) fn validate(&self) -> Result<(), Error> {
validate_topic(&self.topic)?;
if let Some(name) = &self.producer_name {
ensure!(
!name.trim().is_empty(),
Error::config,
"Pulsar producer name must not be empty"
);
}
PendingLimit::validate(self.max_pending, "Pulsar")?;
self.send_retry.validate()
}
}
pub(super) fn validate_service_url(service_url: &str) -> Result<(), Error> {
ensure!(
!service_url.trim().is_empty(),
Error::config,
"Pulsar service URL is required"
);
ensure!(
!service_url.chars().any(char::is_whitespace),
Error::config,
"Pulsar service URL must not contain whitespace"
);
ensure!(
service_url.starts_with("pulsar://") || service_url.starts_with("pulsar+ssl://"),
Error::config,
"Pulsar service URL must use pulsar:// or pulsar+ssl://"
);
Ok(())
}
fn validate_topic(topic: &str) -> Result<(), Error> {
ensure!(
!topic.trim().is_empty(),
Error::config,
"Pulsar topic is required"
);
Ok(())
}