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")]
pub const DEFAULT_POOL_CAPA: usize = 50;
#[cfg(feature = "async")]
pub const DEFAULT_MESSAGE_CAPA: usize = 200;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum WriteMode {
Direct,
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 {
bufsize: usize,
pool_capa: usize,
message_capa: usize,
flush_interval: Duration,
},
}
pub(crate) enum EffectiveWriteMode {
Direct,
BufferAndFlushWith(usize, Duration),
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
#[cfg(feature = "async")]
AsyncWith {
bufsize: usize,
pool_capa: usize,
message_capa: usize,
flush_interval: Duration,
},
BufferDontFlushWith(usize),
}
impl WriteMode {
pub(crate) fn inner(&self) -> EffectiveWriteMode {
match *self {
Self::Direct => EffectiveWriteMode::Direct,
Self::BufferDontFlush => {
EffectiveWriteMode::BufferDontFlushWith(DEFAULT_BUFFER_CAPACITY)
}
Self::BufferDontFlushWith(duration) => {
EffectiveWriteMode::BufferDontFlushWith(duration)
}
Self::BufferAndFlush => EffectiveWriteMode::BufferAndFlushWith(
DEFAULT_BUFFER_CAPACITY,
DEFAULT_FLUSH_INTERVAL,
),
Self::BufferAndFlushWith(bufsize, duration) => {
EffectiveWriteMode::BufferAndFlushWith(bufsize, duration)
}
#[cfg(feature = "async")]
Self::Async => EffectiveWriteMode::AsyncWith {
bufsize: DEFAULT_BUFFER_CAPACITY,
pool_capa: DEFAULT_POOL_CAPA,
message_capa: DEFAULT_MESSAGE_CAPA,
flush_interval: DEFAULT_FLUSH_INTERVAL,
},
#[cfg(feature = "async")]
Self::AsyncWith {
bufsize,
pool_capa,
message_capa,
flush_interval,
} => EffectiveWriteMode::AsyncWith {
bufsize,
pool_capa,
message_capa,
flush_interval,
},
}
}
pub(crate) fn without_flushing(&self) -> WriteMode {
match self {
Self::Direct | Self::BufferDontFlush | Self::BufferDontFlushWith(_) => *self,
Self::BufferAndFlush => Self::BufferDontFlush,
Self::BufferAndFlushWith(bufsize, _) => Self::BufferDontFlushWith(*bufsize),
#[cfg(feature = "async")]
Self::Async => Self::AsyncWith {
bufsize: DEFAULT_BUFFER_CAPACITY,
pool_capa: DEFAULT_POOL_CAPA,
message_capa: DEFAULT_MESSAGE_CAPA,
flush_interval: Duration::from_secs(0),
},
#[cfg(feature = "async")]
Self::AsyncWith {
bufsize,
pool_capa,
message_capa,
flush_interval: _,
} => Self::AsyncWith {
bufsize: *bufsize,
pool_capa: *pool_capa,
message_capa: *message_capa,
flush_interval: Duration::from_secs(0),
},
}
}
pub(crate) fn buffersize(&self) -> Option<usize> {
#[allow(clippy::match_same_arms)]
match self.inner() {
EffectiveWriteMode::Direct => None,
EffectiveWriteMode::BufferAndFlushWith(bufsize, _)
| EffectiveWriteMode::BufferDontFlushWith(bufsize) => Some(bufsize),
#[cfg(feature = "async")]
EffectiveWriteMode::AsyncWith {
bufsize,
pool_capa: _,
message_capa: _,
flush_interval: _,
} => Some(bufsize),
}
}
pub(crate) fn get_flush_interval(&self) -> Duration {
#[allow(clippy::match_same_arms)]
match self {
Self::Direct | Self::BufferDontFlush | Self::BufferDontFlushWith(_) => {
Duration::from_secs(0)
}
Self::BufferAndFlush => DEFAULT_FLUSH_INTERVAL,
#[cfg(feature = "async")]
Self::Async => DEFAULT_FLUSH_INTERVAL,
Self::BufferAndFlushWith(_, flush_interval) => *flush_interval,
#[cfg(feature = "async")]
Self::AsyncWith {
bufsize: _,
pool_capa: _,
message_capa: _,
flush_interval,
} => *flush_interval,
}
}
}