@@ -13,13 +13,14 @@ use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
1313use std:: sync:: { Arc , Mutex , Once } ;
1414use std:: time:: Duration ;
1515
16+ use anyhow:: { anyhow, bail} ;
1617use async_trait:: async_trait;
1718use futures:: future;
1819use mz_ore:: assert_none;
1920use mz_ore:: netio:: Listener ;
2021use mz_ore:: retry:: Retry ;
2122use mz_service:: client:: GenericClient ;
22- use mz_service:: transport:: { self , ChannelHandler , Message , NoopMetrics } ;
23+ use mz_service:: transport:: { self , Message , NoopMetrics } ;
2324use semver:: Version ;
2425use tokio:: io:: AsyncWriteExt ;
2526use tokio:: sync:: { mpsc, oneshot} ;
@@ -556,6 +557,38 @@ fn test_metrics() {
556557 sim. run ( ) . unwrap ( ) ;
557558}
558559
560+ /// A connection handler that simply forwards messages over channels.
561+ #[ derive( Debug ) ]
562+ pub struct ChannelHandler < In , Out > {
563+ tx : mpsc:: UnboundedSender < In > ,
564+ rx : mpsc:: UnboundedReceiver < Out > ,
565+ }
566+
567+ impl < In , Out > ChannelHandler < In , Out > {
568+ pub fn new ( tx : mpsc:: UnboundedSender < In > , rx : mpsc:: UnboundedReceiver < Out > ) -> Self {
569+ Self { tx, rx }
570+ }
571+ }
572+
573+ #[ async_trait]
574+ impl < In : Message , Out : Message > GenericClient < In , Out > for ChannelHandler < In , Out > {
575+ async fn send ( & mut self , cmd : In ) -> anyhow:: Result < ( ) > {
576+ let result = self . tx . send ( cmd) ;
577+ result. map_err ( |_| anyhow ! ( "client channel disconnected" ) )
578+ }
579+
580+ /// # Cancel safety
581+ ///
582+ /// This method is cancel safe.
583+ async fn recv ( & mut self ) -> anyhow:: Result < Option < Out > > {
584+ // `mpsc::Receiver::recv` is cancel safe.
585+ match self . rx . recv ( ) . await {
586+ Some ( resp) => Ok ( Some ( resp) ) ,
587+ None => bail ! ( "client channel disconnected" ) ,
588+ }
589+ }
590+ }
591+
559592/// A connection handler that produces a single outbound message and then becomes silent.
560593#[ derive( Debug ) ]
561594struct OneOutputHandler {
0 commit comments