use std::io;
use thiserror::Error;
use qubit_executor::service::RejectedExecution;
#[derive(Debug, Error)]
pub enum ThreadPoolBuildError {
#[error("thread pool maximum pool size must be greater than zero")]
ZeroMaximumPoolSize,
#[error(
"thread pool core pool size {core_pool_size} exceeds maximum pool size {maximum_pool_size}"
)]
CorePoolSizeExceedsMaximum {
core_pool_size: usize,
maximum_pool_size: usize,
},
#[error("thread pool queue capacity must be greater than zero")]
ZeroQueueCapacity,
#[error("thread pool stack size must be greater than zero")]
ZeroStackSize,
#[error("thread pool keep-alive timeout must be greater than zero")]
ZeroKeepAlive,
#[error("failed to spawn thread pool worker {index}: {source}")]
SpawnWorker {
index: usize,
source: io::Error,
},
}
impl ThreadPoolBuildError {
pub(crate) fn from_rejected_execution(error: RejectedExecution) -> Self {
match error {
RejectedExecution::WorkerSpawnFailed { source } => Self::SpawnWorker {
index: 0,
source: io::Error::new(source.kind(), source.to_string()),
},
RejectedExecution::Shutdown => Self::SpawnWorker {
index: 0,
source: io::Error::other("thread pool shut down during prestart"),
},
RejectedExecution::Saturated => Self::SpawnWorker {
index: 0,
source: io::Error::other("thread pool saturated during prestart"),
},
}
}
}
impl From<RejectedExecution> for ThreadPoolBuildError {
fn from(error: RejectedExecution) -> Self {
Self::from_rejected_execution(error)
}
}