use std::time::Duration;
use crate::error::{Error, Result};
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum BackendPreference {
#[default]
Auto,
IoUring,
Fallback,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RegisteredBufferConfig {
pub count: u32,
pub size: u32,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RingConfig {
pub queue_depth: u32,
pub fallback_threads: usize,
pub completion_batch: usize,
pub backend: BackendPreference,
pub default_timeout: Option<Duration>,
pub sq_poll_idle_ms: Option<u32>,
pub registered_buffers: Option<RegisteredBufferConfig>,
}
impl Default for RingConfig {
fn default() -> Self {
Self {
queue_depth: 256,
fallback_threads: 4,
completion_batch: 64,
backend: BackendPreference::Auto,
default_timeout: Some(Duration::from_secs(30)),
sq_poll_idle_ms: None,
registered_buffers: None,
}
}
}
impl RingConfig {
#[must_use]
pub fn with_queue_depth(mut self, queue_depth: u32) -> Self {
self.queue_depth = queue_depth;
self
}
#[must_use]
pub fn with_backend(mut self, backend: BackendPreference) -> Self {
self.backend = backend;
self
}
#[must_use]
pub fn with_fallback_threads(mut self, fallback_threads: usize) -> Self {
self.fallback_threads = fallback_threads;
self
}
#[must_use]
pub fn with_completion_batch(mut self, completion_batch: usize) -> Self {
self.completion_batch = completion_batch;
self
}
#[must_use]
pub fn with_default_timeout(mut self, default_timeout: Option<Duration>) -> Self {
self.default_timeout = default_timeout;
self
}
#[must_use]
pub fn with_sq_poll(mut self, idle_ms: u32) -> Self {
self.sq_poll_idle_ms = Some(idle_ms);
self
}
#[must_use]
pub fn with_registered_buffers(mut self, count: u32, size: u32) -> Self {
self.registered_buffers = Some(RegisteredBufferConfig { count, size });
self
}
pub fn validate(&self) -> Result<()> {
if self.queue_depth == 0 {
return Err(Error::invalid_config(
"queue_depth must be greater than zero",
));
}
if self.queue_depth > 4096 {
return Err(Error::invalid_config(
"queue_depth above 4096 is rejected to bound kernel and fallback memory use",
));
}
if self.fallback_threads == 0 {
return Err(Error::invalid_config(
"fallback_threads must be greater than zero",
));
}
if self.completion_batch == 0 {
return Err(Error::invalid_config(
"completion_batch must be greater than zero",
));
}
if let Some(ref rb) = self.registered_buffers {
if rb.count == 0 {
return Err(Error::invalid_config(
"registered buffer count must be greater than zero",
));
}
if rb.size == 0 {
return Err(Error::invalid_config(
"registered buffer size must be greater than zero",
));
}
}
Ok(())
}
}