231: Error only on close message when fetching the rate r=thomaseizinger a=da-kami

Ping/Pong messages disturb the rate requests quite frequently resulting in failed swap setup because there is no rate available.

As a result messages Ping, Pong and Binary are now ignored and not reported as error.


Co-authored-by: Daniel Karzel <daniel@comit.network>
This commit is contained in:
bors[bot] 2021-02-26 02:33:13 +00:00 committed by GitHub
commit 93d59398af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,8 @@ use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::convert::TryFrom;
use tokio::sync::watch;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::tungstenite::{protocol::CloseFrame, Message};
use tracing::{error, trace};
use watch::Receiver;
const KRAKEN_WS_URL: &str = "wss://ws.kraken.com";
@ -36,8 +37,8 @@ impl LatestRate for RateService {
pub enum Error {
#[error("Rate has not yet been retrieved from Kraken websocket API")]
NotYetRetrieved,
#[error("Message is not text")]
NonTextMessage,
#[error("Received close message from Kraken")]
CloseMessage,
#[error("Websocket: ")]
WebSocket(String),
#[error("Serde: ")]
@ -77,11 +78,27 @@ impl RateService {
while let Some(msg) = rate_stream.next().await {
let msg = match msg {
Ok(Message::Text(msg)) => msg,
Ok(_) => {
let _ = rate_update.send(Err(Error::NonTextMessage));
Ok(Message::Close(close_frame)) => {
if let Some(CloseFrame { code, reason }) = close_frame {
error!(
"Kraken rate stream was closed with code {} and reason: {}",
code, reason
);
} else {
error!("Kraken rate stream was closed without code and reason");
}
let _ = rate_update.send(Err(Error::CloseMessage));
continue;
}
Ok(msg) => {
trace!(
"Kraken rate stream returned non text message that will be ignored: {}",
msg
);
continue;
}
Err(e) => {
error!("Error when reading from Kraken rate stream: {}", e);
let _ = rate_update.send(Err(e.into()));
continue;
}