pub(crate) mod batch_consumer;
pub(crate) mod debug_consumer;
pub(crate) mod log_consumer;
pub use batch_consumer::{
BatchConsumer, BatchConsumerConfig, DEFAULT_BATCH_SIZE, DEFAULT_CACHE_CAPACITY,
DEFAULT_INTERVAL_SECS, DEFAULT_TIMEOUT_MS, MAX_BATCH_SIZE,
};
pub use debug_consumer::DebugConsumer;
pub use log_consumer::{LogConsumer, LogConsumerConfig, RotateMode};
use crate::data::Data;
use crate::error::Result;
#[cfg(feature = "blocking")]
use std::sync::Arc;
use std::sync::OnceLock;
#[cfg(feature = "blocking")]
use tokio::runtime::Runtime;
#[cfg(feature = "blocking")]
pub trait Consumer: Send + Sync {
fn add(&self, data: &Data) -> Result<()>;
fn flush(&self) -> Result<()>;
fn close(&self) -> Result<()>;
fn is_stringent(&self) -> bool {
false
}
}
#[cfg(feature = "async")]
#[async_trait::async_trait]
pub trait AsyncConsumer: Send + Sync {
async fn add_async(&self, data: &Data) -> Result<()>;
async fn flush_async(&self) -> Result<()>;
async fn close_async(&self) -> Result<()>;
fn is_stringent(&self) -> bool {
false
}
}
#[cfg(feature = "blocking")]
impl<C: Consumer + ?Sized> Consumer for Arc<C> {
fn add(&self, data: &Data) -> Result<()> {
(**self).add(data)
}
fn flush(&self) -> Result<()> {
(**self).flush()
}
fn close(&self) -> Result<()> {
(**self).close()
}
fn is_stringent(&self) -> bool {
(**self).is_stringent()
}
}
#[cfg(feature = "blocking")]
impl<C: Consumer + ?Sized> Consumer for Box<C> {
fn add(&self, data: &Data) -> Result<()> {
(**self).add(data)
}
fn flush(&self) -> Result<()> {
(**self).flush()
}
fn close(&self) -> Result<()> {
(**self).close()
}
fn is_stringent(&self) -> bool {
(**self).is_stringent()
}
}
#[cfg(feature = "async")]
#[async_trait::async_trait]
impl<C: AsyncConsumer + ?Sized + Sync> AsyncConsumer for Arc<C> {
async fn add_async(&self, data: &Data) -> Result<()> {
(**self).add_async(data).await
}
async fn flush_async(&self) -> Result<()> {
(**self).flush_async().await
}
async fn close_async(&self) -> Result<()> {
(**self).close_async().await
}
fn is_stringent(&self) -> bool {
(**self).is_stringent()
}
}
#[cfg(feature = "async")]
#[async_trait::async_trait]
impl<C: AsyncConsumer + ?Sized + Sync> AsyncConsumer for Box<C> {
async fn add_async(&self, data: &Data) -> Result<()> {
(**self).add_async(data).await
}
async fn flush_async(&self) -> Result<()> {
(**self).flush_async().await
}
async fn close_async(&self) -> Result<()> {
(**self).close_async().await
}
fn is_stringent(&self) -> bool {
(**self).is_stringent()
}
}
pub(crate) fn shared_runtime() -> &'static Runtime {
static RUNTIME: OnceLock<Runtime> = OnceLock::new();
RUNTIME.get_or_init(|| {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.thread_name("thinkingdata-runtime")
.enable_all()
.build()
.expect("failed to start the ThinkingData async runtime")
})
}