Skip to content

Commit be0d13c

Browse files
authored
[json rpc server] add timeout (#22095)
## Description add timeouts to JSON RPC server --- ## Release notes Check each box that your changes affect. If none of the boxes relate to your changes, release notes aren't required. For each box you select, include information after the relevant heading that describes the impact of your changes that a user might notice and any actions they must take to implement updates. - [ ] Protocol: - [ ] Nodes (Validators and Full nodes): - [ ] gRPC: - [ ] JSON-RPC: - [ ] GraphQL: - [ ] CLI: - [ ] Rust SDK:
1 parent 3c6f861 commit be0d13c

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

crates/sui-json-rpc/src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Copyright (c) Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::net::SocketAddr;
5-
use std::sync::Arc;
6-
74
use axum::body::Body;
85
use axum::http;
96
use hyper::Request;
107
use jsonrpsee::RpcModule;
118
use metrics::Metrics;
129
use metrics::MetricsLayer;
1310
use prometheus::Registry;
11+
use std::net::SocketAddr;
12+
use std::sync::Arc;
13+
use std::time::Duration;
1414
use sui_core::traffic_controller::metrics::TrafficControllerMetrics;
1515
use sui_core::traffic_controller::TrafficController;
1616
use sui_types::traffic_control::PolicyConfig;
@@ -163,7 +163,13 @@ impl JsonRpcServerBuilder {
163163
let (stop_handle, server_handle) = jsonrpsee::server::stop_channel();
164164
std::mem::forget(server_handle);
165165

166+
let timeout = std::env::var("JSON_RPC_TIMEOUT")
167+
.ok()
168+
.and_then(|value| value.parse::<u64>().ok())
169+
.unwrap_or(60);
170+
166171
let rpc_middleware = jsonrpsee::server::middleware::rpc::RpcServiceBuilder::new()
172+
.layer_fn(move |s| TimeoutLayer::new(s, Duration::from_secs(timeout)))
167173
.layer_fn(move |s| MetricsLayer::new(s, metrics.clone()))
168174
.layer_fn(move |s| TrafficControllerService::new(s, traffic_controller.clone()));
169175
let service_builder = jsonrpsee::server::ServerBuilder::new()
@@ -284,6 +290,7 @@ where
284290
fn rpc_doc_module() -> Module;
285291
}
286292

293+
use crate::metrics::TimeoutLayer;
287294
use jsonrpsee::core::BoxError;
288295

289296
#[derive(Clone)]

crates/sui-json-rpc/src/metrics.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// Copyright (c) Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::collections::HashSet;
5-
use std::sync::Arc;
6-
74
use futures::FutureExt;
85
use jsonrpsee::server::middleware::rpc::RpcServiceT;
6+
use jsonrpsee::types::{ErrorCode, ErrorObject, Id};
97
use prometheus::{
108
register_histogram_vec_with_registry, register_int_counter_vec_with_registry,
119
register_int_gauge_vec_with_registry, HistogramVec, IntCounterVec, IntGaugeVec,
1210
};
11+
use std::collections::HashSet;
12+
use std::sync::Arc;
13+
use std::time::Duration;
1314
use sui_json_rpc_api::TRANSIENT_ERROR_CODE;
1415
use sui_json_rpc_api::{CLIENT_SDK_TYPE_HEADER, CLIENT_TARGET_API_VERSION_HEADER};
1516
use tokio::time::Instant;
@@ -241,3 +242,34 @@ where
241242
.boxed()
242243
}
243244
}
245+
246+
#[derive(Clone)]
247+
pub struct TimeoutLayer<S>(S, Duration);
248+
249+
impl<S> TimeoutLayer<S> {
250+
pub fn new(service: S, duration: Duration) -> Self {
251+
Self(service, duration)
252+
}
253+
}
254+
255+
impl<'a, S> RpcServiceT<'a> for TimeoutLayer<S>
256+
where
257+
S: RpcServiceT<'a> + Send + Sync,
258+
S::Future: 'a,
259+
{
260+
type Future = futures::future::BoxFuture<'a, jsonrpsee::MethodResponse>;
261+
262+
fn call(&self, req: jsonrpsee::types::Request<'a>) -> Self::Future {
263+
let future = tokio::time::timeout(self.1, self.0.call(req));
264+
async move {
265+
match future.await {
266+
Ok(res) => res,
267+
Err(_) => jsonrpsee::MethodResponse::error(
268+
Id::Null,
269+
ErrorObject::from(ErrorCode::InternalError),
270+
),
271+
}
272+
}
273+
.boxed()
274+
}
275+
}

0 commit comments

Comments
 (0)