Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 15 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean up tokio, perhaps we can replace it with futures channels and stuff

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll prob want a tokio feature flag or similar anyway so that we can make it easy to start up the UnstableBackend etc!

I see web enabled tokio?/sync; would webandtokio/sync` work together though?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, tokio/sync works for wasm/web but that could easily be replaced by futures channel

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot that I used tokio::sync::Notify so I prefer to keep tokio but not runtime agnostic but it's doesn't matter anyway because jsonrpsee is already using tokio channels.

We could change that though if becomes an issue.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup I don't mind that it uses tokio since jsonrpsee does anyway, so we don't really gain anything by trying to remove it anyways from this I guess!

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
Loading