@@ -136,7 +136,7 @@ lazy_static! {
136
136
137
137
/// Sliding window for pool task frequency calculation
138
138
static ref FREQ_QUEUE : Mutex <VecDeque <u64 >> = {
139
- Mutex :: new( VecDeque :: with_capacity( FREQUENCY_QUEUE_SIZE + 1 ) )
139
+ Mutex :: new( VecDeque :: with_capacity( FREQUENCY_QUEUE_SIZE . saturating_add ( 1 ) ) )
140
140
} ;
141
141
142
142
/// Dynamic pool thread count variable
@@ -196,7 +196,7 @@ fn scale_pool() {
196
196
197
197
// Add seen frequency data to the frequency histogram.
198
198
freq_queue. push_back ( frequency) ;
199
- if freq_queue. len ( ) == FREQUENCY_QUEUE_SIZE + 1 {
199
+ if freq_queue. len ( ) == FREQUENCY_QUEUE_SIZE . saturating_add ( 1 ) {
200
200
freq_queue. pop_front ( ) ;
201
201
}
202
202
@@ -255,12 +255,14 @@ fn create_blocking_thread() {
255
255
// background noise.
256
256
//
257
257
// Generate a simple random number of milliseconds
258
- let rand_sleep_ms = u64:: from ( random ( 10_000 ) ) ;
258
+ let rand_sleep_ms = 1000_u64
259
+ . checked_add ( u64:: from ( random ( 10_000 ) ) )
260
+ . expect ( "shouldn't overflow" ) ;
259
261
260
262
let _ = thread:: Builder :: new ( )
261
263
. name ( "async-blocking-driver-dynamic" . to_string ( ) )
262
264
. spawn ( move || {
263
- let wait_limit = Duration :: from_millis ( 1000 + rand_sleep_ms) ;
265
+ let wait_limit = Duration :: from_millis ( rand_sleep_ms) ;
264
266
265
267
// Adjust the pool size counter before and after spawn
266
268
* POOL_SIZE . lock ( ) . unwrap ( ) += 1 ;
@@ -276,7 +278,12 @@ fn create_blocking_thread() {
276
278
// Also, some systems have it(like macOS), and some don't(Linux).
277
279
// This case expected not to happen.
278
280
// But when happened this shouldn't throw a panic.
279
- MAX_THREADS . store ( * POOL_SIZE . lock ( ) . unwrap ( ) - 1 , Ordering :: SeqCst ) ;
281
+ let guarded_count = POOL_SIZE
282
+ . lock ( )
283
+ . unwrap ( )
284
+ . checked_sub ( 1 )
285
+ . expect ( "shouldn't underflow" ) ;
286
+ MAX_THREADS . store ( guarded_count, Ordering :: SeqCst ) ;
280
287
}
281
288
_ => eprintln ! (
282
289
"cannot start a dynamic thread driving blocking tasks: {}" ,
0 commit comments