1
+ use std:: num:: NonZeroUsize ;
2
+
1
3
use once_cell:: sync:: Lazy ;
2
4
5
+ pub const DEFAULT_PRIMARY_WORKER_POOL_SIZE : usize = 2 ;
6
+ pub const DEFAULT_USER_WORKER_POOL_SIZE : usize = 1 ;
7
+
3
8
pub static SUPERVISOR_RT : Lazy < tokio:: runtime:: Runtime > = Lazy :: new ( || {
4
9
tokio:: runtime:: Builder :: new_multi_thread ( )
5
10
. enable_all ( )
@@ -11,17 +16,43 @@ pub static SUPERVISOR_RT: Lazy<tokio::runtime::Runtime> = Lazy::new(|| {
11
16
// NOTE: This pool is for the main and event workers. The reason why they should
12
17
// separate from the user worker pool is they can starve them if user workers
13
18
// are saturated.
14
- pub static PRIMARY_WORKER_RT : Lazy < tokio_util:: task:: LocalPoolHandle > =
15
- Lazy :: new ( || tokio_util:: task:: LocalPoolHandle :: new ( 2 ) ) ;
19
+ pub static PRIMARY_WORKER_RT : Lazy < tokio_util:: task:: LocalPoolHandle > = Lazy :: new ( || {
20
+ let maybe_pool_size = std:: env:: var ( "EDGE_RUNTIME_PRIMARY_WORKER_POOL_SIZE" )
21
+ . ok ( )
22
+ . and_then ( |it| it. parse :: < usize > ( ) . ok ( ) )
23
+ . map ( |it| {
24
+ if it < DEFAULT_PRIMARY_WORKER_POOL_SIZE {
25
+ DEFAULT_PRIMARY_WORKER_POOL_SIZE
26
+ } else {
27
+ it
28
+ }
29
+ } ) ;
30
+
31
+ tokio_util:: task:: LocalPoolHandle :: new (
32
+ maybe_pool_size. unwrap_or ( DEFAULT_PRIMARY_WORKER_POOL_SIZE ) ,
33
+ )
34
+ } ) ;
16
35
17
36
pub static USER_WORKER_RT : Lazy < tokio_util:: task:: LocalPoolHandle > = Lazy :: new ( || {
18
37
let maybe_pool_size = std:: env:: var ( "EDGE_RUNTIME_WORKER_POOL_SIZE" )
19
38
. ok ( )
20
- . and_then ( |it| it. parse :: < usize > ( ) . ok ( ) ) ;
39
+ . and_then ( |it| it. parse :: < usize > ( ) . ok ( ) )
40
+ . map ( |it| {
41
+ if it < DEFAULT_USER_WORKER_POOL_SIZE {
42
+ DEFAULT_USER_WORKER_POOL_SIZE
43
+ } else {
44
+ it
45
+ }
46
+ } ) ;
21
47
22
48
tokio_util:: task:: LocalPoolHandle :: new ( if cfg ! ( debug_assertions) {
23
- maybe_pool_size. unwrap_or ( 1 )
49
+ maybe_pool_size. unwrap_or ( DEFAULT_USER_WORKER_POOL_SIZE )
24
50
} else {
25
- maybe_pool_size. unwrap_or ( std:: thread:: available_parallelism ( ) . unwrap ( ) . get ( ) )
51
+ maybe_pool_size. unwrap_or (
52
+ std:: thread:: available_parallelism ( )
53
+ . ok ( )
54
+ . map ( NonZeroUsize :: get)
55
+ . unwrap_or ( DEFAULT_USER_WORKER_POOL_SIZE ) ,
56
+ )
26
57
} )
27
58
} ) ;
0 commit comments