fix: endless poll on an errored ws stream (#683)

* fix: endless poll on an errored ws stream
This commit is contained in:
imspace 2020-06-25 12:23:13 +08:00 committed by GitHub
parent a08ce0760d
commit 37a37d462f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -420,7 +420,7 @@ pub mod subscriptions {
},
};
use futures::{channel::mpsc, Future, StreamExt as _};
use futures::{channel::mpsc, Future, StreamExt as _, TryStreamExt as _};
use juniper::{http::GraphQLRequest, InputValue, ScalarValue, SubscriptionCoordinator as _};
use juniper_subscriptions::Coordinator;
use serde::{Deserialize, Serialize};
@ -458,8 +458,14 @@ pub mod subscriptions {
let context = Arc::new(context);
let running = Arc::new(AtomicBool::new(false));
let got_close_signal = Arc::new(AtomicBool::new(false));
let got_close_signal2 = got_close_signal.clone();
sink_rx.fold(Ok(()), move |_, msg| {
sink_rx
.map_err(move |e| {
got_close_signal2.store(true, Ordering::Relaxed);
failure::format_err!("Websocket error: {}", e)
})
.try_fold((), move |_, msg| {
let coordinator = coordinator.clone();
let context = context.clone();
let running = running.clone();
@ -467,14 +473,6 @@ pub mod subscriptions {
let ws_tx = ws_tx.clone();
async move {
let msg = match msg {
Ok(m) => m,
Err(e) => {
got_close_signal.store(true, Ordering::Relaxed);
return Err(failure::format_err!("Websocket error: {}", e));
}
};
if msg.is_close() {
return Ok(());
}
@ -521,8 +519,10 @@ pub mod subscriptions {
payload.variables,
);
let values_stream =
match coordinator.subscribe(&graphql_request, &context).await {
let values_stream = match coordinator
.subscribe(&graphql_request, &context)
.await
{
Ok(s) => s,
Err(err) => {
let _ =
@ -551,7 +551,9 @@ pub mod subscriptions {
let request_id = request_id.clone();
let closed = got_close_signal.load(Ordering::Relaxed);
if !closed {
let mut response_text = serde_json::to_string(&response)
let mut response_text = serde_json::to_string(
&response,
)
.unwrap_or("Error deserializing response".to_owned());
response_text = format!(
@ -559,8 +561,9 @@ pub mod subscriptions {
request_id, response_text
);
let _ = ws_tx
.unbounded_send(Some(Ok(Message::text(response_text))));
let _ = ws_tx.unbounded_send(Some(Ok(Message::text(
response_text,
))));
}
async move { !closed }