refactor: replace reconnecting-jsonrpsee-ws-client with subxt-reconnecting-rpc-client (#1705)

* feat: add native subxt rpc reconn client

* add jsonrpsee dep to reconnecting-client

* Update subxt/src/backend/rpc/reconnecting_rpc_client/tests.rs

* fix grumbles

* add simple wasm test for reconnecting client

* fix test build

* cargo fmt

* remove reconnect apis

* Update testing/wasm-rpc-tests/tests/wasm.rs

* Update subxt/src/backend/rpc/reconnecting_rpc_client/tests.rs

* Update subxt/src/backend/rpc/reconnecting_rpc_client/tests.rs
This commit is contained in:
Niklas Adolfsson
2024-08-27 15:18:06 +02:00
committed by GitHub
parent 193452e95f
commit 4bc27d4977
14 changed files with 1102 additions and 343 deletions
@@ -9,7 +9,7 @@
use std::time::Duration;
use futures::StreamExt;
use subxt::backend::rpc::reconnecting_rpc_client::{Client, ExponentialBackoff};
use subxt::backend::rpc::reconnecting_rpc_client::{ExponentialBackoff, RpcClient};
use subxt::{OnlineClient, PolkadotConfig};
// Generate an interface that we can use from the node's metadata.
@@ -21,7 +21,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
// Create a new client with with a reconnecting RPC client.
let rpc = Client::builder()
let rpc = RpcClient::builder()
// Reconnect with exponential backoff
//
// This API is "iterator-like" and we use `take` to limit the number of retries.
@@ -53,22 +53,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api: OnlineClient<PolkadotConfig> = OnlineClient::from_rpc_client(rpc.clone()).await?;
// Optionally print if the RPC client reconnects.
let rpc2 = rpc.clone();
tokio::spawn(async move {
loop {
// The connection was lost and the client is trying to reconnect.
let reconnected = rpc2.reconnect_initiated().await;
let now = std::time::Instant::now();
// The connection was re-established.
reconnected.await;
println!(
"RPC client reconnection took `{}s`",
now.elapsed().as_secs()
);
}
});
// Run for at most 100 blocks and print a bunch of information about it.
//
// The subscription is automatically re-started when the RPC client has reconnected.
@@ -96,7 +80,5 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Block #{block_number} ({block_hash})");
}
println!("RPC client reconnected `{}` times", rpc.reconnect_count());
Ok(())
}