use crate::ZERO_DURATION;
use std::time::Duration;
pub const DEFAULT_BUFFER_CAPACITY: usize = 8 * 1024;
pub const DEFAULT_FLUSH_INTERVAL: Duration = Duration::from_secs(1);
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
pub const DEFAULT_POOL_CAPA: usize = 50;
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
pub const DEFAULT_MESSAGE_CAPA: usize = 200;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum WriteMode {
Direct,
SupportCapture,
BufferAndFlush,
BufferAndFlushWith(
usize,
Duration,
),
BufferDontFlush,
BufferDontFlushWith(
usize,
),
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
#[cfg(feature = "async")]
Async,
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
#[cfg(feature = "async")]
AsyncWith {
pool_capa: usize,
message_capa: usize,
flush_interval: Duration,
},
}
impl WriteMode {
pub(crate) fn effective_write_mode(&self) -> EffectiveWriteMode {
match *self {
Self::Direct | Self::SupportCapture => EffectiveWriteMode::Direct,
Self::BufferDontFlush => {
EffectiveWriteMode::BufferDontFlushWith(DEFAULT_BUFFER_CAPACITY)
}
Self::BufferDontFlushWith(duration) => {
EffectiveWriteMode::BufferDontFlushWith(duration)
}
Self::BufferAndFlush => EffectiveWriteMode::BufferAndFlushWith(DEFAULT_BUFFER_CAPACITY),
Self::BufferAndFlushWith(bufsize, _duration) => {
EffectiveWriteMode::BufferAndFlushWith(bufsize)
}
#[cfg(feature = "async")]
Self::Async => EffectiveWriteMode::AsyncWith {
pool_capa: DEFAULT_POOL_CAPA,
message_capa: DEFAULT_MESSAGE_CAPA,
flush_interval: DEFAULT_FLUSH_INTERVAL,
},
#[cfg(feature = "async")]
Self::AsyncWith {
pool_capa,
message_capa,
flush_interval,
} => EffectiveWriteMode::AsyncWith {
pool_capa,
message_capa,
flush_interval,
},
}
}
pub(crate) fn without_flushing(&self) -> WriteMode {
match self {
Self::Direct
| Self::SupportCapture
| Self::BufferDontFlush
| Self::BufferDontFlushWith(_) => *self,
Self::BufferAndFlush => Self::BufferDontFlush,
Self::BufferAndFlushWith(bufsize, _) => Self::BufferDontFlushWith(*bufsize),
#[cfg(feature = "async")]
Self::Async => Self::AsyncWith {
pool_capa: DEFAULT_POOL_CAPA,
message_capa: DEFAULT_MESSAGE_CAPA,
flush_interval: ZERO_DURATION,
},
#[cfg(feature = "async")]
Self::AsyncWith {
pool_capa,
message_capa,
flush_interval: _,
} => Self::AsyncWith {
pool_capa: *pool_capa,
message_capa: *message_capa,
flush_interval: ZERO_DURATION,
},
}
}
pub(crate) fn buffersize(&self) -> Option<usize> {
match self.effective_write_mode() {
EffectiveWriteMode::Direct => None,
EffectiveWriteMode::BufferAndFlushWith(bufsize)
| EffectiveWriteMode::BufferDontFlushWith(bufsize) => Some(bufsize),
#[cfg(feature = "async")]
EffectiveWriteMode::AsyncWith {
pool_capa: _,
message_capa: _,
flush_interval: _,
} => None,
}
}
pub(crate) fn get_flush_interval(&self) -> Duration {
match self {
Self::Direct
| Self::SupportCapture
| Self::BufferDontFlush
| Self::BufferDontFlushWith(_) => ZERO_DURATION,
Self::BufferAndFlush => DEFAULT_FLUSH_INTERVAL,
#[cfg(feature = "async")]
Self::Async => DEFAULT_FLUSH_INTERVAL,
Self::BufferAndFlushWith(_, flush_interval) => *flush_interval,
#[cfg(feature = "async")]
Self::AsyncWith {
pool_capa: _,
message_capa: _,
flush_interval,
} => *flush_interval,
}
}
}
pub(crate) enum EffectiveWriteMode {
Direct,
BufferAndFlushWith(usize),
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
#[cfg(feature = "async")]
AsyncWith {
pool_capa: usize,
message_capa: usize,
flush_interval: Duration,
},
BufferDontFlushWith(usize),
}