use super::{
config::{PulsarAuthentication, SinkConfig, SourceConfig, validate_service_url},
sink::PulsarSink,
source::PulsarSource,
};
use crate::error::{BoxError, Error};
use magnetar::{PulsarClient, proto::SupervisorConfig};
use std::{fmt, sync::Arc};
use tokio::{runtime::Handle, sync::OnceCell};
#[derive(Clone)]
pub struct Pulsar {
shared: Arc<Shared>,
}
struct Shared {
service_url: String,
authentication: Option<PulsarAuthentication>,
client: OnceCell<PulsarClient>,
}
impl Pulsar {
pub fn new(service_url: impl Into<String>) -> Self {
Self::from_parts(service_url.into(), None)
}
pub fn token(self, token: impl Into<Vec<u8>>) -> Self {
self.authentication(PulsarAuthentication::token(token))
}
pub fn authentication(self, authentication: PulsarAuthentication) -> Self {
Self::from_parts(self.shared.service_url.clone(), Some(authentication))
}
pub fn source<C: Default>(
&self,
topic: impl Into<String>,
subscription: impl Into<String>,
) -> PulsarSource<C> {
self.source_with_codec(C::default(), topic, subscription)
}
pub fn source_with_codec<C>(
&self,
codec: C,
topic: impl Into<String>,
subscription: impl Into<String>,
) -> PulsarSource<C> {
PulsarSource::new(
self.clone(),
SourceConfig::new(topic.into(), subscription.into()),
codec,
)
}
pub fn sink<C: Default>(&self, topic: impl Into<String>) -> PulsarSink<C> {
self.sink_with_codec(C::default(), topic)
}
pub fn sink_with_codec<C>(&self, codec: C, topic: impl Into<String>) -> PulsarSink<C> {
PulsarSink::new(self.clone(), SinkConfig::new(topic.into()), codec)
}
pub fn validate(&self) -> Result<(), Error> {
validate_service_url(&self.shared.service_url)?;
if let Some(authentication) = &self.shared.authentication {
authentication.validate()?;
}
Ok(())
}
pub(super) async fn client(&self) -> Result<&PulsarClient, BoxError> {
self.shared
.client
.get_or_try_init(|| async {
self.validate()?;
let mut builder = PulsarClient::builder()
.service_url(&self.shared.service_url)
.enable_reconnect(SupervisorConfig::default());
if let Some(authentication) = &self.shared.authentication {
builder = builder.auth(authentication.provider());
}
Ok(builder.build().await?)
})
.await
}
fn from_parts(service_url: String, authentication: Option<PulsarAuthentication>) -> Self {
Self {
shared: Arc::new(Shared {
service_url,
authentication,
client: OnceCell::new(),
}),
}
}
}
impl fmt::Debug for Pulsar {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Pulsar")
.field("service_url", &self.shared.service_url)
.field("authentication", &self.shared.authentication)
.finish_non_exhaustive()
}
}
impl Drop for Shared {
fn drop(&mut self) {
if let (Some(client), Ok(runtime)) = (self.client.take(), Handle::try_current()) {
runtime.spawn(client.close());
}
}
}
pub(super) async fn partition_topics(
client: &PulsarClient,
topic: &str,
) -> Result<Vec<String>, BoxError> {
let partitions = client.partitions_for_topic(topic).await?;
Ok(if partitions == 0 {
vec![topic.to_owned()]
} else {
(0..partitions)
.map(|partition| format!("{topic}-partition-{partition}"))
.collect()
})
}