stream-reconnect
This crate provides a Stream
/Sink
-wrapping struct that automatically recover from potential
disconnections/interruptions.
This is a fork of stubborn-io, which is built for the same purpose but
for AsyncRead
/AsyncWrite
.
To use with your project, add the following to your Cargo.toml:
stream-reconnect = "0.3"
Minimum supported rust version: 1.43.1
Runtime Support
This crate supports both tokio
and async-std
runtime.
tokio
support is enabled by default. While used on an async-std
runtime, change the corresponding dependency
in Cargo.toml
to
stream-reconnect = { version = "0.3", default-features = false, features = ["async-std"] }
Example
In this example, we will see a drop in replacement for tungstenite's WebSocketStream, with the distinction that it will
automatically attempt to reconnect in the face of connectivity failures.
struct MyWs(WebSocketStream<MaybeTlsStream<TcpStream>>);
impl UnderlyingStream<String, Message, WsError> for MyWs {
fn establish(addr: String) -> Pin<Box<dyn Future<Output=Result<Self, WsError>> + Send>> {
Box::pin(async move {
let ws_connection = connect_async(addr).await.unwrap().0;
Ok(MyWs(ws_connection))
})
}
fn is_write_disconnect_error(&self, err: &WsError) -> bool {
matches!(
err,
WsError::ConnectionClosed
| WsError::AlreadyClosed
| WsError::Io(_)
| WsError::Tls(_)
| WsError::Protocol(_)
)
}
fn is_read_disconnect_error(&self, item: &Result<Message, WsError>) -> bool {
if let Err(e) = item {
self.is_write_disconnect_error(e)
} else {
false
}
}
fn exhaust_err() -> WsError {
WsError::Io(io::Error::new(io::ErrorKind::Other, "Exhausted"))
}
}
type ReconnectWs = ReconnectStream<MyWs, String, Result<Message, WsError>, WsError>;
#[tokio::main]
async fn main() {
let mut ws_stream: ReconnectWs = ReconnectWs::connect(String::from("wss://localhost:8000"));
ws_stream.send("hello world!").await.unwrap();
}
License
MIT