Browse Source

Do not fallback to HTTP on first error (#1061)

Instead use the same failure counter.

Fixes #496
C 1 month ago
parent
commit
053a3a7e43
1 changed files with 21 additions and 15 deletions
  1. 21 15
      crates/cdk/src/wallet/subscription/ws.rs

+ 21 - 15
crates/cdk/src/wallet/subscription/ws.rs

@@ -69,8 +69,6 @@ pub async fn ws_main(
         };
         tracing::debug!("Connected to {}", url);
 
-        failure_count = 0;
-
         let (mut write, mut read) = ws_stream.split();
         let req_id = AtomicUsize::new(0);
 
@@ -147,23 +145,31 @@ pub async fn ws_main(
                         WsMessageOrResponse::Response(response) => {
                             tracing::debug!("Received response from server: {:?}", response);
                             subscription_requests.remove(&response.id);
+                            // reset connection failure after a successful response from the serer
+                            failure_count = 0;
                         }
                         WsMessageOrResponse::ErrorResponse(error) => {
                             tracing::error!("Received error from server: {:?}", error);
+
                             if subscription_requests.contains(&error.id) {
-                                tracing::error!(
-                                    "Falling back to HTTP client"
-                                );
-
-                                return http_main(
-                                    active_subscriptions.into_keys(),
-                                    http_client,
-                                    subscriptions,
-                                    new_subscription_recv,
-                                    on_drop,
-                                    wallet,
-                                )
-                                .await;
+                                failure_count += 1;
+                                if failure_count > MAX_ATTEMPT_FALLBACK_HTTP {
+                                    tracing::error!(
+                                        "Falling back to HTTP client"
+                                    );
+
+                                    return http_main(
+                                        active_subscriptions.into_keys(),
+                                        http_client,
+                                        subscriptions,
+                                        new_subscription_recv,
+                                        on_drop,
+                                        wallet,
+                                    )
+                                    .await;
+                                }
+
+                                break; // break connection to force a reconnection, to attempt to recover form this error
                             }
                         }
                     }