Skip to content

Commit 12c8b1c

Browse files
committed
feat: tracing permits
1 parent 5055753 commit 12c8b1c

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

crates/rpc/src/ctx/full.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use signet_node_types::Pnt;
1313
use signet_tx_cache::client::TxCache;
1414
use signet_types::constants::SignetSystemConstants;
1515
use std::sync::Arc;
16+
use tokio::sync::{AcquireError, OwnedSemaphorePermit, Semaphore};
1617
use trevm::helpers::Ctx;
1718

1819
/// State location when instantiating an EVM instance.
@@ -126,6 +127,12 @@ where
126127
}
127128
}
128129

130+
/// Shared context between all RPC handlers.
131+
#[derive(Debug)]
132+
struct SharedContext {
133+
tracing_semaphores: Arc<Semaphore>,
134+
}
135+
129136
/// Inner context for [`RpcCtx`].
130137
#[derive(Debug)]
131138
pub struct RpcCtxInner<Host, Signet>
@@ -135,6 +142,8 @@ where
135142
{
136143
host: Host,
137144
signet: SignetCtx<Signet>,
145+
146+
shared: SharedContext,
138147
}
139148

140149
impl<Host, Signet> RpcCtxInner<Host, Signet>
@@ -154,8 +163,18 @@ where
154163
where
155164
Tasks: TaskSpawner + Clone + 'static,
156165
{
157-
SignetCtx::new(constants, factory, eth_config, tx_cache, spawner)
158-
.map(|signet| Self { host, signet })
166+
SignetCtx::new(constants, factory, eth_config, tx_cache, spawner).map(|signet| Self {
167+
host,
168+
signet,
169+
shared: SharedContext {
170+
tracing_semaphores: Semaphore::new(eth_config.max_tracing_requests).into(),
171+
},
172+
})
173+
}
174+
175+
/// Acquire a permit for tracing.
176+
pub async fn acquire_tracing_permit(&self) -> Result<OwnedSemaphorePermit, AcquireError> {
177+
self.shared.tracing_semaphores.clone().acquire_owned().await
159178
}
160179

161180
pub const fn host(&self) -> &Host {

crates/rpc/src/debug/endpoints.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ where
3838
Host: FullNodeComponents,
3939
Signet: Pnt,
4040
{
41+
let _permit = response_tri!(
42+
ctx.acquire_tracing_permit()
43+
.await
44+
.map_err(|_| DebugError::rpc_error("Failed to acquire tracing permit".into()))
45+
);
46+
4147
let id = id.into();
4248
let span = tracing::debug_span!("traceBlock", ?id, tracer = ?opts.as_ref().and_then(|o| o.tracer.as_ref()));
4349

@@ -101,6 +107,12 @@ where
101107
Host: FullNodeComponents,
102108
Signet: Pnt,
103109
{
110+
let _permit = response_tri!(
111+
ctx.acquire_tracing_permit()
112+
.await
113+
.map_err(|_| DebugError::rpc_error("Failed to acquire tracing permit".into()))
114+
);
115+
104116
let span = tracing::debug_span!("traceTransaction", %tx_hash, tracer = ?opts.as_ref().and_then(|o| o.tracer.as_ref()));
105117

106118
let fut = async move {

0 commit comments

Comments
 (0)