@@ -34,7 +34,7 @@ pub(crate) struct WorkerCommand(Conn);
34
34
/// and `false` if some connections still alive.
35
35
pub ( crate ) struct StopCommand {
36
36
graceful : bool ,
37
- result : oneshot:: Sender < bool > ,
37
+ tx : oneshot:: Sender < bool > ,
38
38
}
39
39
40
40
#[ derive( Debug ) ]
@@ -98,8 +98,8 @@ impl WorkerHandle {
98
98
}
99
99
100
100
pub fn stop ( & self , graceful : bool ) -> oneshot:: Receiver < bool > {
101
- let ( result , rx) = oneshot:: channel ( ) ;
102
- let _ = self . tx2 . send ( StopCommand { graceful, result } ) ;
101
+ let ( tx , rx) = oneshot:: channel ( ) ;
102
+ let _ = self . tx2 . send ( StopCommand { graceful, tx } ) ;
103
103
rx
104
104
}
105
105
}
@@ -221,7 +221,7 @@ impl ServerWorker {
221
221
let mut wrk = MAX_CONNS_COUNTER . with ( move |conns| ServerWorker {
222
222
rx,
223
223
rx2,
224
- services : Default :: default ( ) ,
224
+ services : Vec :: new ( ) ,
225
225
availability,
226
226
factories,
227
227
state : Default :: default ( ) ,
@@ -272,11 +272,15 @@ impl ServerWorker {
272
272
WorkerHandle :: new ( idx, tx1, tx2, avail)
273
273
}
274
274
275
- fn restart_service ( & mut self , token : Token , idx : usize ) {
276
- let factory = & self . factories [ idx ] ;
275
+ fn restart_service ( & mut self , token : Token , factory_id : usize ) {
276
+ let factory = & self . factories [ factory_id ] ;
277
277
trace ! ( "Service {:?} failed, restarting" , factory. name( token) ) ;
278
278
self . services [ token. 0 ] . status = WorkerServiceStatus :: Restarting ;
279
- self . state = WorkerState :: Restarting ( idx, token, factory. create ( ) ) ;
279
+ self . state = WorkerState :: Restarting ( Restart {
280
+ factory_id,
281
+ token,
282
+ fut : factory. create ( ) ,
283
+ } ) ;
280
284
}
281
285
282
286
fn shutdown ( & mut self , force : bool ) {
@@ -342,16 +346,24 @@ impl ServerWorker {
342
346
enum WorkerState {
343
347
Available ,
344
348
Unavailable ,
345
- Restarting (
346
- usize ,
347
- Token ,
348
- LocalBoxFuture < ' static , Result < Vec < ( Token , BoxedServerService ) > , ( ) > > ,
349
- ) ,
350
- // Shutdown keep states necessary for server shutdown:
351
- // Sleep for interval check the shutdown progress.
352
- // Instant for the start time of shutdown.
353
- // Sender for send back the shutdown outcome(force/grace) to StopCommand caller.
354
- Shutdown ( Pin < Box < Sleep > > , Instant , oneshot:: Sender < bool > ) ,
349
+ Restarting ( Restart ) ,
350
+ Shutdown ( Shutdown ) ,
351
+ }
352
+
353
+ struct Restart {
354
+ factory_id : usize ,
355
+ token : Token ,
356
+ fut : LocalBoxFuture < ' static , Result < Vec < ( Token , BoxedServerService ) > , ( ) > > ,
357
+ }
358
+
359
+ // Shutdown keep states necessary for server shutdown:
360
+ // Sleep for interval check the shutdown progress.
361
+ // Instant for the start time of shutdown.
362
+ // Sender for send back the shutdown outcome(force/grace) to StopCommand caller.
363
+ struct Shutdown {
364
+ timer : Pin < Box < Sleep > > ,
365
+ start_from : Instant ,
366
+ tx : oneshot:: Sender < bool > ,
355
367
}
356
368
357
369
impl Default for WorkerState {
@@ -367,27 +379,29 @@ impl Future for ServerWorker {
367
379
let this = self . as_mut ( ) . get_mut ( ) ;
368
380
369
381
// `StopWorker` message handler
370
- if let Poll :: Ready ( Some ( StopCommand { graceful, result } ) ) =
382
+ if let Poll :: Ready ( Some ( StopCommand { graceful, tx } ) ) =
371
383
Pin :: new ( & mut this. rx2 ) . poll_recv ( cx)
372
384
{
373
385
this. availability . set ( false ) ;
374
386
let num = num_connections ( ) ;
375
387
if num == 0 {
376
388
info ! ( "Shutting down worker, 0 connections" ) ;
377
- let _ = result . send ( true ) ;
389
+ let _ = tx . send ( true ) ;
378
390
return Poll :: Ready ( ( ) ) ;
379
391
} else if graceful {
380
392
info ! ( "Graceful worker shutdown, {} connections" , num) ;
381
393
this. shutdown ( false ) ;
382
394
383
- let timer = Box :: pin ( sleep ( Duration :: from_secs ( 1 ) ) ) ;
384
- let start_from = Instant :: now ( ) ;
385
- this. state = WorkerState :: Shutdown ( timer, start_from, result) ;
395
+ this. state = WorkerState :: Shutdown ( Shutdown {
396
+ timer : Box :: pin ( sleep ( Duration :: from_secs ( 1 ) ) ) ,
397
+ start_from : Instant :: now ( ) ,
398
+ tx,
399
+ } ) ;
386
400
} else {
387
401
info ! ( "Force shutdown worker, {} connections" , num) ;
388
402
this. shutdown ( true ) ;
389
403
390
- let _ = result . send ( false ) ;
404
+ let _ = tx . send ( false ) ;
391
405
return Poll :: Ready ( ( ) ) ;
392
406
}
393
407
}
@@ -405,11 +419,14 @@ impl Future for ServerWorker {
405
419
self . poll ( cx)
406
420
}
407
421
} ,
408
- WorkerState :: Restarting ( idx, token, ref mut fut) => {
409
- let item = ready ! ( fut. as_mut( ) . poll( cx) ) . unwrap_or_else ( |_| {
422
+ WorkerState :: Restarting ( ref mut restart) => {
423
+ let factory_id = restart. factory_id ;
424
+ let token = restart. token ;
425
+
426
+ let item = ready ! ( restart. fut. as_mut( ) . poll( cx) ) . unwrap_or_else ( |_| {
410
427
panic ! (
411
428
"Can not restart {:?} service" ,
412
- this. factories[ idx ] . name( token)
429
+ this. factories[ factory_id ] . name( token)
413
430
)
414
431
} ) ;
415
432
@@ -421,37 +438,37 @@ impl Future for ServerWorker {
421
438
422
439
trace ! (
423
440
"Service {:?} has been restarted" ,
424
- this. factories[ idx ] . name( token)
441
+ this. factories[ factory_id ] . name( token)
425
442
) ;
426
443
427
444
this. services [ token. 0 ] . created ( service) ;
428
445
this. state = WorkerState :: Unavailable ;
429
446
430
447
self . poll ( cx)
431
448
}
432
- WorkerState :: Shutdown ( ref mut timer , ref start_from , _ ) => {
449
+ WorkerState :: Shutdown ( ref mut shutdown ) => {
433
450
// Wait for 1 second.
434
- ready ! ( timer. as_mut( ) . poll( cx) ) ;
451
+ ready ! ( shutdown . timer. as_mut( ) . poll( cx) ) ;
435
452
436
453
if num_connections ( ) == 0 {
437
454
// Graceful shutdown.
438
- if let WorkerState :: Shutdown ( _ , _ , sender ) = mem:: take ( & mut this. state ) {
439
- let _ = sender . send ( true ) ;
455
+ if let WorkerState :: Shutdown ( shutdown ) = mem:: take ( & mut this. state ) {
456
+ let _ = shutdown . tx . send ( true ) ;
440
457
}
441
458
Arbiter :: current ( ) . stop ( ) ;
442
459
Poll :: Ready ( ( ) )
443
- } else if start_from. elapsed ( ) >= this. shutdown_timeout {
460
+ } else if shutdown . start_from . elapsed ( ) >= this. shutdown_timeout {
444
461
// Timeout forceful shutdown.
445
- if let WorkerState :: Shutdown ( _ , _ , sender ) = mem:: take ( & mut this. state ) {
446
- let _ = sender . send ( false ) ;
462
+ if let WorkerState :: Shutdown ( shutdown ) = mem:: take ( & mut this. state ) {
463
+ let _ = shutdown . tx . send ( false ) ;
447
464
}
448
465
Arbiter :: current ( ) . stop ( ) ;
449
466
Poll :: Ready ( ( ) )
450
467
} else {
451
468
// Reset timer and wait for 1 second.
452
469
let time = Instant :: now ( ) + Duration :: from_secs ( 1 ) ;
453
- timer. as_mut ( ) . reset ( time) ;
454
- timer. as_mut ( ) . poll ( cx)
470
+ shutdown . timer . as_mut ( ) . reset ( time) ;
471
+ shutdown . timer . as_mut ( ) . poll ( cx)
455
472
}
456
473
}
457
474
// actively poll stream and handle worker command
0 commit comments