@@ -18,7 +18,7 @@ use irpc::{
18
18
} ;
19
19
use n0_future:: { future:: Boxed as BoxFuture , TryFutureExt } ;
20
20
use serde:: de:: DeserializeOwned ;
21
- use tracing:: { trace, trace_span, warn, Instrument } ;
21
+ use tracing:: { debug , error_span , trace, trace_span, warn, Instrument } ;
22
22
23
23
/// Returns a client that connects to a irpc service using an [`iroh::Endpoint`].
24
24
pub fn client < S : irpc:: Service > (
@@ -207,6 +207,10 @@ pub async fn handle_connection<R: DeserializeOwned + 'static>(
207
207
connection : Connection ,
208
208
handler : Handler < R > ,
209
209
) -> io:: Result < ( ) > {
210
+ if let Ok ( remote) = connection. remote_node_id ( ) {
211
+ tracing:: Span :: current ( ) . record ( "remote" , tracing:: field:: display ( remote. fmt_short ( ) ) ) ;
212
+ }
213
+ debug ! ( "connection accepted" ) ;
210
214
loop {
211
215
let Some ( ( msg, rx, tx) ) = read_request_raw ( & connection) . await ? else {
212
216
return Ok ( ( ) ) ;
@@ -270,19 +274,32 @@ pub async fn read_request_raw<R: DeserializeOwned + 'static>(
270
274
pub async fn listen < R : DeserializeOwned + ' static > ( endpoint : iroh:: Endpoint , handler : Handler < R > ) {
271
275
let mut request_id = 0u64 ;
272
276
let mut tasks = n0_future:: task:: JoinSet :: new ( ) ;
273
- while let Some ( incoming) = endpoint. accept ( ) . await {
277
+ loop {
278
+ let incoming = tokio:: select! {
279
+ Some ( res) = tasks. join_next( ) , if !tasks. is_empty( ) => {
280
+ res. expect( "irpc connection task panicked" ) ;
281
+ continue ;
282
+ }
283
+ incoming = endpoint. accept( ) => {
284
+ match incoming {
285
+ None => break ,
286
+ Some ( incoming) => incoming
287
+ }
288
+ }
289
+ } ;
274
290
let handler = handler. clone ( ) ;
275
291
let fut = async move {
276
- let connection = match incoming. await {
277
- Ok ( connection) => connection,
292
+ match incoming. await {
293
+ Ok ( connection) => match handle_connection ( connection, handler) . await {
294
+ Err ( err) => warn ! ( "connection closed with error: {err:?}" ) ,
295
+ Ok ( ( ) ) => debug ! ( "connection closed" ) ,
296
+ } ,
278
297
Err ( cause) => {
279
- warn ! ( "failed to accept connection {cause:?}" ) ;
280
- return io:: Result :: Ok ( ( ) ) ;
298
+ warn ! ( "failed to accept connection: {cause:?}" ) ;
281
299
}
282
300
} ;
283
- handle_connection ( connection, handler) . await
284
301
} ;
285
- let span = trace_span ! ( "rpc" , id = request_id) ;
302
+ let span = error_span ! ( "rpc" , id = request_id, remote = tracing :: field :: Empty ) ;
286
303
tasks. spawn ( fut. instrument ( span) ) ;
287
304
request_id += 1 ;
288
305
}
0 commit comments