@@ -209,7 +209,7 @@ async fn federation<D: TwinDB, R: RateLimiter>(
209209
210210 let dst: SessionID = ( & envelope. destination ) . into ( ) ;
211211
212- // fast-fail: if requested and destination is not local, return error immediately
212+ // fast-fail path : if requested and destination is not local, return error immediately
213213 if has_fast_fail ( & envelope) && !data. switch . is_local ( & dst) . await {
214214 // destination session doesn’t exist on this relay
215215 return Response :: builder ( )
@@ -376,18 +376,15 @@ impl<M: Metrics, D: TwinDB> Session<M, D> {
376376 }
377377
378378 let is_local = self . switch . is_local ( & dst) . await ;
379- // fast-fail: if requested and destination is not local, return error immediately
380- if has_fast_fail ( envelope) && !is_local {
381- anyhow:: bail!( "destination offline" ) ;
382- }
383379
384380 // check if the dst twin id is already connected locally
385381 // if so, we don't have to check federation and directly
386382 // switch the message
387383 if is_local {
388- log:: debug !( "found local session for '{}' , forwarding message" , dst) ;
384+ log:: trace !( "found local session for '{}' , forwarding message" , dst) ;
389385 if let Err ( err) = self . switch . send ( & dst, & msg) . await {
390386 log:: error!( "failed to route message to peer '{}' : {}" , dst, err) ;
387+ anyhow:: bail!( "failed to route message to peer '{}': {}" , dst, err) ;
391388 }
392389 return Ok ( ( ) ) ;
393390 }
@@ -414,13 +411,18 @@ impl<M: Metrics, D: TwinDB> Session<M, D> {
414411 // push message to the (relay.federation) queue
415412 return Ok ( self . federator . send ( & msg) . await ?) ;
416413 }
417-
414+ // fast-fail path: we confirmed that this is not an foreign message, and if it is fast-fail taged and destination session is not connected
415+ // then the peer must be offline, return error immediately
416+ if has_fast_fail ( envelope) && !is_local {
417+ anyhow:: bail!( "destination offline" ) ;
418+ }
418419 // we don't return an error because when we return an error
419420 // we will send this error back to the sender user. Hence
420421 // calling the switch.send again
421422 // this is an internal error anyway, and should not happen
422423 if let Err ( err) = self . switch . send ( & dst, & msg) . await {
423424 log:: error!( "failed to route message to peer '{}': {}" , dst, err) ;
425+ anyhow:: bail!( "failed to route message to peer '{}': {}" , dst, err) ;
424426 }
425427
426428 Ok ( ( ) )
@@ -474,7 +476,7 @@ impl<M: Metrics, D: TwinDB> Session<M, D> {
474476 // TODO: throttling to avoid sending too many messages in short time!
475477 match message {
476478 Message :: Text ( _) => {
477- log:: trace !( "received unsupported (text) message. disconnecting!" ) ;
479+ log:: debug !( "received unsupported (text) message. disconnecting!" ) ;
478480 break ;
479481 }
480482 Message :: Binary ( msg) => {
@@ -487,16 +489,21 @@ impl<M: Metrics, D: TwinDB> Session<M, D> {
487489 super :: switch:: MESSAGE_RX_TWIN . with_label_values( & [ & format!( "{}" , envelope. source. twin) ] ) . inc( ) ;
488490
489491 if !self . metrics. measure( msg. len( ) ) . await {
490- log:: trace!( "twin with stream id {} exceeded its request limits, dropping message" , self . id) ;
491- self . send_error( envelope, "exceeded rate limits, dropping message" ) . await ;
492- continue
492+ log:: debug!( "twin with stream id {} exceeded its request limits, dropping message" , self . id) ;
493+ if envelope. has_request( ) {
494+ self . send_error( envelope, "exceeded rate limits, dropping message" ) . await ;
495+ }
496+ continue ;
493497 }
494498
495499 // if we failed to route back the message to the user
496500 // for any reason we send an error message back
497501 // server error has no source address set.
498502 if let Err ( err) = self . route( & mut envelope, msg) . await {
499- self . send_error( envelope, err) . await ;
503+ // check if the envolope is request or response
504+ if envelope. has_request( ) {
505+ self . send_error( envelope, err) . await ;
506+ }
500507 }
501508 }
502509 Message :: Ping ( _) => {
0 commit comments