@@ -6,10 +6,11 @@ use std::sync::Arc;
6
6
use crate :: pool:: { AsyncPool , Thread } ;
7
7
use crate :: pool:: { CustomSpawn , DefaultSpawn , ThreadSpawn } ;
8
8
9
- /// A builder for constructing an [`AsyncPool`] with custom configurations.
9
+ /// [`AsyncPoolBuilder`] provides a flexible way to configure and build an [`AsyncPool`] for executing
10
+ /// asynchronous tasks concurrently on dedicated threads.
10
11
///
11
- /// Use this builder to fine-tune the performance and threading behavior of your asynchronous
12
- /// task pool .
12
+ /// This builder enables you to customize the number of threads, concurrency limits, thread naming,
13
+ /// and panic handling strategies .
13
14
pub struct AsyncPoolBuilder < S = DefaultSpawn > {
14
15
pub ( crate ) runtime : tokio:: runtime:: Handle ,
15
16
pub ( crate ) thread_name : Option < Box < dyn FnMut ( usize ) -> String > > ,
@@ -23,7 +24,9 @@ pub struct AsyncPoolBuilder<S = DefaultSpawn> {
23
24
}
24
25
25
26
impl AsyncPoolBuilder < DefaultSpawn > {
26
- /// Creates a new [`AsyncPoolBuilder`] with default settings.
27
+ /// Initializes a new [`AsyncPoolBuilder`] with default settings.
28
+ ///
29
+ /// The builder is tied to the provided [`tokio::runtime::Handle`] and prepares to configure an [`AsyncPool`].
27
30
pub fn new ( runtime : tokio:: runtime:: Handle ) -> AsyncPoolBuilder < DefaultSpawn > {
28
31
AsyncPoolBuilder {
29
32
runtime,
@@ -41,8 +44,10 @@ impl<S> AsyncPoolBuilder<S>
41
44
where
42
45
S : ThreadSpawn ,
43
46
{
44
- /// Specifies a custom hook to generate the thread name given the thread index within the
45
- /// [`AsyncPool`].
47
+ /// Specifies a custom naming convention for threads in the [`AsyncPool`].
48
+ ///
49
+ /// The provided closure receives the thread's index and returns a name,
50
+ /// which can be useful for debugging and logging.
46
51
pub fn thread_name < F > ( mut self , thread_name : F ) -> Self
47
52
where
48
53
F : FnMut ( usize ) -> String + ' static ,
@@ -51,11 +56,14 @@ where
51
56
self
52
57
}
53
58
54
- /// Specifies a custom hook to handle the panic happening in one of the threads of the
55
- /// [`AsyncPool`].
59
+ /// Sets a custom panic handler for threads in the [`AsyncPool`].
60
+ ///
61
+ /// If a thread panics, the provided handler will be invoked so that you can perform
62
+ /// custom error handling or cleanup.
56
63
///
57
- /// The `panic_handler` is called once for each panicking thread with the error caught in the
58
- /// panicking as argument.
64
+ /// # Panics
65
+ ///
66
+ /// In the absence of this handler, thread panics will propagate.
59
67
pub fn thread_panic_handler < F > ( mut self , panic_handler : F ) -> Self
60
68
where
61
69
F : Fn ( Box < dyn Any + Send > ) + Send + Sync + ' static ,
@@ -64,11 +72,14 @@ where
64
72
self
65
73
}
66
74
67
- /// Specifies a custom hook to handle the panic happening in one of the tasks of the
68
- /// [`AsyncPool`].
75
+ /// Sets a custom panic handler for tasks executed by the [`AsyncPool`].
76
+ ///
77
+ /// This handler is used to manage panics that occur during task execution, allowing for graceful
78
+ /// error handling.
69
79
///
70
- /// The `panic_handler` is called once for each panicking task with the error caught in the
71
- /// panicking as argument.
80
+ /// # Panics
81
+ ///
82
+ /// Without a handler, panics in tasks will propagate.
72
83
pub fn task_panic_handler < F > ( mut self , panic_handler : F ) -> Self
73
84
where
74
85
F : Fn ( Box < dyn Any + Send > ) + Send + Sync + ' static ,
77
88
self
78
89
}
79
90
80
- /// Specifies a custom hook to dynamically adjust thread settings for the [`AsyncPool`].
91
+ /// Configures a custom thread spawning procedure for the [`AsyncPool`].
92
+ ///
93
+ /// This method allows you to adjust thread settings (e.g. naming, stack size) before thread creation,
94
+ /// making it possible to apply application-specific configurations.
81
95
pub fn spawn_handler < F > ( self , spawn_handler : F ) -> AsyncPoolBuilder < CustomSpawn < F > >
82
96
where
83
97
F : FnMut ( Thread ) -> io:: Result < ( ) > ,
@@ -93,21 +107,26 @@ where
93
107
}
94
108
}
95
109
96
- /// Sets the number of executor threads for running tasks in the [`AsyncPool`].
110
+ /// Sets the number of worker threads for the [`AsyncPool`].
111
+ ///
112
+ /// This determines how many dedicated threads will be available for running tasks concurrently.
97
113
pub fn num_threads ( mut self , num_threads : usize ) -> Self {
98
114
self . num_threads = num_threads;
99
115
self
100
116
}
101
117
102
- /// Adjusts the maximum number of concurrent tasks allowed per executor in the [`AsyncPool`].
118
+ /// Sets the maximum number of concurrent tasks per thread in the [`AsyncPool`].
103
119
///
104
- /// The max concurrency determines how many futures can be polled simultaneously.
120
+ /// This controls how many futures can be polled simultaneously on each worker thread .
105
121
pub fn max_concurrency ( mut self , max_concurrency : usize ) -> Self {
106
122
self . max_concurrency = max_concurrency;
107
123
self
108
124
}
109
125
110
- /// Finalizes the configuration and constructs an operational [`AsyncPool`] for executing tasks.
126
+ /// Constructs an [`AsyncPool`] based on the configured settings.
127
+ ///
128
+ /// Finalizing the builder sets up dedicated worker threads and configures the executor
129
+ /// to enforce the specified concurrency limits.
111
130
pub fn build < F > ( self ) -> Result < AsyncPool < F > , io:: Error >
112
131
where
113
132
F : Future < Output = ( ) > + Send + ' static ,
0 commit comments