use std::collections::HashMap;
use std::sync::Arc;
use async_trait::async_trait;
use serde::Serialize;
use tokio::sync::mpsc;
use varpulis_core::Event;
use crate::sink::Sink;
use crate::types::ConnectorError;
#[derive(Debug, Clone, Serialize)]
pub struct ConnectorHealthReport {
pub connected: bool,
pub last_error: Option<String>,
pub messages_received: u64,
pub seconds_since_last_message: u64,
pub circuit_breaker_state: String,
pub circuit_breaker_failures: u64,
pub circuit_breaker_rejections: u64,
}
impl Default for ConnectorHealthReport {
fn default() -> Self {
Self {
connected: true,
last_error: None,
messages_received: 0,
seconds_since_last_message: 0,
circuit_breaker_state: "closed".to_string(),
circuit_breaker_failures: 0,
circuit_breaker_rejections: 0,
}
}
}
#[async_trait]
pub trait ManagedConnector: Send + Sync {
fn name(&self) -> &str;
fn connector_type(&self) -> &str;
async fn start_source(
&mut self,
topic: &str,
tx: mpsc::Sender<Event>,
params: &HashMap<String, String>,
) -> Result<(), ConnectorError>;
fn create_sink(
&mut self,
topic: &str,
params: &HashMap<String, String>,
) -> Result<Arc<dyn Sink>, ConnectorError>;
fn health(&self) -> ConnectorHealthReport {
ConnectorHealthReport::default()
}
fn converter(&self) -> Box<dyn crate::converter::Converter> {
Box::new(crate::converter::json::JsonConverter)
}
async fn shutdown(&mut self) -> Result<(), ConnectorError>;
}