@@ -25,6 +25,7 @@ use tokio_stream::StreamExt as _;
25
25
use tower:: util:: BoxCloneService ;
26
26
use tower:: util:: Oneshot ;
27
27
use tower:: ServiceExt ;
28
+ use tracing:: debug;
28
29
use tracing:: trace;
29
30
30
31
#[ cfg( feature = "tls" ) ]
@@ -563,14 +564,14 @@ impl<L> Server<L> {
563
564
564
565
tokio:: pin!( incoming) ;
565
566
567
+ let graceful = signal. is_some ( ) ;
566
568
let sig = Fuse { inner : signal } ;
567
569
tokio:: pin!( sig) ;
568
570
569
571
loop {
570
572
tokio:: select! {
571
573
_ = & mut sig => {
572
574
trace!( "signal received, shutting down" ) ;
573
- drop( signal_rx) ;
574
575
break ;
575
576
} ,
576
577
io = incoming. next( ) => {
@@ -580,7 +581,9 @@ impl<L> Server<L> {
580
581
trace!( "error accepting connection: {:#}" , e) ;
581
582
continue ;
582
583
} ,
583
- None => break ,
584
+ None => {
585
+ break
586
+ } ,
584
587
} ;
585
588
586
589
trace!( "connection accepted" ) ;
@@ -595,11 +598,23 @@ impl<L> Server<L> {
595
598
. map_err( super :: Error :: from_source) ?;
596
599
let hyper_svc = TowerToHyperService :: new( req_svc) ;
597
600
598
- serve_connection( io, hyper_svc, builder. clone( ) , signal_tx . clone( ) ) ;
601
+ serve_connection( io, hyper_svc, builder. clone( ) , graceful . then ( || signal_rx . clone( ) ) ) ;
599
602
}
600
603
}
601
604
}
602
605
606
+ if graceful {
607
+ let _ = signal_tx. send ( ( ) ) ;
608
+ drop ( signal_rx) ;
609
+ trace ! (
610
+ "waiting for {} connections to close" ,
611
+ signal_tx. receiver_count( )
612
+ ) ;
613
+
614
+ // Wait for all connections to close
615
+ signal_tx. closed ( ) . await ;
616
+ }
617
+
603
618
Ok ( ( ) )
604
619
}
605
620
}
@@ -610,7 +625,7 @@ fn serve_connection<IO, S>(
610
625
io : ServerIo < IO > ,
611
626
hyper_svc : TowerToHyperService < S > ,
612
627
builder : hyper_util:: server:: conn:: auto:: Builder < TokioExecutor > ,
613
- watcher : Arc < tokio:: sync:: watch:: Sender < ( ) > > ,
628
+ mut watcher : Option < tokio:: sync:: watch:: Receiver < ( ) > > ,
614
629
) where
615
630
S : Service < Request , Response = Response > + Clone + Send + ' static ,
616
631
S :: Future : Send + ' static ,
@@ -619,30 +634,32 @@ fn serve_connection<IO, S>(
619
634
IO :: ConnectInfo : Clone + Send + Sync + ' static ,
620
635
{
621
636
tokio:: spawn ( async move {
622
- let sig = Fuse {
623
- inner : Some ( watcher. closed ( ) ) ,
624
- } ;
637
+ {
638
+ let sig = Fuse {
639
+ inner : watcher. as_mut ( ) . map ( |w| w. changed ( ) ) ,
640
+ } ;
625
641
626
- tokio:: pin!( sig) ;
642
+ tokio:: pin!( sig) ;
627
643
628
- let conn = builder. serve_connection ( TokioIo :: new ( io) , hyper_svc) ;
629
- tokio:: pin!( conn) ;
644
+ let conn = builder. serve_connection ( TokioIo :: new ( io) , hyper_svc) ;
645
+ tokio:: pin!( conn) ;
630
646
631
- loop {
632
- tokio:: select! {
633
- rv = & mut conn => {
634
- if let Err ( err) = rv {
635
- trace!( "failed serving connection: {:#}" , err) ;
647
+ loop {
648
+ tokio:: select! {
649
+ rv = & mut conn => {
650
+ if let Err ( err) = rv {
651
+ debug!( "failed serving connection: {:#}" , err) ;
652
+ }
653
+ break ;
654
+ } ,
655
+ _ = & mut sig => {
656
+ conn. as_mut( ) . graceful_shutdown( ) ;
636
657
}
637
- break ;
638
- } ,
639
- _ = & mut sig => {
640
- trace!( "signal received, shutting down" ) ;
641
- conn. as_mut( ) . graceful_shutdown( ) ;
642
658
}
643
659
}
644
660
}
645
661
662
+ drop ( watcher) ;
646
663
trace ! ( "connection closed" ) ;
647
664
} ) ;
648
665
}
@@ -1057,6 +1074,12 @@ struct Fuse<F> {
1057
1074
inner : Option < F > ,
1058
1075
}
1059
1076
1077
+ impl < F > Fuse < F > {
1078
+ fn is_terminated ( self : & Pin < & mut Self > ) -> bool {
1079
+ self . inner . is_none ( )
1080
+ }
1081
+ }
1082
+
1060
1083
impl < F > Future for Fuse < F >
1061
1084
where
1062
1085
F : Future ,
0 commit comments