@@ -78,7 +78,7 @@ use sb_event_worker::events::{EventMetadata, WorkerEventWithMetadata};
78
78
use sb_event_worker:: js_interceptors:: sb_events_js_interceptors;
79
79
use sb_event_worker:: sb_user_event_worker;
80
80
use sb_node:: deno_node;
81
- use sb_workers:: context:: { UserWorkerMsgs , WorkerContextInitOpts , WorkerRuntimeOpts } ;
81
+ use sb_workers:: context:: { UserWorkerMsgs , WorkerContextInitOpts , WorkerKind , WorkerRuntimeOpts } ;
82
82
use sb_workers:: sb_user_workers;
83
83
84
84
const DEFAULT_ALLOC_CHECK_INT_MSEC : u64 = 1000 ;
@@ -107,6 +107,11 @@ pub static SHOULD_USE_VERBOSE_DEPRECATED_API_WARNING: OnceCell<bool> = OnceCell:
107
107
pub static SHOULD_INCLUDE_MALLOCED_MEMORY_ON_MEMCHECK : OnceCell < bool > = OnceCell :: new ( ) ;
108
108
pub static MAYBE_DENO_VERSION : OnceCell < String > = OnceCell :: new ( ) ;
109
109
110
+ pub static MAIN_WORKER_INITIAL_HEAP_SIZE_MIB : OnceCell < u64 > = OnceCell :: new ( ) ;
111
+ pub static MAIN_WORKER_MAX_HEAP_SIZE_MIB : OnceCell < u64 > = OnceCell :: new ( ) ;
112
+ pub static EVENT_WORKER_INITIAL_HEAP_SIZE_MIB : OnceCell < u64 > = OnceCell :: new ( ) ;
113
+ pub static EVENT_WORKER_MAX_HEAP_SIZE_MIB : OnceCell < u64 > = OnceCell :: new ( ) ;
114
+
110
115
thread_local ! {
111
116
// NOTE: Suppose we have met `.await` points while initializing a
112
117
// DenoRuntime. In that case, the current v8 isolate's thread-local state can be
@@ -761,37 +766,62 @@ where
761
766
let beforeunload_cpu_threshold = ArcSwapOption :: < u64 > :: from_pointee ( None ) ;
762
767
let beforeunload_mem_threshold = ArcSwapOption :: < u64 > :: from_pointee ( None ) ;
763
768
764
- if conf. is_user_worker ( ) {
765
- let conf = maybe_user_conf. unwrap ( ) ;
766
- let memory_limit_bytes = mib_to_bytes ( conf. memory_limit_mb ) as usize ;
767
-
768
- beforeunload_mem_threshold. store (
769
- flags
770
- . beforeunload_memory_pct
771
- . and_then ( |it| percentage_value ( memory_limit_bytes as u64 , it) )
772
- . map ( Arc :: new) ,
773
- ) ;
769
+ match conf. to_worker_kind ( ) {
770
+ WorkerKind :: UserWorker => {
771
+ let conf = maybe_user_conf. unwrap ( ) ;
772
+ let memory_limit_bytes = mib_to_bytes ( conf. memory_limit_mb ) as usize ;
774
773
775
- if conf. cpu_time_hard_limit_ms > 0 {
776
- beforeunload_cpu_threshold. store (
774
+ beforeunload_mem_threshold. store (
777
775
flags
778
- . beforeunload_cpu_pct
779
- . and_then ( |it| percentage_value ( conf . cpu_time_hard_limit_ms , it) )
776
+ . beforeunload_memory_pct
777
+ . and_then ( |it| percentage_value ( memory_limit_bytes as u64 , it) )
780
778
. map ( Arc :: new) ,
781
779
) ;
782
- }
783
780
784
- let allocator = CustomAllocator :: new ( memory_limit_bytes) ;
781
+ if conf. cpu_time_hard_limit_ms > 0 {
782
+ beforeunload_cpu_threshold. store (
783
+ flags
784
+ . beforeunload_cpu_pct
785
+ . and_then ( |it| percentage_value ( conf. cpu_time_hard_limit_ms , it) )
786
+ . map ( Arc :: new) ,
787
+ ) ;
788
+ }
785
789
786
- allocator. set_waker ( mem_check . waker . clone ( ) ) ;
790
+ let allocator = CustomAllocator :: new ( memory_limit_bytes ) ;
787
791
788
- mem_check. limit = Some ( memory_limit_bytes) ;
789
- create_params = Some (
790
- v8:: CreateParams :: default ( )
791
- . heap_limits ( mib_to_bytes ( 0 ) as usize , memory_limit_bytes)
792
- . array_buffer_allocator ( allocator. into_v8_allocator ( ) ) ,
793
- )
794
- } ;
792
+ allocator. set_waker ( mem_check. waker . clone ( ) ) ;
793
+
794
+ mem_check. limit = Some ( memory_limit_bytes) ;
795
+ create_params = Some (
796
+ v8:: CreateParams :: default ( )
797
+ . heap_limits ( mib_to_bytes ( 0 ) as usize , memory_limit_bytes)
798
+ . array_buffer_allocator ( allocator. into_v8_allocator ( ) ) ,
799
+ )
800
+ }
801
+ kind => {
802
+ assert_ne ! ( kind, WorkerKind :: UserWorker ) ;
803
+ let initial_heap_size = match kind {
804
+ WorkerKind :: MainWorker => & MAIN_WORKER_INITIAL_HEAP_SIZE_MIB ,
805
+ WorkerKind :: EventsWorker => & EVENT_WORKER_INITIAL_HEAP_SIZE_MIB ,
806
+ _ => unreachable ! ( ) ,
807
+ } ;
808
+ let max_heap_size = match kind {
809
+ WorkerKind :: MainWorker => & MAIN_WORKER_MAX_HEAP_SIZE_MIB ,
810
+ WorkerKind :: EventsWorker => & EVENT_WORKER_MAX_HEAP_SIZE_MIB ,
811
+ _ => unreachable ! ( ) ,
812
+ } ;
813
+
814
+ let initial_heap_size = initial_heap_size. get ( ) . cloned ( ) . unwrap_or_default ( ) ;
815
+ let max_heap_size = max_heap_size. get ( ) . cloned ( ) . unwrap_or_default ( ) ;
816
+
817
+ if max_heap_size > 0 {
818
+ create_params = Some ( v8:: CreateParams :: default ( ) . heap_limits (
819
+ mib_to_bytes ( initial_heap_size) as usize ,
820
+ mib_to_bytes ( max_heap_size) as usize ,
821
+ ) ) ;
822
+ }
823
+ }
824
+ }
795
825
796
826
let mem_check = Arc :: new ( mem_check) ;
797
827
let runtime_options = RuntimeOptions {
0 commit comments