Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(rpc): split server startup into dedicated modules

Steven Enamakel08fcbaf3ae70
diff --git a/crates/openhuman-rpc/src/server/http/rpc_handler.rs b/crates/openhuman-rpc/src/server/http/rpc_handler.rs
index 4d5d731c2b..967686be86 100644
--- a/crates/openhuman-rpc/src/server/http/rpc_handler.rs
+++ b/crates/openhuman-rpc/src/server/http/rpc_handler.rs
@@ -1,11 +1,13 @@
//! `POST /rpc`: the JSON-RPC 2.0 endpoint.
use axum::extract::State;
+use axum::Extension;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use crate::core_host::core::invoke::invoke_method;
+use crate::core_host::core::runtime::DispatchCtx;
use crate::core_host::core::types::AppState;
use crate::core_host::core::StructuredRpcError;
@@ -26,12 +28,20 @@ use crate::{RpcError, RpcFailure, RpcRequest, RpcSuccess, JSONRPC_VERSION, SERVE
/// # Arguments
///
/// * `state` - The application state, injected by Axum.
+/// * `ctx` - The [`DispatchCtx`] the serving middleware attached to the request:
+/// the operator's context, or the context of the SaaS user the gateway
+/// admitted. A request that carries none dispatches unbound.
/// * `req` - The parsed [`RpcRequest`].
-pub async fn rpc_handler(State(state): State<AppState>, Json(req): Json<RpcRequest>) -> Response {
+pub async fn rpc_handler(
+ State(state): State<AppState>,
+ ctx: Option<Extension<DispatchCtx>>,
+ Json(req): Json<RpcRequest>,
+) -> Response {
+ let ctx = ctx.map_or_else(DispatchCtx::unbound, |Extension(ctx)| ctx);
let id = req.id.clone();
let method = req.method.clone();
let started = std::time::Instant::now();
- let result = invoke_method(state, method.as_str(), req.params).await;
+ let result = invoke_method(ctx, state, method.as_str(), req.params).await;
let ms = started.elapsed().as_millis();
match result {
diff --git a/crates/openhuman-rpc/src/server/saas_gateway.rs b/crates/openhuman-rpc/src/server/saas_gateway.rs
index c2df474da9..2628936086 100644
--- a/crates/openhuman-rpc/src/server/saas_gateway.rs
+++ b/crates/openhuman-rpc/src/server/saas_gateway.rs
@@ -127,6 +127,14 @@ fn bearer(req: &Request) -> Option<&str> {
.map(str::trim)
}
+/// Runs the request under `ctx`: attaches the [`DispatchCtx`] that
+/// `rpc_handler` hands to the dispatcher, and (until every reader has moved off
+/// the ambient context, #7801) scopes the same context onto the request task.
+pub(crate) async fn dispatch_in(ctx: Arc<CoreContext>, mut req: Request, next: Next) -> Response {
+ req.extensions_mut().insert(DispatchCtx::new(Arc::clone(&ctx)));
+ CoreContext::scope(ctx, next.run(req)).await
+}
+
/// The SaaS request layer; `operator` is the runtime's own context.
pub(crate) async fn saas_gateway(operator: Arc<CoreContext>, req: Request, next: Next) -> Response {
let now = SystemTime::now()
@@ -136,7 +144,7 @@ pub(crate) async fn saas_gateway(operator: Arc<CoreContext>, req: Request, next:
let secret = crate::core_host::core::auth::get_rpc_token();
let (user, signature) = match decide(&req, secret) {
Err(refused) => return refused.into_response(),
- Ok(Admitted::Operator) => return CoreContext::scope(operator, next.run(req)).await,
+ Ok(Admitted::Operator) => return dispatch_in(operator, req, next).await,
Ok(Admitted::User { user, signature }) => (user, signature),
};
let secret = secret.expect("decide admits a user only once the core has a token");
@@ -146,11 +154,11 @@ pub(crate) async fn saas_gateway(operator: Arc<CoreContext>, req: Request, next:
let ctx = Arc::clone(profile.context());
// Holding the state for the request keeps the profile from being
// evicted under it.
- let response = CoreContext::scope(ctx, next.run(req)).await;
+ let response = dispatch_in(ctx, req, next).await;
drop(profile);
response
}
- Ok(GatewayScope::Operator) => CoreContext::scope(operator, next.run(req)).await,
+ Ok(GatewayScope::Operator) => dispatch_in(operator, req, next).await,
}
}
diff --git a/crates/openhuman-rpc/src/server/serve.rs b/crates/openhuman-rpc/src/server/serve.rs
index 604004a0cd..38a56bdc30 100644
--- a/crates/openhuman-rpc/src/server/serve.rs
+++ b/crates/openhuman-rpc/src/server/serve.rs
@@ -151,7 +151,7 @@ pub async fn serve(
move |req: axum::extract::Request, next: axum::middleware::Next| {
let ctx = Arc::clone(&ctx);
async move {
- crate::core_host::core::runtime::CoreContext::scope(ctx, next.run(req)).await
+ super::saas_gateway::dispatch_in(ctx, req, next).await
}
},
))
diff --git a/crates/openhuman-rpc/src/server/socketio.rs b/crates/openhuman-rpc/src/server/socketio.rs
index ced3cd6510..ff370fd28c 100644
--- a/crates/openhuman-rpc/src/server/socketio.rs
+++ b/crates/openhuman-rpc/src/server/socketio.rs
@@ -360,6 +360,9 @@ pub fn attach_socketio() -> (socketioxide::layer::SocketIoLayer, SocketIo) {
// Invoke the method through the same logic used by the HTTP RPC endpoint.
let response = match crate::core_host::core::invoke::invoke_method(
+ // A Socket.IO event is not scoped to a request, so it
+ // runs under the process default context.
+ crate::core_host::core::runtime::DispatchCtx::process_default(),
crate::core_host::core::invoke::default_state(),
payload.method.as_str(),
payload.params,