pub struct RequestContext { /* private fields */ }Expand description
Context provided to each RPC and subscription handler.
RequestContext provides access to connection metadata and three tiers of extensions:
- Server extensions: Shared across all connections
- Connection extensions: Scoped to a single connection
- Request extensions: Unique per request
Additionally, handlers can monitor for graceful shutdown notifications to cleanly exit loops, flush pending data, and release resources.
This allows handlers to access shared resources (like database pools), connection-specific state (like authentication sessions), and request-specific data (like trace IDs).
Implementations§
Source§impl RequestContext
impl RequestContext
Sourcepub fn new(
connection: Connection,
endpoint: Endpoint,
service: String,
resource: String,
server_extensions: Extensions,
connection_extensions: Extensions,
shutdown_signal: Arc<Notify>,
) -> Self
pub fn new( connection: Connection, endpoint: Endpoint, service: String, resource: String, server_extensions: Extensions, connection_extensions: Extensions, shutdown_signal: Arc<Notify>, ) -> Self
Create a new RequestContext.
This is typically called by the connection handler for each incoming request.
Sourcepub fn connection(&self) -> &Connection
pub fn connection(&self) -> &Connection
Get a reference to the underlying Iroh connection.
Sourcepub fn connection_type(&self) -> Option<impl Watcher + use<>>
pub fn connection_type(&self) -> Option<impl Watcher + use<>>
Get a watcher for the connection type (direct vs relay).
The returned watcher provides real-time monitoring of how this connection is routed:
Direct- Direct UDP connection to peerRelay- Routed through a relay serverMixed- Both paths availableNone- No path available
Use this for adaptive behavior, monitoring, and debugging connectivity issues.
§Example
use iroh::Watcher;
// Get current connection type
if let Some(mut watcher) = ctx.connection_type() {
let conn_type = watcher.get();
log::info!("Connection type: {:?}", conn_type);
}
// Monitor for changes
if let Some(watcher) = ctx.connection_type() {
use futures::StreamExt;
let mut stream = watcher.stream();
while let Some(conn_type) = stream.next().await {
log::info!("Connection type changed: {:?}", conn_type);
}
}Sourcepub fn server_extensions(&self) -> &Extensions
pub fn server_extensions(&self) -> &Extensions
Get server-level extensions (shared across all connections).
Use this to access resources like database pools, configuration, etc.
Sourcepub fn connection_extensions(&self) -> &Extensions
pub fn connection_extensions(&self) -> &Extensions
Get connection-level extensions (scoped to this connection).
Use this to access connection-specific state like authentication sessions.
Sourcepub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
Get request-level extensions (unique to this request).
Use this to access request-specific data like trace IDs, request timing, etc.
Sourcepub fn with_extension<T: Send + Sync + 'static>(self, value: T) -> Self
pub fn with_extension<T: Send + Sync + 'static>(self, value: T) -> Self
Add a request-level extension, returning a new RequestContext.
This is useful for adding request-specific data like trace IDs.
Sourcepub async fn shutdown_notified(&self)
pub async fn shutdown_notified(&self)
Wait for shutdown notification (async).
Use this in tokio::select! to react to graceful shutdown:
loop {
tokio::select! {
_ = interval.tick() => {
// Normal operation
}
_ = ctx.shutdown_notified() => {
log::info!("Shutdown detected, exiting cleanly");
break;
}
}
}Sourcepub fn connection_rtt(&self) -> Duration
pub fn connection_rtt(&self) -> Duration
Get the current round-trip time (RTT) estimate for this connection.
This provides the current best estimate of the connection’s latency. Useful for adaptive behavior, monitoring, and debugging.
§Example
let rtt = ctx.connection_rtt();
if rtt > Duration::from_millis(100) {
log::warn!("High latency detected: {:?}", rtt);
}Sourcepub fn connection_stats(&self) -> ConnectionStats
pub fn connection_stats(&self) -> ConnectionStats
Get detailed statistics for this connection.
Returns comprehensive metrics including:
- UDP statistics (packets sent/received, bytes transferred)
- Path statistics (congestion window, RTT variance)
- Frame statistics (sent/received frame counts by type)
Useful for monitoring, debugging, and performance analysis.
§Example
let stats = ctx.connection_stats();
log::info!(
"Connection stats - RTT: {:?}, sent: {} bytes, lost: {} packets",
stats.path.rtt,
stats.udp.datagrams_sent,
stats.path.lost_packets
);Trait Implementations§
Source§impl Clone for RequestContext
impl Clone for RequestContext
Source§fn clone(&self) -> RequestContext
fn clone(&self) -> RequestContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more