Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
fb263ce
jsonrpsee v0.20.3
niklasad1 Aug 30, 2023
989ad97
RpcHandlers: fix panic in tokio::mpsc
niklasad1 Oct 30, 2023
310227c
fix host filtering
niklasad1 Oct 30, 2023
635e6f9
Update substrate/client/rpc-spec-v2/src/chain_head/chain_head.rs
niklasad1 Oct 31, 2023
97335eb
fix nits
niklasad1 Oct 31, 2023
49dcc5f
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.20
niklasad1 Oct 31, 2023
875319c
Update substrate/client/rpc-spec-v2/src/chain_head/chain_head.rs
niklasad1 Nov 1, 2023
98da3fc
cargo fmt
niklasad1 Nov 1, 2023
4be7c05
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.20
niklasad1 Nov 1, 2023
780cd6e
rpc rate limit PoC
niklasad1 Nov 2, 2023
395848c
add metrics support
niklasad1 Nov 3, 2023
eaaf70a
more work
niklasad1 Nov 6, 2023
f83d26a
cleanup
niklasad1 Nov 7, 2023
c520780
fix warnings
niklasad1 Nov 7, 2023
2f52aad
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-middle…
niklasad1 Nov 8, 2023
9fb1404
fix bad merge
niklasad1 Nov 8, 2023
6a128be
patch jsonrpsee_core as well
niklasad1 Nov 8, 2023
1730922
option layer for metrics
niklasad1 Nov 9, 2023
0f1c200
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-middle…
niklasad1 Nov 9, 2023
234f515
add CLI param --rpc-rate-limit
niklasad1 Nov 10, 2023
cb2e6bb
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-middle…
niklasad1 Nov 10, 2023
d66415f
fix nits
niklasad1 Nov 10, 2023
4fb4e78
fix test build
niklasad1 Nov 10, 2023
242a4cb
update jsonrpsee
niklasad1 Nov 13, 2023
d2895ba
update jsonrpsee again
niklasad1 Dec 5, 2023
79d7b6f
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-middle…
niklasad1 Dec 6, 2023
423018c
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-middle…
niklasad1 Dec 6, 2023
615e72a
update jsonrpsee again
niklasad1 Dec 8, 2023
46f7e23
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-middle…
niklasad1 Dec 8, 2023
ace52dc
fix nits
niklasad1 Dec 8, 2023
bd78593
fix more merge nits
niklasad1 Dec 8, 2023
c128075
fix cumulus
niklasad1 Dec 8, 2023
271307a
fix more nits
niklasad1 Dec 9, 2023
284da03
Merge branch 'master' into na-jsonrpsee-middleware-poc
niklasad1 Dec 11, 2023
15439db
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-middle…
niklasad1 Jan 24, 2024
3f7e94b
jsonrpsee v0.22
niklasad1 Feb 6, 2024
250f109
fix race in chainhead
niklasad1 Feb 6, 2024
8bc7fd7
fix some nits
niklasad1 Feb 6, 2024
b0769b7
fix bad Cargo.toml fmt
niklasad1 Feb 6, 2024
3d16db8
make test compile
niklasad1 Feb 6, 2024
a7032d9
fix tests
niklasad1 Feb 7, 2024
be5052a
jsonrpsee v0.22
niklasad1 Feb 7, 2024
6326b34
fix more nits
niklasad1 Feb 7, 2024
dc856ac
Merge remote-tracking branch 'origin/master' into na-bump-jsonrpsee-v…
niklasad1 Feb 7, 2024
6d088e1
Update cumulus/client/relay-chain-interface/Cargo.toml
niklasad1 Feb 7, 2024
b77a992
update Cargo.lock
niklasad1 Feb 7, 2024
4802cc4
fix rustdocs
niklasad1 Feb 7, 2024
485393f
remove needless box
niklasad1 Feb 8, 2024
bdc42f6
address grumbles
niklasad1 Feb 8, 2024
8bf3636
Update substrate/client/rpc-servers/src/lib.rs
niklasad1 Feb 8, 2024
3a78c54
clarify breaking ws pings
niklasad1 Feb 8, 2024
118480d
fix nits
niklasad1 Feb 8, 2024
104dc2d
Merge remote-tracking branch 'origin/master' into na-bump-jsonrpsee-v…
niklasad1 Feb 9, 2024
e310c9d
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-rate-l…
niklasad1 Feb 12, 2024
9407c78
Merge remote-tracking branch 'origin/na-bump-jsonrpsee-v0.22' into na…
niklasad1 Feb 12, 2024
0f77b36
move to governor crate for rate-limiting
niklasad1 Feb 12, 2024
776be60
add API to inject custom middleware
niklasad1 Feb 13, 2024
0380d9a
Merge branch 'master' into na-jsonrpsee-rate-limit-middleware
niklasad1 Feb 14, 2024
7958229
rewrite me
niklasad1 Feb 14, 2024
4d92963
cleanup
niklasad1 Feb 14, 2024
3a9231d
cargo fmt
niklasad1 Feb 14, 2024
9f60cd5
fix prdoc
niklasad1 Feb 14, 2024
d1daf61
fix build again
niklasad1 Feb 14, 2024
6085873
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-rate-l…
niklasad1 Feb 15, 2024
d2c6154
Update substrate/client/rpc-servers/src/middleware/mod.rs
niklasad1 Feb 15, 2024
7804d43
Update Cargo.toml
niklasad1 Feb 15, 2024
ca2d1f9
fix nits
niklasad1 Feb 15, 2024
e1c293e
more nits
niklasad1 Feb 15, 2024
2fccb5c
Update substrate/client/service/src/config.rs
niklasad1 Feb 15, 2024
de24325
Update prdoc/pr_3301.prdoc
niklasad1 Feb 15, 2024
7b5c885
cli: change to NonZeroU32
niklasad1 Feb 15, 2024
0fdc928
Merge remote-tracking branch 'origin/na-jsonrpsee-rate-limit-middlewa…
niklasad1 Feb 15, 2024
94a7ac9
cargo fmt
niklasad1 Feb 15, 2024
c2f0b6d
Update substrate/client/rpc-servers/src/middleware/rate_limit.rs
niklasad1 Feb 15, 2024
16273a4
address grumbles
niklasad1 Feb 15, 2024
d4841fd
Merge remote-tracking branch 'origin/na-jsonrpsee-rate-limit-middlewa…
niklasad1 Feb 15, 2024
970d5a4
address grumbles
niklasad1 Feb 15, 2024
c1c195a
grumbles: register metrics middleware first
niklasad1 Feb 15, 2024
4bc1b94
cargo fmt
niklasad1 Feb 15, 2024
2451e27
Merge branch 'master' into na-jsonrpsee-rate-limit-middleware
niklasad1 Feb 16, 2024
7fc7e11
Update substrate/client/cli/src/commands/run_cmd.rs
niklasad1 Feb 16, 2024
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
Prev Previous commit
Next Next commit
update jsonrpsee again
  • Loading branch information
niklasad1 committed Dec 5, 2023
commit d2895baae87d12987ac5fc5a2675f59f7939473d
19 changes: 9 additions & 10 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,6 @@ yamux = { opt-level = 3 }
zeroize = { opt-level = 3 }

[patch.crates-io]
jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee", branch = "na-jsonrpsee-service" }
jsonrpsee-core = { git = "https://github.com/paritytech/jsonrpsee", branch = "na-jsonrpsee-service" }
jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee", branch = "master" }
jsonrpsee-core = { git = "https://github.com/paritytech/jsonrpsee", branch = "master" }

1 change: 1 addition & 0 deletions substrate/client/rpc-servers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ tower = { version = "0.4.13", features = ["util"] }
http = "0.2.8"
hyper = "0.14.27"
futures = "0.3.29"
pin-project = "1.1.3"
7 changes: 5 additions & 2 deletions substrate/client/rpc-servers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,11 @@ pub async fn start_server<M: Send + Sync + 'static>(
.max_response_body_size(max_payload_out_mb.saturating_mul(MEGABYTE))
.max_connections(max_connections)
.max_subscriptions_per_connection(max_subs_per_conn)
.ping_interval(PingConfig::WithoutInactivityCheck(Duration::from_secs(30)))
.unwrap()
.enable_ws_ping(
PingConfig::new()
.ping_interval(Duration::from_secs(30))
.inactive_limit(Duration::from_secs(40)),
)
.set_http_middleware(http_middleware)
.set_message_buffer_capacity(message_buffer_capacity)
.custom_tokio_runtime(tokio_handle);
Expand Down
107 changes: 76 additions & 31 deletions substrate/client/rpc-servers/src/middleware/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use prometheus_endpoint::{
register, Counter, CounterVec, HistogramOpts, HistogramVec, Opts, PrometheusError, Registry,
U64,
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
time::Instant,
};

use jsonrpsee::{
core::async_trait, server::middleware::rpc::RpcServiceT, types::Request, MethodResponse,
};
use std::time::Instant;
use pin_project::pin_project;
use prometheus_endpoint::{
register, Counter, CounterVec, HistogramOpts, HistogramVec, Opts, PrometheusError, Registry,
U64,
};

/// Histogram time buckets in microseconds.
const HISTOGRAM_BUCKETS: [f64; 11] = [
Expand Down Expand Up @@ -172,7 +178,9 @@ impl<'a, S> RpcServiceT<'a> for Metrics<S>
where
S: Send + Sync + RpcServiceT<'a>,
{
async fn call(&self, req: Request<'a>) -> MethodResponse {
type Future = ResponseFuture<'a, S::Future>;

fn call(&self, req: Request<'a>) -> Self::Future {
let now = Instant::now();

log::trace!(
Expand All @@ -187,33 +195,70 @@ where
.with_label_values(&[self.transport_label, req.method_name()])
.inc();

let rp = self.service.call(req.clone()).await;
ResponseFuture {
fut: self.service.call(req.clone()),
metrics: self.metrics.clone(),
req,
now,
transport_label: self.transport_label,
}
}
}

log::trace!(target: "rpc_metrics", "[{}] on_response started_at={:?}", self.transport_label, now);
log::trace!(target: "rpc_metrics::extra", "[{}] result={:?}", self.transport_label, rp);
/// Response future for metrics.
#[pin_project]
pub struct ResponseFuture<'a, F> {
#[pin]
fut: F,
metrics: RpcMetrics,
req: Request<'a>,
now: Instant,
transport_label: &'static str,
}

let micros = now.elapsed().as_micros();
log::debug!(
target: "rpc_metrics",
"[{}] {} call took {} μs",
self.transport_label,
req.method_name(),
micros,
);
self.metrics
.calls_time
.with_label_values(&[self.transport_label, req.method_name()])
.observe(micros as _);
self.metrics
.calls_finished
.with_label_values(&[
self.transport_label,
req.method_name(),
// the label "is_error", so `success` should be regarded as false
// and vice-versa to be registrered correctly.
if rp.success_or_error.is_success() { "false" } else { "true" },
])
.inc();
rp
impl<'a, F> std::fmt::Debug for ResponseFuture<'a, F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("ResponseFuture")
}
}

impl<'a, F: Future<Output = MethodResponse>> Future for ResponseFuture<'a, F> {
type Output = F::Output;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();

let res = this.fut.poll(cx);
if let Poll::Ready(rp) = &res {
let method_name = this.req.method_name();
let transport_label = &this.transport_label;
let now = this.now;
let metrics = &this.metrics;

log::trace!(target: "rpc_metrics", "[{transport_label}] on_response started_at={:?}", now);
log::trace!(target: "rpc_metrics::extra", "[{transport_label}] result={:?}", rp);

let micros = now.elapsed().as_micros();
log::debug!(
target: "rpc_metrics",
"[{transport_label}] {method_name} call took {} μs",
micros,
);
metrics
.calls_time
.with_label_values(&[transport_label, method_name])
.observe(micros as _);
metrics
.calls_finished
.with_label_values(&[
transport_label,
method_name,
// the label "is_error", so `success` should be regarded as false
// and vice-versa to be registrered correctly.
if rp.success_or_error.is_success() { "false" } else { "true" },
])
.inc();
}
res
}
}
22 changes: 10 additions & 12 deletions substrate/client/rpc-servers/src/middleware/rate_limit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use jsonrpsee::{
core::async_trait,
server::middleware::rpc::RpcServiceT,
server::middleware::rpc::{ResponseFuture, RpcServiceT},
types::{ErrorObject, Request},
MethodResponse,
};
Expand Down Expand Up @@ -51,13 +51,7 @@ enum State {
Allow { until: Instant, rem: u64 },
}

/// Depending on how the rate limit is instantiated
/// it's possible to select whether the rate limit
/// is be applied per connection or shared by
/// all connections.
///
/// Have a look at `async fn run_server` below which
/// shows how do it.
/// Rate limit middleware.
#[derive(Clone)]
pub struct RateLimit<S> {
service: S,
Expand Down Expand Up @@ -87,7 +81,9 @@ impl<'a, S> RpcServiceT<'a> for RateLimit<S>
where
S: Send + Sync + RpcServiceT<'a>,
{
async fn call(&self, req: Request<'a>) -> MethodResponse {
type Future = ResponseFuture<S::Future>;

fn call(&self, req: Request<'a>) -> Self::Future {
let now = Instant::now();

let is_denied = {
Expand Down Expand Up @@ -117,16 +113,18 @@ where
};

if is_denied {
MethodResponse::error(
let rp = MethodResponse::error(
req.id,
ErrorObject::owned(
-32000,
"RPC rate limit",
Some(format!("{} calls/min is allowed", self.rate.num)),
),
)
);

ResponseFuture::ready(rp)
} else {
self.service.call(req).await
ResponseFuture::future(self.service.call(req))
}
}
}