@@ -12,7 +12,7 @@ use event_worker::events::{
12
12
use hyper:: { Body , Request , Response } ;
13
13
use log:: { debug, error} ;
14
14
use sb_core:: conn_sync:: ConnSync ;
15
- use sb_core:: WorkerMetricSource ;
15
+ use sb_core:: { MetricSource , SharedMetricSource } ;
16
16
use sb_graph:: EszipPayloadKind ;
17
17
use sb_workers:: context:: {
18
18
EventWorkerRuntimeOpts , MainWorkerRuntimeOpts , Timing , UserWorkerMsgs , WorkerContextInitOpts ,
@@ -310,10 +310,10 @@ impl CreateWorkerArgs {
310
310
311
311
pub async fn create_worker < Opt : Into < CreateWorkerArgs > > (
312
312
init_opts : Opt ,
313
- ) -> Result < ( WorkerMetricSource , mpsc:: UnboundedSender < WorkerRequestMsg > ) , Error > {
313
+ ) -> Result < ( MetricSource , mpsc:: UnboundedSender < WorkerRequestMsg > ) , Error > {
314
314
let ( unix_stream_tx, unix_stream_rx) = mpsc:: unbounded_channel :: < UnixStreamEntry > ( ) ;
315
315
let ( worker_boot_result_tx, worker_boot_result_rx) =
316
- oneshot:: channel :: < Result < WorkerMetricSource , Error > > ( ) ;
316
+ oneshot:: channel :: < Result < MetricSource , Error > > ( ) ;
317
317
318
318
let CreateWorkerArgs ( init_opts, maybe_supervisor_policy, maybe_termination_token) =
319
319
init_opts. into ( ) ;
@@ -464,13 +464,7 @@ pub async fn create_events_worker(
464
464
no_module_cache : bool ,
465
465
maybe_entrypoint : Option < String > ,
466
466
termination_token : Option < TerminationToken > ,
467
- ) -> Result <
468
- (
469
- WorkerMetricSource ,
470
- mpsc:: UnboundedSender < WorkerEventWithMetadata > ,
471
- ) ,
472
- Error ,
473
- > {
467
+ ) -> Result < ( MetricSource , mpsc:: UnboundedSender < WorkerEventWithMetadata > ) , Error > {
474
468
let ( events_tx, events_rx) = mpsc:: unbounded_channel :: < WorkerEventWithMetadata > ( ) ;
475
469
476
470
let mut service_path = events_worker_path. clone ( ) ;
@@ -509,71 +503,79 @@ pub async fn create_user_worker_pool(
509
503
policy : WorkerPoolPolicy ,
510
504
worker_event_sender : Option < mpsc:: UnboundedSender < WorkerEventWithMetadata > > ,
511
505
termination_token : Option < TerminationToken > ,
512
- ) -> Result < mpsc:: UnboundedSender < UserWorkerMsgs > , Error > {
506
+ ) -> Result < ( SharedMetricSource , mpsc:: UnboundedSender < UserWorkerMsgs > ) , Error > {
507
+ let metric_src = SharedMetricSource :: default ( ) ;
513
508
let ( user_worker_msgs_tx, mut user_worker_msgs_rx) =
514
509
mpsc:: unbounded_channel :: < UserWorkerMsgs > ( ) ;
515
510
516
511
let user_worker_msgs_tx_clone = user_worker_msgs_tx. clone ( ) ;
517
512
518
- let _handle: tokio:: task:: JoinHandle < Result < ( ) , Error > > = tokio:: spawn ( async move {
519
- let token = termination_token. as_ref ( ) ;
520
- let mut termination_requested = false ;
521
- let mut worker_pool =
522
- WorkerPool :: new ( policy, worker_event_sender, user_worker_msgs_tx_clone) ;
523
-
524
- // Note: Keep this loop non-blocking. Spawn a task to run blocking calls.
525
- // Handle errors within tasks and log them - do not bubble up errors.
526
- loop {
527
- tokio:: select! {
528
- _ = async {
529
- if let Some ( token) = token {
530
- token. inbound. cancelled( ) . await ;
531
- } else {
532
- pending:: <( ) >( ) . await ;
533
- }
534
- } , if !termination_requested => {
535
- termination_requested = true ;
536
-
537
- if worker_pool. user_workers. is_empty( ) {
513
+ let _handle: tokio:: task:: JoinHandle < Result < ( ) , Error > > = tokio:: spawn ( {
514
+ let metric_src_inner = metric_src. clone ( ) ;
515
+ async move {
516
+ let token = termination_token. as_ref ( ) ;
517
+ let mut termination_requested = false ;
518
+ let mut worker_pool = WorkerPool :: new (
519
+ policy,
520
+ metric_src_inner,
521
+ worker_event_sender,
522
+ user_worker_msgs_tx_clone,
523
+ ) ;
524
+
525
+ // Note: Keep this loop non-blocking. Spawn a task to run blocking calls.
526
+ // Handle errors within tasks and log them - do not bubble up errors.
527
+ loop {
528
+ tokio:: select! {
529
+ _ = async {
538
530
if let Some ( token) = token {
539
- token. outbound. cancel( ) ;
531
+ token. inbound. cancelled( ) . await ;
532
+ } else {
533
+ pending:: <( ) >( ) . await ;
540
534
}
535
+ } , if !termination_requested => {
536
+ termination_requested = true ;
541
537
542
- break ;
543
- }
544
- }
538
+ if worker_pool. user_workers. is_empty( ) {
539
+ if let Some ( token) = token {
540
+ token. outbound. cancel( ) ;
541
+ }
545
542
546
- msg = user_worker_msgs_rx. recv( ) => {
547
- match msg {
548
- None => break ,
549
- Some ( UserWorkerMsgs :: Create ( worker_options, tx) ) => {
550
- worker_pool. create_user_worker( worker_options, tx, termination_token. as_ref( ) . map( |it| it. child_token( ) ) ) ;
551
- }
552
- Some ( UserWorkerMsgs :: Created ( key, profile) ) => {
553
- worker_pool. add_user_worker( key, profile) ;
554
- }
555
- Some ( UserWorkerMsgs :: SendRequest ( key, req, res_tx, conn_watch) ) => {
556
- worker_pool. send_request( & key, req, res_tx, conn_watch) ;
557
- }
558
- Some ( UserWorkerMsgs :: Idle ( key) ) => {
559
- worker_pool. idle( & key) ;
543
+ break ;
560
544
}
561
- Some ( UserWorkerMsgs :: Shutdown ( key) ) => {
562
- worker_pool. shutdown( & key) ;
545
+ }
563
546
564
- if let Some ( token) = token {
565
- if token. inbound. is_cancelled( ) && worker_pool. user_workers. is_empty( ) {
566
- token. outbound. cancel( ) ;
547
+ msg = user_worker_msgs_rx. recv( ) => {
548
+ match msg {
549
+ None => break ,
550
+ Some ( UserWorkerMsgs :: Create ( worker_options, tx) ) => {
551
+ worker_pool. create_user_worker( worker_options, tx, termination_token. as_ref( ) . map( |it| it. child_token( ) ) ) ;
552
+ }
553
+ Some ( UserWorkerMsgs :: Created ( key, profile) ) => {
554
+ worker_pool. add_user_worker( key, profile) ;
555
+ }
556
+ Some ( UserWorkerMsgs :: SendRequest ( key, req, res_tx, conn_watch) ) => {
557
+ worker_pool. send_request( & key, req, res_tx, conn_watch) ;
558
+ }
559
+ Some ( UserWorkerMsgs :: Idle ( key) ) => {
560
+ worker_pool. idle( & key) ;
561
+ }
562
+ Some ( UserWorkerMsgs :: Shutdown ( key) ) => {
563
+ worker_pool. shutdown( & key) ;
564
+
565
+ if let Some ( token) = token {
566
+ if token. inbound. is_cancelled( ) && worker_pool. user_workers. is_empty( ) {
567
+ token. outbound. cancel( ) ;
568
+ }
567
569
}
568
570
}
569
571
}
570
572
}
571
573
}
572
574
}
573
- }
574
575
575
- Ok ( ( ) )
576
+ Ok ( ( ) )
577
+ }
576
578
} ) ;
577
579
578
- Ok ( user_worker_msgs_tx)
580
+ Ok ( ( metric_src , user_worker_msgs_tx) )
579
581
}
0 commit comments