@@ -134,7 +134,7 @@ impl ActiveWorkerRegistry {
134
134
}
135
135
}
136
136
137
- fn mark_used_and_try_advance ( & mut self ) -> Option < & Uuid > {
137
+ fn mark_used_and_try_advance ( & mut self , policy : SupervisorPolicy ) -> Option < & Uuid > {
138
138
if self . workers . is_empty ( ) {
139
139
let _ = self . next . take ( ) ;
140
140
return None ;
@@ -147,25 +147,40 @@ impl ActiveWorkerRegistry {
147
147
. unwrap_or ( 0 ) ;
148
148
149
149
match self . workers . iter ( ) . nth ( idx) . cloned ( ) {
150
- Some ( WorkerId ( key, true ) ) => {
151
- let key = self
152
- . workers
153
- . replace ( WorkerId ( key, false ) )
154
- . and_then ( |WorkerId ( ref key, _) | self . workers . get ( key) . map ( |it| & it. 0 ) ) ;
155
-
156
- self . next = self . workers . iter ( ) . position ( |it| it. 1 ) ;
157
- key
158
- }
150
+ Some ( WorkerId ( key, true ) ) => match policy {
151
+ SupervisorPolicy :: PerWorker => {
152
+ self . next = Some ( idx + 1 ) ;
153
+ self . workers . get ( & key) . map ( |it| & it. 0 )
154
+ }
155
+
156
+ SupervisorPolicy :: PerRequest { .. } => {
157
+ let key = self
158
+ . workers
159
+ . replace ( WorkerId ( key, false ) )
160
+ . and_then ( |WorkerId ( ref key, _) | self . workers . get ( key) . map ( |it| & it. 0 ) ) ;
161
+
162
+ self . next = self . workers . iter ( ) . position ( |it| it. 1 ) ;
163
+ key
164
+ }
165
+ } ,
166
+
159
167
_ => {
160
168
let _ = self . next . take ( ) ;
161
169
None
162
170
}
163
171
}
164
172
}
165
173
166
- fn mark_idle ( & mut self , key : & Uuid ) {
167
- if let Some ( WorkerId ( key, false ) ) = self . workers . get ( key) . cloned ( ) {
168
- let _ = self . workers . replace ( WorkerId ( key, true ) ) ;
174
+ fn mark_idle ( & mut self , key : & Uuid , policy : SupervisorPolicy ) {
175
+ if let Some ( WorkerId ( key, mark) ) = self . workers . get ( key) . cloned ( ) {
176
+ if policy. is_per_request ( ) {
177
+ if mark != false {
178
+ return ;
179
+ }
180
+
181
+ let _ = self . workers . replace ( WorkerId ( key, true ) ) ;
182
+ }
183
+
169
184
let ( notify_tx, _) = self . notify_pair . clone ( ) ;
170
185
let _ = notify_tx. send ( Some ( key) ) ;
171
186
}
@@ -418,7 +433,10 @@ impl WorkerPool {
418
433
. entry ( profile. service_path . clone ( ) )
419
434
. or_insert_with ( || ActiveWorkerRegistry :: new ( self . policy . max_parallelism ) ) ;
420
435
421
- registry. workers . insert ( WorkerId ( key, false ) ) ;
436
+ registry
437
+ . workers
438
+ . insert ( WorkerId ( key, self . policy . supervisor_policy . is_per_worker ( ) ) ) ;
439
+
422
440
self . user_workers . insert ( key, profile) ;
423
441
}
424
442
@@ -431,21 +449,24 @@ impl WorkerPool {
431
449
) {
432
450
let _: Result < ( ) , Error > = match self . user_workers . get ( key) {
433
451
Some ( worker) => {
452
+ let policy = self . policy . supervisor_policy ;
434
453
let profile = worker. clone ( ) ;
435
454
let cancel = worker. cancel . clone ( ) ;
436
455
let ( req_start_tx, req_end_tx) = profile. timing_tx_pair . clone ( ) ;
437
456
438
457
// Create a closure to handle the request and send the response
439
458
let request_handler = async move {
440
- let fence = Arc :: new ( Notify :: const_new ( ) ) ;
459
+ if !policy. is_per_worker ( ) {
460
+ let fence = Arc :: new ( Notify :: const_new ( ) ) ;
441
461
442
- if let Err ( ex) = req_start_tx. send ( fence. clone ( ) ) {
443
- error ! ( "failed to notify the fence to the supervisor" ) ;
444
- return Err ( ex)
445
- . with_context ( || "failed to notify the fence to the supervisor" ) ;
446
- }
462
+ if let Err ( ex) = req_start_tx. send ( fence. clone ( ) ) {
463
+ error ! ( "failed to notify the fence to the supervisor" ) ;
464
+ return Err ( ex)
465
+ . with_context ( || "failed to notify the fence to the supervisor" ) ;
466
+ }
447
467
448
- fence. notified ( ) . await ;
468
+ fence. notified ( ) . await ;
469
+ }
449
470
450
471
let result = send_user_worker_request (
451
472
profile. worker_request_msg_tx ,
@@ -487,26 +508,13 @@ impl WorkerPool {
487
508
} ;
488
509
}
489
510
490
- pub fn retire ( & mut self , key : & Uuid ) {
491
- if let Some ( profile) = self . user_workers . get ( key) {
492
- let registry = self
493
- . active_workers
494
- . get_mut ( & profile. service_path )
495
- . expect ( "registry must be initialized at this point" ) ;
496
-
497
- if registry. workers . contains ( key) {
498
- registry. workers . remove ( key) ;
499
- }
500
- }
501
- }
502
-
503
511
pub fn idle ( & mut self , key : & Uuid ) {
504
512
if let Some ( registry) = self
505
513
. user_workers
506
514
. get_mut ( key)
507
515
. and_then ( |it| self . active_workers . get_mut ( & it. service_path ) )
508
516
{
509
- registry. mark_idle ( key) ;
517
+ registry. mark_idle ( key, self . policy . supervisor_policy ) ;
510
518
}
511
519
}
512
520
@@ -525,6 +533,19 @@ impl WorkerPool {
525
533
let _ = notify_tx. send ( None ) ;
526
534
}
527
535
536
+ fn retire ( & mut self , key : & Uuid ) {
537
+ if let Some ( profile) = self . user_workers . get ( key) {
538
+ let registry = self
539
+ . active_workers
540
+ . get_mut ( & profile. service_path )
541
+ . expect ( "registry must be initialized at this point" ) ;
542
+
543
+ if registry. workers . contains ( key) {
544
+ registry. workers . remove ( key) ;
545
+ }
546
+ }
547
+ }
548
+
528
549
fn maybe_active_worker ( & mut self , service_path : & String , force_create : bool ) -> Option < Uuid > {
529
550
if force_create {
530
551
return None ;
@@ -534,33 +555,35 @@ impl WorkerPool {
534
555
return None ;
535
556
} ;
536
557
537
- let mut advance_fn = move || registry. mark_used_and_try_advance ( ) . copied ( ) ;
558
+ let policy = self . policy . supervisor_policy ;
559
+ let mut advance_fn = move || registry. mark_used_and_try_advance ( policy) . copied ( ) ;
538
560
539
- if self . policy . supervisor_policy . is_per_request ( ) {
561
+ if policy. is_per_request ( ) {
540
562
return advance_fn ( ) ;
541
563
}
542
564
543
- loop {
544
- let Some ( worker_uuid) = advance_fn ( ) else {
545
- return None ;
546
- } ;
565
+ let Some ( worker_uuid) = advance_fn ( ) else {
566
+ return None ;
567
+ } ;
547
568
548
- match self
549
- . user_workers
550
- . get ( & worker_uuid)
551
- . and_then ( |it| it. status . is_retired . as_ref ( ) )
552
- {
553
- Some ( is_retired) if !is_retired. is_raised ( ) => {
554
- self . user_workers
555
- . get ( & worker_uuid)
556
- . map ( |it| it. status . demand . as_ref ( ) )
557
- . unwrap ( )
558
- . fetch_add ( 1 , Ordering :: Release ) ;
559
-
560
- return Some ( worker_uuid) ;
561
- }
569
+ match self
570
+ . user_workers
571
+ . get ( & worker_uuid)
572
+ . and_then ( |it| it. status . is_retired . as_ref ( ) )
573
+ {
574
+ Some ( is_retired) if !is_retired. is_raised ( ) => {
575
+ self . user_workers
576
+ . get ( & worker_uuid)
577
+ . map ( |it| it. status . demand . as_ref ( ) )
578
+ . unwrap ( )
579
+ . fetch_add ( 1 , Ordering :: Release ) ;
580
+
581
+ return Some ( worker_uuid) ;
582
+ }
562
583
563
- _ => { }
584
+ _ => {
585
+ self . retire ( & worker_uuid) ;
586
+ self . maybe_active_worker ( service_path, force_create)
564
587
}
565
588
}
566
589
}
0 commit comments