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

Resource Limiting #499

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions examples/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
use jsonrpsee::{
http_client::HttpClientBuilder,
http_server::{HttpServerBuilder, RpcModule},
types::{traits::Client, JsonValue},
rpc_params,
types::traits::Client,
};
use std::net::SocketAddr;

Expand All @@ -38,10 +39,9 @@ async fn main() -> anyhow::Result<()> {
let server_addr = run_server().await?;
let url = format!("http://{}", server_addr);

let params: &[JsonValue] = &[1_u64.into(), 2.into(), 3.into()];

let client = HttpClientBuilder::default().build(url)?;
let response: Result<String, _> = client.request("say_hello", params.into()).await;
let params = rpc_params!(1_u64, 2, 3);
let response: Result<String, _> = client.request("say_hello", params).await;
println!("r: {:?}", response);

Ok(())
Expand Down
5 changes: 3 additions & 2 deletions examples/ws_sub_with_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// DEALINGS IN THE SOFTWARE.

use jsonrpsee::{
rpc_params,
types::{traits::SubscriptionClient, v2::ParamsSer},
ws_client::WsClientBuilder,
ws_server::{RpcModule, WsServerBuilder},
Expand All @@ -40,8 +41,8 @@ async fn main() -> anyhow::Result<()> {
let client = WsClientBuilder::default().build(&url).await?;

// Subscription with a single parameter
let params = ParamsSer::Array(vec![3.into()]);
let mut sub_params_one = client.subscribe::<Option<char>>("sub_one_param", params, "unsub_one_param").await?;
let mut sub_params_one =
client.subscribe::<Option<char>>("sub_one_param", rpc_params!(3), "unsub_one_param").await?;
println!("subscription with one param: {:?}", sub_params_one.next().await);

// Subscription with multiple parameters
Expand Down
10 changes: 5 additions & 5 deletions examples/ws_subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@
// DEALINGS IN THE SOFTWARE.

use jsonrpsee::{
types::{traits::SubscriptionClient, v2::ParamsSer, Error, Subscription},
rpc_params,
types::{traits::SubscriptionClient, Error, Subscription},
ws_client::WsClientBuilder,
ws_server::{RpcModule, WsServerBuilder},
};
use std::net::SocketAddr;

const NUM_SUBSCRIPTION_RESPONSES: usize = 10;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
const NUM_SUBSCRIPTION_RESPONSES: usize = 5;
env_logger::init();
let addr = run_server().await?;
let url = format!("ws://{}", addr);

let client = WsClientBuilder::default().build(&url).await?;
let mut subscribe_hello: Subscription<String> =
client.subscribe("subscribe_hello", ParamsSer::NoParams, "unsubscribe_hello").await?;
client.subscribe("subscribe_hello", rpc_params!(), "unsubscribe_hello").await?;

let mut i = 0;
while i <= NUM_SUBSCRIPTION_RESPONSES {
let r = subscribe_hello.next().await;
log::debug!("received {:?}", r);
println!("received {:?}", r);
i += 1;
}

Expand Down
29 changes: 18 additions & 11 deletions jsonrpsee/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ homepage = "https://github.com/paritytech/jsonrpsee"
documentation = "https://docs.rs/jsonrpsee"

[dependencies]
http-client = { path = "../http-client", version = "0.3.0", package = "jsonrpsee-http-client", optional = true }
http-server = { path = "../http-server", version = "0.3.0", package = "jsonrpsee-http-server", optional = true }
ws-client = { path = "../ws-client", version = "0.3.0", package = "jsonrpsee-ws-client", optional = true }
ws-server = { path = "../ws-server", version = "0.3.0", package = "jsonrpsee-ws-server", optional = true }
proc-macros = { path = "../proc-macros", version = "0.3.0", package = "jsonrpsee-proc-macros", optional = true }
utils = { path = "../utils", version = "0.3.0", package = "jsonrpsee-utils", optional = true }
types = { path = "../types", version = "0.3.0", package = "jsonrpsee-types", optional = true }
# No support for namespaced features yet so workspace dependencies are prefixed with `jsonrpsee-`.
# See https://github.com/rust-lang/cargo/issues/5565 for more details.
jsonrpsee-http-client = { path = "../http-client", version = "0.3.0", package = "jsonrpsee-http-client", optional = true }
jsonrpsee-http-server = { path = "../http-server", version = "0.3.0", package = "jsonrpsee-http-server", optional = true }
jsonrpsee-ws-client = { path = "../ws-client", version = "0.3.0", package = "jsonrpsee-ws-client", optional = true }
jsonrpsee-ws-server = { path = "../ws-server", version = "0.3.0", package = "jsonrpsee-ws-server", optional = true }
jsonrpsee-proc-macros = { path = "../proc-macros", version = "0.3.0", package = "jsonrpsee-proc-macros", optional = true }
jsonrpsee-utils = { path = "../utils", version = "0.3.0", package = "jsonrpsee-utils", optional = true }
jsonrpsee-types = { path = "../types", version = "0.3.0", package = "jsonrpsee-types", optional = true }

[features]
client = ["http-client", "ws-client", "types"]
server = ["http-server", "ws-server", "utils", "types"]
macros = ["proc-macros", "types"]
full = ["client", "server", "macros", "utils"]
http-client = ["jsonrpsee-http-client", "jsonrpsee-types", "jsonrpsee-utils/client"]
http-server = ["jsonrpsee-http-server", "jsonrpsee-types", "jsonrpsee-utils"]
ws-client = ["jsonrpsee-ws-client", "jsonrpsee-types", "jsonrpsee-utils/client"]
ws-server = ["jsonrpsee-ws-server", "jsonrpsee-types", "jsonrpsee-utils"]
macros = ["jsonrpsee-proc-macros", "jsonrpsee-types"]

client = ["http-client", "ws-client"]
server = ["http-server", "ws-server"]
full = ["client", "server", "macros"]
74 changes: 45 additions & 29 deletions jsonrpsee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,54 @@
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

//! jsonrpsee wrapper crate.
//! Jsonrpsee wrapper crate.
//!
//! <br>
//!
//! # Optional features
//!
//! The `jsonrpsee` crate composes JSON-RPC functionality behind optional feature
//! flags to provide for client and server communication over specific protocols.
//! There are no default features, all functionality must be opted in to accordingly.
//! The following features are avaliable.
//!
//! - **`http-client`** - JSON-RPC client functionality over HTTP protocol.
//! - **`http-server`** - JSON-RPC server functionality over HTTP protocol.
//! - **`ws-client`** - JSON-RPC client functionality over WebSocket protocol.
//! - **`ws-server`** - JSON-RPC server functionality over WebSocket protocol.
//! - **`macros`** - JSON-RPC API generation convenience by derive macros.
//! - **`client`** - Enables `http-client` and `ws-client` features.
//! - **`server`** - Enables `http-server` and `ws-server` features.
//! - **`full`** - Enables `client`, `server` and `macros` features.

/// JSON RPC HTTP client.
#[cfg(feature = "client")]
pub use http_client;
/// JSON-RPC HTTP client.
#[cfg(feature = "jsonrpsee-http-client")]
pub use jsonrpsee_http_client as http_client;

/// JSON RPC WebSocket client.
#[cfg(feature = "client")]
pub use ws_client;
/// JSON-RPC WebSocket client.
#[cfg(feature = "jsonrpsee-ws-client")]
pub use jsonrpsee_ws_client as ws_client;

/// JSON RPC HTTP server.
#[cfg(feature = "server")]
pub use http_server;
/// JSON-RPC client convenience macro to build params.
#[cfg(any(feature = "http-client", feature = "ws-client"))]
pub use jsonrpsee_utils::rpc_params;

/// JSON RPC WebSocket server.
#[cfg(feature = "server")]
pub use ws_server;
/// JSON-RPC HTTP server.
#[cfg(feature = "jsonrpsee-http-server")]
pub use jsonrpsee_http_server as http_server;

/// JSON-RPC WebSocket server.
#[cfg(feature = "jsonrpsee-ws-server")]
pub use jsonrpsee_ws_server as ws_server;

/// Procedural macros for JSON-RPC implementations.
#[cfg(feature = "jsonrpsee-proc-macros")]
pub use jsonrpsee_proc_macros as proc_macros;

/// Common types used to implement JSON-RPC server and client.
#[cfg(feature = "jsonrpsee-types")]
pub use jsonrpsee_types as types;

/// Set of RPC methods that can be mounted to the server.
#[cfg(feature = "server")]
pub use utils::server::rpc_module::{RpcModule, SubscriptionSink};

/// Procedural macros for JSON RPC implementations.
#[cfg(feature = "macros")]
pub use proc_macros;

/// Common types used to implement JSON RPC server and client.
#[cfg(any(feature = "types", feature = "macros"))]
pub mod types {
pub use ::types::*;

/// Set of RPC methods that can be mounted to the server.
#[cfg(feature = "server")]
pub use utils::server::rpc_module::{RpcModule, SubscriptionSink};
}
#[cfg(any(feature = "http-server", feature = "ws-server"))]
pub use jsonrpsee_utils::server::rpc_module::{RpcModule, SubscriptionSink};
1 change: 1 addition & 0 deletions utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ server = [
"parking_lot",
"rand",
]
client = ["jsonrpsee-types"]

[dev-dependencies]
serde_json = "1.0"
Expand Down
49 changes: 49 additions & 0 deletions utils/src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

//! Shared utilities for `jsonrpsee` clients.

#[doc(hidden)]
pub mod __reexports {
pub use jsonrpsee_types::{to_json_value, v2::ParamsSer};
}

#[macro_export]
/// Convert the given values to a [`ParamsSer`] as expected by a jsonrpsee Client (http or websocket).
macro_rules! rpc_params {
($($param:expr),*) => {
{
let mut __params = vec![];
$(
__params.push($crate::client::__reexports::to_json_value($param).expect("json serialization is infallible; qed."));
)*
$crate::client::__reexports::ParamsSer::Array(__params)
}
};
() => {
$crate::client::__reexports::ParamsSer::NoParams,
}
}
4 changes: 4 additions & 0 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ pub mod http_helpers;
/// Shared code for JSON-RPC servers.
#[cfg(feature = "server")]
pub mod server;

/// Shared code for JSON-RPC clients.
#[cfg(feature = "client")]
pub mod client;