@@ -10,22 +10,26 @@ use http::{HeaderValue, Request};
1010use tonic:: body:: Body ;
1111use tonic:: transport:: { Channel as TonicChannel , ClientTlsConfig , Endpoint } ;
1212use tonic:: { Code , Status } ;
13- use tower:: filter:: { AsyncFilter , AsyncFilterLayer , AsyncPredicate } ;
14- use tower:: util:: Either ;
13+ use tower:: filter:: { AsyncFilter , AsyncPredicate } ;
1514use tower:: { BoxError , ServiceBuilder } ;
1615
1716use token_source:: { TokenSource , TokenSourceProvider } ;
1817
19- pub type Channel = Either < AsyncFilter < TonicChannel , AsyncAuthInterceptor > , TonicChannel > ;
18+ pub type Channel = AsyncFilter < TonicChannel , AsyncAuthInterceptor > ;
2019
2120#[ derive( Clone , Debug ) ]
2221pub struct AsyncAuthInterceptor {
23- token_source : Arc < dyn TokenSource > ,
22+ token_source : Option < Arc < dyn TokenSource > > ,
2423}
2524
2625impl AsyncAuthInterceptor {
2726 fn new ( token_source : Arc < dyn TokenSource > ) -> Self {
28- Self { token_source }
27+ Self {
28+ token_source : Some ( token_source) ,
29+ }
30+ }
31+ fn empty ( ) -> Self {
32+ Self { token_source : None }
2933 }
3034}
3135
@@ -34,7 +38,10 @@ impl AsyncPredicate<Request<Body>> for AsyncAuthInterceptor {
3438 type Request = Request < Body > ;
3539
3640 fn check ( & mut self , request : Request < Body > ) -> Self :: Future {
37- let ts = self . token_source . clone ( ) ;
41+ let ts = match & self . token_source {
42+ Some ( ts) => ts. clone ( ) ,
43+ None => return Box :: pin ( async move { Ok ( request) } ) ,
44+ } ;
3845 Box :: pin ( async move {
3946 let token = ts
4047 . token ( )
@@ -154,8 +161,8 @@ impl<'a> ConnectionManager {
154161
155162 let con = Self :: connect ( endpoint) . await ?;
156163 // use GCP token per call
157- let auth_layer = Some ( AsyncFilterLayer :: new ( AsyncAuthInterceptor :: new ( Arc :: clone ( & ts) ) ) ) ;
158- let auth_con = ServiceBuilder :: new ( ) . option_layer ( auth_layer ) . service ( con) ;
164+ let auth_filter = AsyncAuthInterceptor :: new ( Arc :: clone ( & ts) ) ;
165+ let auth_con = ServiceBuilder :: new ( ) . filter_async ( auth_filter ) . service ( con) ;
159166 conns. push ( auth_con) ;
160167 }
161168 Ok ( conns)
@@ -171,11 +178,9 @@ impl<'a> ConnectionManager {
171178 let endpoint = conn_options. apply ( endpoint) ;
172179
173180 let con = Self :: connect ( endpoint) . await ?;
174- conns. push (
175- ServiceBuilder :: new ( )
176- . option_layer :: < AsyncFilterLayer < AsyncAuthInterceptor > > ( None )
177- . service ( con) ,
178- ) ;
181+ let auth_filter = AsyncAuthInterceptor :: empty ( ) ;
182+ let auth_con = ServiceBuilder :: new ( ) . filter_async ( auth_filter) . service ( con) ;
183+ conns. push ( auth_con) ;
179184 Ok ( conns)
180185 }
181186
0 commit comments