@@ -6,7 +6,8 @@ use std::{
6
6
time:: Duration ,
7
7
} ;
8
8
9
- use futures:: { future:: BoxFuture , FutureExt } ;
9
+ use futures:: future:: BoxFuture ;
10
+ use futures_util:: FutureExt ;
10
11
use snafu:: ResultExt ;
11
12
use tokio:: {
12
13
io:: AsyncWriteExt ,
@@ -26,7 +27,7 @@ use crate::{
26
27
sinks:: { util:: retries:: ExponentialBackoff , Healthcheck } ,
27
28
} ;
28
29
29
- use super :: { NetError , net_error:: * } ;
30
+ use super :: { net_error:: * , NetError , ServiceState } ;
30
31
31
32
/// Unix socket modes.
32
33
#[ configurable_component]
@@ -120,14 +121,16 @@ pub struct UnixConnector {
120
121
impl UnixConnector {
121
122
async fn connect ( & self ) -> Result < ( PathBuf , UnixEither ) , NetError > {
122
123
let either_socket = match self . mode {
123
- UnixMode :: Datagram => UnixDatagram :: unbound ( )
124
- . context ( FailedToBind )
125
- . and_then ( |datagram| {
126
- datagram
127
- . connect ( & self . path )
128
- . context ( FailedToConnect )
129
- . map ( |_| UnixEither :: Datagram ( datagram) )
130
- } ) ?,
124
+ UnixMode :: Datagram => {
125
+ UnixDatagram :: unbound ( )
126
+ . context ( FailedToBind )
127
+ . and_then ( |datagram| {
128
+ datagram
129
+ . connect ( & self . path )
130
+ . context ( FailedToConnect )
131
+ . map ( |_| UnixEither :: Datagram ( datagram) )
132
+ } ) ?
133
+ }
131
134
UnixMode :: Stream => UnixStream :: connect ( & self . path )
132
135
. await
133
136
. context ( FailedToConnect )
@@ -175,34 +178,16 @@ impl UnixConnector {
175
178
}
176
179
}
177
180
178
- enum UnixServiceState {
179
- /// The service is currently disconnected.
180
- Disconnected ,
181
-
182
- /// The service is currently attempting to connect to the endpoint.
183
- Connecting ( BoxFuture < ' static , UnixEither > ) ,
184
-
185
- /// The service is connected and idle.
186
- Connected ( UnixEither ) ,
187
-
188
- /// The service has an in-flight send to the socket.
189
- ///
190
- /// If the socket experiences an unrecoverable error during the send, `None` will be returned
191
- /// over the channel to signal the need to establish a new connection rather than reusing the
192
- /// existing connection.
193
- Sending ( oneshot:: Receiver < Option < UnixEither > > ) ,
194
- }
195
-
196
181
pub struct UnixService {
197
182
connector : UnixConnector ,
198
- state : UnixServiceState ,
183
+ state : ServiceState < UnixEither > ,
199
184
}
200
185
201
186
impl UnixService {
202
187
const fn new ( connector : UnixConnector ) -> Self {
203
188
Self {
204
189
connector,
205
- state : UnixServiceState :: Disconnected ,
190
+ state : ServiceState :: Disconnected ,
206
191
}
207
192
}
208
193
}
@@ -215,24 +200,24 @@ impl Service<Vec<u8>> for UnixService {
215
200
fn poll_ready ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
216
201
loop {
217
202
self . state = match & mut self . state {
218
- UnixServiceState :: Disconnected => {
203
+ ServiceState :: Disconnected => {
219
204
let connector = self . connector . clone ( ) ;
220
- UnixServiceState :: Connecting ( Box :: pin ( async move {
221
- connector. connect_backoff ( ) . await
222
- } ) )
205
+ ServiceState :: Connecting ( Box :: pin (
206
+ async move { connector. connect_backoff ( ) . await } ,
207
+ ) )
223
208
}
224
- UnixServiceState :: Connecting ( fut) => {
209
+ ServiceState :: Connecting ( fut) => {
225
210
let socket = ready ! ( fut. poll_unpin( cx) ) ;
226
- UnixServiceState :: Connected ( socket)
211
+ ServiceState :: Connected ( socket)
227
212
}
228
- UnixServiceState :: Connected ( _) => break ,
229
- UnixServiceState :: Sending ( fut) => {
213
+ ServiceState :: Connected ( _) => break ,
214
+ ServiceState :: Sending ( fut) => {
230
215
match ready ! ( fut. poll_unpin( cx) ) {
231
216
// When a send concludes, and there's an error, the request future sends
232
217
// back `None`. Otherwise, it'll send back `Some(...)` with the socket.
233
218
Ok ( maybe_socket) => match maybe_socket {
234
- Some ( socket) => UnixServiceState :: Connected ( socket) ,
235
- None => UnixServiceState :: Disconnected ,
219
+ Some ( socket) => ServiceState :: Connected ( socket) ,
220
+ None => ServiceState :: Disconnected ,
236
221
} ,
237
222
Err ( _) => return Poll :: Ready ( Err ( NetError :: ServiceSocketChannelClosed ) ) ,
238
223
}
@@ -245,8 +230,8 @@ impl Service<Vec<u8>> for UnixService {
245
230
fn call ( & mut self , buf : Vec < u8 > ) -> Self :: Future {
246
231
let ( tx, rx) = oneshot:: channel ( ) ;
247
232
248
- let mut socket = match std:: mem:: replace ( & mut self . state , UnixServiceState :: Sending ( rx) ) {
249
- UnixServiceState :: Connected ( socket) => socket,
233
+ let mut socket = match std:: mem:: replace ( & mut self . state , ServiceState :: Sending ( rx) ) {
234
+ ServiceState :: Connected ( socket) => socket,
250
235
_ => panic ! ( "poll_ready must be called first" ) ,
251
236
} ;
252
237
0 commit comments