Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to renew the connection upon transient error #304

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 36 additions & 25 deletions swap/src/kraken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,44 @@ pub fn connect() -> Result<RateUpdateStream> {
async move {
let mut stream = connection::new().await?;

while let Some(update) = stream.try_next().await.map_err(to_backoff)? {
let send_result = rate_update.send(Ok(update));

if send_result.is_err() {
return Err(backoff::Error::Permanent(anyhow!(
"receiver disconnected"
)));
loop {
let update = stream.try_next().await;

match update {
Ok(rate) => {
if let Some(rate) = rate {
let send_result = rate_update.send(Ok(rate));

if send_result.is_err() {
return Err(backoff::Error::Permanent(anyhow!(
"receiver disconnected"
)));
}
}
}
Err(e) => {
match e {
// Connection closures and websocket errors will be retried
connection::Error::ConnectionClosed => {
// Try to renew the connection in case of websocket failure
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Try to renew the connection in case of websocket failure
// Try to renew the connection in case of connection failure

// This failure can be caused by Cloudflare closing he connection for specific IPs
stream = connection::new().await?;
return Err(backoff::Error::Transient(anyhow::Error::from(e)));
},
connection::Error::WebSocket(_) => {
// Try to renew the connection in case of websocket failure
// This failure can be caused by Cloudflare closing he connection for specific IPs
stream = connection::new().await?;
return Err(backoff::Error::Transient(anyhow::Error::from(e)));
},

// Failures while parsing a message are permanent because they most likely present a
// programmer error
connection::Error::Parse(_) => { return Err(backoff::Error::Permanent(anyhow::Error::from(e))) },
}
}
}
}

Err(backoff::Error::Transient(anyhow!("stream ended")))
}
},
|error, next: Duration| {
Expand Down Expand Up @@ -84,22 +111,6 @@ pub enum Error {

type RateUpdate = Result<Rate, Error>;

/// Maps a [`connection::Error`] to a backoff error, effectively defining our
/// retry strategy.
fn to_backoff(e: connection::Error) -> backoff::Error<anyhow::Error> {
use backoff::Error::*;

match e {
// Connection closures and websocket errors will be retried
connection::Error::ConnectionClosed => Transient(anyhow::Error::from(e)),
connection::Error::WebSocket(_) => Transient(anyhow::Error::from(e)),

// Failures while parsing a message are permanent because they most likely present a
// programmer error
connection::Error::Parse(_) => Permanent(anyhow::Error::from(e)),
}
}

/// Kraken websocket connection module.
///
/// Responsible for establishing a connection to the Kraken websocket API and
Expand Down