Skip to content

Commit

Permalink
refactor: replace reconnecting-jsonrpsee-ws-client with `subxt-reco…
Browse files Browse the repository at this point in the history
…nnecting-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
  • Loading branch information
niklasad1 authored Aug 27, 2024
1 parent 193452e commit 4bc27d4
Show file tree
Hide file tree
Showing 14 changed files with 1,102 additions and 343 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ jobs:
uses: actions-rs/cargo@v1.0.3
with:
command: test
args: --doc
args: --doc --features unstable-reconnecting-rpc-client

- if: "failure()"
uses: "andymckay/cancel-action@a955d435292c0d409d104b57d8e78435a93a6ef1" # v0.5
Expand Down Expand Up @@ -295,7 +295,7 @@ jobs:
uses: actions-rs/cargo@v1.0.3
with:
command: nextest
args: run --workspace
args: run --workspace --features unstable-reconnecting-rpc-client

- if: "failure()"
uses: "andymckay/cancel-action@a955d435292c0d409d104b57d8e78435a93a6ef1" # v0.5
Expand Down
70 changes: 52 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ console_error_panic_hook = "0.1.7"
darling = "0.20.10"
derive-where = "1.2.7"
either = { version = "1.13.0", default-features = false }
finito = { version = "0.1.0", default-features = false }
frame-metadata = { version = "16.0.0", default-features = false }
futures = { version = "0.3.30", default-features = false, features = ["std"] }
getrandom = { version = "0.2", default-features = false }
Expand Down
24 changes: 18 additions & 6 deletions subxt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,28 @@ default = ["jsonrpsee", "native"]
native = [
"jsonrpsee?/async-client",
"jsonrpsee?/client-ws-transport-tls",
"jsonrpsee?/ws-client",
"subxt-lightclient?/native",
"tokio-util",
"reconnecting-jsonrpsee-ws-client?/native",
"tokio?/sync",
]

# Enable this for web/wasm builds.
# Exactly 1 of "web" and "native" is expected.
web = [
"jsonrpsee?/async-wasm-client",
"jsonrpsee?/client-web-transport",
"jsonrpsee?/wasm-client",
"getrandom/js",
"subxt-lightclient?/web",
"subxt-macro/web",
"instant/wasm-bindgen",
"reconnecting-jsonrpsee-ws-client?/web",
"tokio?/sync",
"finito?/wasm-bindgen",
]

# Enable this to use the reconnecting rpc client
unstable-reconnecting-rpc-client = ["dep:reconnecting-jsonrpsee-ws-client"]
unstable-reconnecting-rpc-client = ["dep:finito", "dep:tokio", "jsonrpsee", "wasm-bindgen-futures"]

# Enable this to use jsonrpsee (allowing for example `OnlineClient::from_url`).
jsonrpsee = [
Expand Down Expand Up @@ -100,9 +103,6 @@ subxt-core = { workspace = true, features = ["std"] }
subxt-metadata = { workspace = true, features = ["std"] }
subxt-lightclient = { workspace = true, optional = true, default-features = false }

# Reconnecting jsonrpc ws client
reconnecting-jsonrpsee-ws-client = { version = "0.4.3", optional = true, default-features = false }

# For parsing urls to disallow insecure schemes
url = { workspace = true }

Expand All @@ -112,6 +112,13 @@ getrandom = { workspace = true, optional = true }
# Included if "native" feature is enabled
tokio-util = { workspace = true, features = ["compat"], optional = true }

# Included if the reconnecting rpc client feature is enabled
# Only the `tokio/sync` is used in the reconnecting rpc client
# and that compiles both for native and web.
tokio = { workspace = true, optional = true }
finito = { workspace = true, optional = true }
wasm-bindgen-futures = { workspace = true, optional = true }

[dev-dependencies]
bitvec = { workspace = true }
codec = { workspace = true, features = ["derive", "bit-vec"] }
Expand All @@ -127,6 +134,11 @@ subxt-signer = { path = "../signer", features = ["unstable-eth"] }
# the light-client wlll emit INFO logs with
# `GrandPa warp sync finished` and `Finalized block runtime ready.`
tracing-subscriber = { workspace = true }
# These deps are needed to test the reconnecting rpc client
jsonrpsee = { workspace = true, features = ["server"] }
tower = "0.4"
hyper = "1"
http-body = "1"

[[example]]
name = "light_client_basic"
Expand Down
22 changes: 2 additions & 20 deletions subxt/examples/setup_reconnecting_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(())
}
Loading

0 comments on commit 4bc27d4

Please sign in to comment.