@@ -18,7 +18,7 @@ use router::endpoint::Endpoint;
18
18
use serde:: Serialize ;
19
19
use snafu:: { Backtrace , OptionExt , ResultExt , Snafu } ;
20
20
use table_engine:: { engine:: EngineRuntimes , table:: FlushRequest } ;
21
- use tokio:: sync:: oneshot:: { self , Sender } ;
21
+ use tokio:: sync:: oneshot:: { self , Receiver , Sender } ;
22
22
use warp:: {
23
23
header,
24
24
http:: StatusCode ,
@@ -102,6 +102,9 @@ pub enum Error {
102
102
Internal {
103
103
source : Box < dyn StdError + Send + Sync > ,
104
104
} ,
105
+
106
+ #[ snafu( display( "Server already started.\n Backtrace:\n {}" , backtrace) ) ]
107
+ AlreadyStarted { backtrace : Backtrace } ,
105
108
}
106
109
107
110
define_result ! ( Error ) ;
@@ -122,14 +125,45 @@ pub struct Service<Q> {
122
125
prom_remote_storage : RemoteStorageRef < RequestContext , crate :: handlers:: prom:: Error > ,
123
126
influxdb : Arc < InfluxDb < Q > > ,
124
127
tx : Sender < ( ) > ,
128
+ rx : Option < Receiver < ( ) > > ,
125
129
config : HttpConfig ,
126
130
config_content : String ,
127
131
}
128
132
129
- impl < Q > Service < Q > {
133
+ impl < Q : QueryExecutor + ' static > Service < Q > {
134
+ pub async fn start ( & mut self ) -> Result < ( ) > {
135
+ let ip_addr: IpAddr = self
136
+ . config
137
+ . endpoint
138
+ . addr
139
+ . parse ( )
140
+ . with_context ( || ParseIpAddr {
141
+ ip : self . config . endpoint . addr . to_string ( ) ,
142
+ } ) ?;
143
+ let rx = self . rx . take ( ) . context ( AlreadyStarted ) ?;
144
+
145
+ info ! (
146
+ "HTTP server tries to listen on {}" ,
147
+ & self . config. endpoint. to_string( )
148
+ ) ;
149
+
150
+ // Register filters to warp and rejection handler
151
+ let routes = self . routes ( ) . recover ( handle_rejection) ;
152
+ let ( _addr, server) = warp:: serve ( routes) . bind_with_graceful_shutdown (
153
+ ( ip_addr, self . config . endpoint . port ) ,
154
+ async {
155
+ rx. await . ok ( ) ;
156
+ } ,
157
+ ) ;
158
+
159
+ self . engine_runtimes . bg_runtime . spawn ( server) ;
160
+
161
+ Ok ( ( ) )
162
+ }
163
+
130
164
pub fn stop ( self ) {
131
- if self . tx . send ( ( ) ) . is_err ( ) {
132
- error ! ( "Failed to send http service stop message" ) ;
165
+ if let Err ( e ) = self . tx . send ( ( ) ) {
166
+ error ! ( "Failed to send http service stop message, err:{:?}" , e ) ;
133
167
}
134
168
}
135
169
}
@@ -493,7 +527,7 @@ impl<Q> Builder<Q> {
493
527
impl < Q : QueryExecutor + ' static > Builder < Q > {
494
528
/// Build and start the service
495
529
pub fn build ( self ) -> Result < Service < Q > > {
496
- let engine_runtime = self . engine_runtimes . context ( MissingEngineRuntimes ) ?;
530
+ let engine_runtimes = self . engine_runtimes . context ( MissingEngineRuntimes ) ?;
497
531
let log_runtime = self . log_runtime . context ( MissingLogRuntime ) ?;
498
532
let instance = self . instance . context ( MissingInstance ) ?;
499
533
let config_content = self . config_content . context ( MissingInstance ) ?;
@@ -508,37 +542,18 @@ impl<Q: QueryExecutor + 'static> Builder<Q> {
508
542
let ( tx, rx) = oneshot:: channel ( ) ;
509
543
510
544
let service = Service {
511
- engine_runtimes : engine_runtime . clone ( ) ,
545
+ engine_runtimes,
512
546
log_runtime,
513
547
instance,
514
548
prom_remote_storage,
515
549
influxdb,
516
550
profiler : Arc :: new ( Profiler :: default ( ) ) ,
517
551
tx,
552
+ rx : Some ( rx) ,
518
553
config : self . config . clone ( ) ,
519
554
config_content,
520
555
} ;
521
556
522
- info ! (
523
- "HTTP server tries to listen on {}" ,
524
- & self . config. endpoint. to_string( )
525
- ) ;
526
-
527
- let ip_addr: IpAddr = self . config . endpoint . addr . parse ( ) . context ( ParseIpAddr {
528
- ip : self . config . endpoint . addr ,
529
- } ) ?;
530
-
531
- // Register filters to warp and rejection handler
532
- let routes = service. routes ( ) . recover ( handle_rejection) ;
533
- let ( _addr, server) = warp:: serve ( routes) . bind_with_graceful_shutdown (
534
- ( ip_addr, self . config . endpoint . port ) ,
535
- async {
536
- rx. await . ok ( ) ;
537
- } ,
538
- ) ;
539
- // Run the service
540
- engine_runtime. bg_runtime . spawn ( server) ;
541
-
542
557
Ok ( service)
543
558
}
544
559
}
@@ -571,6 +586,7 @@ fn error_to_status_code(err: &Error) -> StatusCode {
571
586
| Error :: ProfileHeap { .. }
572
587
| Error :: Internal { .. }
573
588
| Error :: JoinAsyncTask { .. }
589
+ | Error :: AlreadyStarted { .. }
574
590
| Error :: HandleUpdateLogLevel { .. } => StatusCode :: INTERNAL_SERVER_ERROR ,
575
591
}
576
592
}
0 commit comments