@@ -23,8 +23,12 @@ use tokio::sync::mpsc::Sender;
23
23
use tokio:: sync:: { mpsc, oneshot, watch} ;
24
24
use tokio_util:: sync:: CancellationToken ;
25
25
26
- pub enum ServerCodes {
27
- Listening ,
26
+ pub enum ServerEvent {
27
+ ConnectionError ( hyper:: Error ) ,
28
+ }
29
+
30
+ pub enum ServerHealth {
31
+ Listening ( mpsc:: UnboundedReceiver < ServerEvent > ) ,
28
32
Failure ,
29
33
}
30
34
@@ -158,7 +162,7 @@ pub struct Server {
158
162
ip : Ipv4Addr ,
159
163
port : u16 ,
160
164
main_worker_req_tx : mpsc:: UnboundedSender < WorkerRequestMsg > ,
161
- callback_tx : Option < Sender < ServerCodes > > ,
165
+ callback_tx : Option < Sender < ServerHealth > > ,
162
166
termination_token : TerminationToken ,
163
167
}
164
168
@@ -172,7 +176,7 @@ impl Server {
172
176
maybe_user_worker_policy : Option < WorkerPoolPolicy > ,
173
177
import_map_path : Option < String > ,
174
178
no_module_cache : bool ,
175
- callback_tx : Option < Sender < ServerCodes > > ,
179
+ callback_tx : Option < Sender < ServerHealth > > ,
176
180
entrypoints : WorkerEntrypoints ,
177
181
termination_token : Option < TerminationToken > ,
178
182
) -> Result < Self , Error > {
@@ -237,10 +241,14 @@ impl Server {
237
241
let listener = TcpListener :: bind ( & addr) . await ?;
238
242
let termination_token = self . termination_token . clone ( ) ;
239
243
244
+ let mut can_receive_event = false ;
245
+ let ( event_tx, event_rx) = mpsc:: unbounded_channel ( ) ;
246
+
240
247
debug ! ( "edge-runtime is listening on {:?}" , listener. local_addr( ) ?) ;
241
248
242
249
if let Some ( callback) = self . callback_tx . clone ( ) {
243
- let _ = callback. send ( ServerCodes :: Listening ) . await ;
250
+ can_receive_event = true ;
251
+ let _ = callback. send ( ServerHealth :: Listening ( event_rx) ) . await ;
244
252
}
245
253
246
254
loop {
@@ -250,21 +258,28 @@ impl Server {
250
258
msg = listener. accept( ) => {
251
259
match msg {
252
260
Ok ( ( conn, _) ) => {
253
- tokio:: task:: spawn( async move {
254
- let ( service, cancel) = WorkerService :: new( main_worker_req_tx) ;
255
- let _guard = cancel. drop_guard( ) ;
256
-
257
- let conn_fut = Http :: new( )
258
- . serve_connection( conn, service) ;
259
-
260
- if let Err ( e) = conn_fut. await {
261
- // Most common cause for these errors are
262
- // when the client closes the connection
263
- // before we could send a response
264
- if e. is_incomplete_message( ) {
265
- debug!( "connection reset ({:?})" , e) ;
266
- } else {
267
- error!( "client connection error ({:?})" , e) ;
261
+ tokio:: task:: spawn( {
262
+ let event_tx = event_tx. clone( ) ;
263
+ async move {
264
+ let ( service, cancel) = WorkerService :: new( main_worker_req_tx) ;
265
+ let _guard = cancel. drop_guard( ) ;
266
+
267
+ let conn_fut = Http :: new( )
268
+ . serve_connection( conn, service) ;
269
+
270
+ if let Err ( e) = conn_fut. await {
271
+ // Most common cause for these errors are
272
+ // when the client closes the connection
273
+ // before we could send a response
274
+ if e. is_incomplete_message( ) {
275
+ debug!( "connection reset ({:?})" , e) ;
276
+ } else {
277
+ error!( "client connection error ({:?})" , e) ;
278
+ }
279
+
280
+ if can_receive_event {
281
+ let _ = event_tx. send( ServerEvent :: ConnectionError ( e) ) ;
282
+ }
268
283
}
269
284
}
270
285
} ) ;
0 commit comments