qubit_thread_pool/thread_pool/thread_pool_build_error.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9use std::io;
10
11use thiserror::Error;
12
13use qubit_executor::service::RejectedExecution;
14
15/// Error returned when a [`super::thread_pool::ThreadPool`] cannot be built.
16///
17/// # Author
18///
19/// Haixing Hu
20#[derive(Debug, Error)]
21pub enum ThreadPoolBuildError {
22 /// The configured maximum pool size is zero.
23 #[error("thread pool maximum pool size must be greater than zero")]
24 ZeroMaximumPoolSize,
25
26 /// The configured core pool size is greater than the maximum pool size.
27 #[error(
28 "thread pool core pool size {core_pool_size} exceeds maximum pool size {maximum_pool_size}"
29 )]
30 CorePoolSizeExceedsMaximum {
31 /// Configured core pool size.
32 core_pool_size: usize,
33
34 /// Configured maximum pool size.
35 maximum_pool_size: usize,
36 },
37
38 /// The configured bounded queue capacity is zero.
39 #[error("thread pool queue capacity must be greater than zero")]
40 ZeroQueueCapacity,
41
42 /// The configured worker stack size is zero.
43 #[error("thread pool stack size must be greater than zero")]
44 ZeroStackSize,
45
46 /// The configured keep-alive timeout is zero.
47 #[error("thread pool keep-alive timeout must be greater than zero")]
48 ZeroKeepAlive,
49
50 /// A worker thread could not be spawned.
51 #[error("failed to spawn thread pool worker {index}: {source}")]
52 SpawnWorker {
53 /// Index of the worker that failed to spawn.
54 index: usize,
55
56 /// I/O error reported by [`std::thread::Builder::spawn`].
57 source: io::Error,
58 },
59}
60
61impl ThreadPoolBuildError {
62 /// Converts a runtime worker-spawn rejection into a build error.
63 ///
64 /// # Parameters
65 ///
66 /// * `error` - Rejection produced while prestarting workers during build.
67 ///
68 /// # Returns
69 ///
70 /// A build error carrying equivalent failure context.
71 pub(crate) fn from_rejected_execution(error: RejectedExecution) -> Self {
72 match error {
73 RejectedExecution::WorkerSpawnFailed { source } => Self::SpawnWorker {
74 index: 0,
75 source: io::Error::new(source.kind(), source.to_string()),
76 },
77 RejectedExecution::Shutdown => Self::SpawnWorker {
78 index: 0,
79 source: io::Error::other("thread pool shut down during prestart"),
80 },
81 RejectedExecution::Saturated => Self::SpawnWorker {
82 index: 0,
83 source: io::Error::other("thread pool saturated during prestart"),
84 },
85 }
86 }
87}
88
89impl From<RejectedExecution> for ThreadPoolBuildError {
90 /// Converts rejected-execution reasons into build-time thread-pool errors.
91 ///
92 /// # Parameters
93 ///
94 /// * `error` - Rejection reason observed during thread prestart.
95 ///
96 /// # Returns
97 ///
98 /// A build error that preserves equivalent failure context.
99 fn from(error: RejectedExecution) -> Self {
100 Self::from_rejected_execution(error)
101 }
102}