Refine Rust architecture boundaries
This commit is contained in:
@@ -31,6 +31,48 @@ pub struct RuntimeExecutor {
|
||||
metering_sink: SharedMeteringSink,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct RuntimeExecutionRequest<'a> {
|
||||
pub operation: &'a RuntimeOperation,
|
||||
pub input: &'a Value,
|
||||
pub resolved_auth: Option<&'a ResolvedAuth>,
|
||||
pub request_context: Option<&'a RuntimeRequestContext>,
|
||||
}
|
||||
|
||||
impl<'a> RuntimeExecutionRequest<'a> {
|
||||
pub fn new(operation: &'a RuntimeOperation, input: &'a Value) -> Self {
|
||||
Self {
|
||||
operation,
|
||||
input,
|
||||
resolved_auth: None,
|
||||
request_context: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_auth(mut self, resolved_auth: &'a ResolvedAuth) -> Self {
|
||||
self.resolved_auth = Some(resolved_auth);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_optional_auth(mut self, resolved_auth: Option<&'a ResolvedAuth>) -> Self {
|
||||
self.resolved_auth = resolved_auth;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_context(mut self, request_context: &'a RuntimeRequestContext) -> Self {
|
||||
self.request_context = Some(request_context);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_optional_context(
|
||||
mut self,
|
||||
request_context: Option<&'a RuntimeRequestContext>,
|
||||
) -> Self {
|
||||
self.request_context = request_context;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RuntimeExecutor {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
@@ -86,7 +128,7 @@ impl RuntimeExecutor {
|
||||
operation: &RuntimeOperation,
|
||||
input: &Value,
|
||||
) -> Result<Value, RuntimeError> {
|
||||
self.execute_with_auth_and_context(operation, input, None, None)
|
||||
self.execute_request(RuntimeExecutionRequest::new(operation, input))
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -96,8 +138,10 @@ impl RuntimeExecutor {
|
||||
input: &Value,
|
||||
resolved_auth: Option<&ResolvedAuth>,
|
||||
) -> Result<Value, RuntimeError> {
|
||||
self.execute_with_auth_and_context(operation, input, resolved_auth, None)
|
||||
.await
|
||||
self.execute_request(
|
||||
RuntimeExecutionRequest::new(operation, input).with_optional_auth(resolved_auth),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn execute_with_context(
|
||||
@@ -106,8 +150,10 @@ impl RuntimeExecutor {
|
||||
input: &Value,
|
||||
request_context: Option<&RuntimeRequestContext>,
|
||||
) -> Result<Value, RuntimeError> {
|
||||
self.execute_with_auth_and_context(operation, input, None, request_context)
|
||||
.await
|
||||
self.execute_request(
|
||||
RuntimeExecutionRequest::new(operation, input).with_optional_context(request_context),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn execute_with_auth_and_context(
|
||||
@@ -117,16 +163,38 @@ impl RuntimeExecutor {
|
||||
resolved_auth: Option<&ResolvedAuth>,
|
||||
request_context: Option<&RuntimeRequestContext>,
|
||||
) -> Result<Value, RuntimeError> {
|
||||
log_runtime_event("unary.execute", operation, request_context);
|
||||
let _permit = self.acquire_unary_permit(operation)?;
|
||||
self.execute_request(
|
||||
RuntimeExecutionRequest::new(operation, input)
|
||||
.with_optional_auth(resolved_auth)
|
||||
.with_optional_context(request_context),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn execute_request(
|
||||
&self,
|
||||
request: RuntimeExecutionRequest<'_>,
|
||||
) -> Result<Value, RuntimeError> {
|
||||
log_runtime_event("unary.execute", request.operation, request.request_context);
|
||||
let _permit = self.acquire_unary_permit(request.operation)?;
|
||||
let started_at = Instant::now();
|
||||
let prepared_request = self.prepare_request(operation, input)?;
|
||||
let prepared_request = apply_resolved_auth(prepared_request, resolved_auth);
|
||||
let prepared_request = self.prepare_request(request.operation, request.input)?;
|
||||
let prepared_request = apply_resolved_auth(prepared_request, request.resolved_auth);
|
||||
let result = self
|
||||
.execute_prepared(operation, input, prepared_request, request_context)
|
||||
.await;
|
||||
self.record_metering(operation, request_context, &result, started_at)
|
||||
.execute_prepared(
|
||||
request.operation,
|
||||
request.input,
|
||||
prepared_request,
|
||||
request.request_context,
|
||||
)
|
||||
.await;
|
||||
self.record_metering(
|
||||
request.operation,
|
||||
request.request_context,
|
||||
&result,
|
||||
started_at,
|
||||
)
|
||||
.await;
|
||||
result
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ pub use cache_factory::{
|
||||
BuiltinCacheBackendFactory, CacheBackendFactory, SharedCacheBackendFactory,
|
||||
};
|
||||
pub use error::RuntimeError;
|
||||
pub use executor::RuntimeExecutor;
|
||||
pub use executor::{RuntimeExecutionRequest, RuntimeExecutor};
|
||||
pub use executor_builder::{RuntimeExecutorBuilder, community_default};
|
||||
pub use limits::{RuntimeLimits, RuntimeLimitsConfigError};
|
||||
pub use model::{AdapterResponse, PreparedRequest, RuntimeOperation};
|
||||
|
||||
Reference in New Issue
Block a user