@@ -9,8 +9,9 @@ pub use endpoint::Endpoint;
99#[ cfg( feature = "tls" ) ]
1010pub use tls:: ClientTlsConfig ;
1111
12- use super :: service:: { Connection , DynamicServiceStream } ;
12+ use super :: service:: { Connection , DynamicServiceStream , SharedExec } ;
1313use crate :: body:: BoxBody ;
14+ use crate :: transport:: Executor ;
1415use bytes:: Bytes ;
1516use http:: {
1617 uri:: { InvalidUri , Uri } ,
@@ -124,10 +125,26 @@ impl Channel {
124125 pub fn balance_channel < K > ( capacity : usize ) -> ( Self , Sender < Change < K , Endpoint > > )
125126 where
126127 K : Hash + Eq + Send + Clone + ' static ,
128+ {
129+ Self :: balance_channel_with_executor ( capacity, SharedExec :: tokio ( ) )
130+ }
131+
132+ /// Balance a list of [`Endpoint`]'s.
133+ ///
134+ /// This creates a [`Channel`] that will listen to a stream of change events and will add or remove provided endpoints.
135+ ///
136+ /// The [`Channel`] will use the given executor to spawn async tasks.
137+ pub fn balance_channel_with_executor < K , E > (
138+ capacity : usize ,
139+ executor : E ,
140+ ) -> ( Self , Sender < Change < K , Endpoint > > )
141+ where
142+ K : Hash + Eq + Send + Clone + ' static ,
143+ E : Executor < Pin < Box < dyn Future < Output = ( ) > + Send > > > + Send + Sync + ' static ,
127144 {
128145 let ( tx, rx) = channel ( capacity) ;
129146 let list = DynamicServiceStream :: new ( rx) ;
130- ( Self :: balance ( list, DEFAULT_BUFFER_SIZE ) , tx)
147+ ( Self :: balance ( list, DEFAULT_BUFFER_SIZE , executor ) , tx)
131148 }
132149
133150 pub ( crate ) fn new < C > ( connector : C , endpoint : Endpoint ) -> Self
@@ -138,9 +155,11 @@ impl Channel {
138155 C :: Response : AsyncRead + AsyncWrite + HyperConnection + Unpin + Send + ' static ,
139156 {
140157 let buffer_size = endpoint. buffer_size . unwrap_or ( DEFAULT_BUFFER_SIZE ) ;
158+ let executor = endpoint. executor . clone ( ) ;
141159
142160 let svc = Connection :: lazy ( connector, endpoint) ;
143- let svc = Buffer :: new ( Either :: A ( svc) , buffer_size) ;
161+ let ( svc, worker) = Buffer :: pair ( Either :: A ( svc) , buffer_size) ;
162+ executor. execute ( Box :: pin ( worker) ) ;
144163
145164 Channel { svc }
146165 }
@@ -153,25 +172,29 @@ impl Channel {
153172 C :: Response : AsyncRead + AsyncWrite + HyperConnection + Unpin + Send + ' static ,
154173 {
155174 let buffer_size = endpoint. buffer_size . unwrap_or ( DEFAULT_BUFFER_SIZE ) ;
175+ let executor = endpoint. executor . clone ( ) ;
156176
157177 let svc = Connection :: connect ( connector, endpoint)
158178 . await
159179 . map_err ( super :: Error :: from_source) ?;
160- let svc = Buffer :: new ( Either :: A ( svc) , buffer_size) ;
180+ let ( svc, worker) = Buffer :: pair ( Either :: A ( svc) , buffer_size) ;
181+ executor. execute ( Box :: pin ( worker) ) ;
161182
162183 Ok ( Channel { svc } )
163184 }
164185
165- pub ( crate ) fn balance < D > ( discover : D , buffer_size : usize ) -> Self
186+ pub ( crate ) fn balance < D , E > ( discover : D , buffer_size : usize , executor : E ) -> Self
166187 where
167188 D : Discover < Service = Connection > + Unpin + Send + ' static ,
168189 D :: Error : Into < crate :: Error > ,
169190 D :: Key : Hash + Send + Clone ,
191+ E : Executor < futures_core:: future:: BoxFuture < ' static , ( ) > > + Send + Sync + ' static ,
170192 {
171193 let svc = Balance :: new ( discover) ;
172194
173195 let svc = BoxService :: new ( svc) ;
174- let svc = Buffer :: new ( Either :: B ( svc) , buffer_size) ;
196+ let ( svc, worker) = Buffer :: pair ( Either :: B ( svc) , buffer_size) ;
197+ executor. execute ( Box :: pin ( worker) ) ;
175198
176199 Channel { svc }
177200 }
0 commit comments