Skip to content

Commit 7097f3b

Browse files
authored
Add rate limit config to sui StateSync service (#7613)
1 parent 27bee57 commit 7097f3b

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

crates/sui-config/src/p2p.rs

+24
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,30 @@ pub struct StateSyncConfig {
9292
/// If unspecified, this will default to `100`.
9393
#[serde(skip_serializing_if = "Option::is_none")]
9494
pub transaction_download_concurrency: Option<usize>,
95+
96+
/// Per-peer rate-limit (in requests/sec) for the PushCheckpointSummary RPC.
97+
///
98+
/// If unspecified, this will default to no limit.
99+
#[serde(skip_serializing_if = "Option::is_none")]
100+
pub push_checkpoint_summary_rate_limit: Option<NonZeroU32>,
101+
102+
/// Per-peer rate-limit (in requests/sec) for the GetCheckpointSummary RPC.
103+
///
104+
/// If unspecified, this will default to no limit.
105+
#[serde(skip_serializing_if = "Option::is_none")]
106+
pub get_checkpoint_summary_rate_limit: Option<NonZeroU32>,
107+
108+
/// Per-peer rate-limit (in requests/sec) for the GetCheckpointContents RPC.
109+
///
110+
/// If unspecified, this will default to no limit.
111+
#[serde(skip_serializing_if = "Option::is_none")]
112+
pub get_checkpoint_contents_rate_limit: Option<NonZeroU32>,
113+
114+
/// Per-peer rate-limit (in requests/sec) for the GetTransactionAndEffects RPC.
115+
///
116+
/// If unspecified, this will default to no limit.
117+
#[serde(skip_serializing_if = "Option::is_none")]
118+
pub get_transaction_and_effects_rate_limit: Option<NonZeroU32>,
95119
}
96120

97121
impl StateSyncConfig {

crates/sui-network/src/state_sync/builder.rs

+41-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use anemo::codegen::InboundRequestLayer;
5+
use anemo_tower::rate_limit;
46
use std::{
57
collections::HashMap,
68
sync::{Arc, RwLock},
@@ -62,8 +64,45 @@ where
6264
<S as ReadStore>::Error: std::error::Error,
6365
{
6466
pub fn build(self) -> (UnstartedStateSync<S>, StateSyncServer<impl StateSync>) {
67+
let state_sync_config = self.config.clone().unwrap_or_default();
6568
let (builder, server) = self.build_internal();
66-
(builder, StateSyncServer::new(server))
69+
let mut state_sync_server = StateSyncServer::new(server);
70+
71+
// Apply rate limits from configuration as needed.
72+
if let Some(limit) = state_sync_config.push_checkpoint_summary_rate_limit {
73+
state_sync_server = state_sync_server.add_layer_for_push_checkpoint_summary(
74+
InboundRequestLayer::new(rate_limit::RateLimitLayer::new(
75+
governor::Quota::per_second(limit),
76+
rate_limit::WaitMode::Block,
77+
)),
78+
);
79+
}
80+
if let Some(limit) = state_sync_config.get_checkpoint_summary_rate_limit {
81+
state_sync_server = state_sync_server.add_layer_for_get_checkpoint_summary(
82+
InboundRequestLayer::new(rate_limit::RateLimitLayer::new(
83+
governor::Quota::per_second(limit),
84+
rate_limit::WaitMode::Block,
85+
)),
86+
);
87+
}
88+
if let Some(limit) = state_sync_config.get_checkpoint_contents_rate_limit {
89+
state_sync_server = state_sync_server.add_layer_for_get_checkpoint_contents(
90+
InboundRequestLayer::new(rate_limit::RateLimitLayer::new(
91+
governor::Quota::per_second(limit),
92+
rate_limit::WaitMode::Block,
93+
)),
94+
);
95+
}
96+
if let Some(limit) = state_sync_config.get_transaction_and_effects_rate_limit {
97+
state_sync_server = state_sync_server.add_layer_for_get_transaction_and_effects(
98+
InboundRequestLayer::new(rate_limit::RateLimitLayer::new(
99+
governor::Quota::per_second(limit),
100+
rate_limit::WaitMode::Block,
101+
)),
102+
);
103+
}
104+
105+
(builder, state_sync_server)
67106
}
68107

69108
pub(super) fn build_internal(self) -> (UnstartedStateSync<S>, Server<S>) {
@@ -77,7 +116,7 @@ where
77116
let metrics = metrics.unwrap_or_else(Metrics::disabled);
78117

79118
let (sender, mailbox) = mpsc::channel(config.mailbox_capacity());
80-
let (checkpoint_event_sender, _reciever) =
119+
let (checkpoint_event_sender, _receiver) =
81120
broadcast::channel(config.synced_checkpoint_broadcast_channel_capacity());
82121
let weak_sender = sender.downgrade();
83122
let handle = Handle {

0 commit comments

Comments
 (0)