Polish community UI copy and cleanup
This commit is contained in:
@@ -6,7 +6,6 @@ use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
|
||||
use crank_core::{
|
||||
AdapterRegistry, CachedHeader, CachedResponse, ExecutionMode, HttpMethod, InvocationStatus,
|
||||
MeteringEvent, ResponseCacheStore, SharedMeteringSink, SharedProtocolAdapter, Target,
|
||||
TransportBehavior,
|
||||
};
|
||||
use serde_json::{Map, Value, json};
|
||||
use sha2::{Digest, Sha256};
|
||||
@@ -16,7 +15,7 @@ use tracing::debug;
|
||||
|
||||
use crate::{
|
||||
AdapterResponse, PreparedRequest, ResolvedAuth, RuntimeError, RuntimeLimits, RuntimeOperation,
|
||||
RuntimeRequestContext, WindowExecutionResult,
|
||||
RuntimeRequestContext,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -24,9 +23,6 @@ pub struct RuntimeExecutor {
|
||||
adapters: AdapterRegistry,
|
||||
limits: RuntimeLimits,
|
||||
unary_limiter: Arc<Semaphore>,
|
||||
window_limiter: Arc<Semaphore>,
|
||||
session_limiter: Arc<Semaphore>,
|
||||
job_limiter: Arc<Semaphore>,
|
||||
response_cache: Option<Arc<dyn ResponseCacheStore>>,
|
||||
metering_sink: SharedMeteringSink,
|
||||
}
|
||||
@@ -57,9 +53,6 @@ impl RuntimeExecutor {
|
||||
Self {
|
||||
adapters,
|
||||
unary_limiter: Arc::new(Semaphore::new(limits.max_concurrent_unary)),
|
||||
window_limiter: Arc::new(Semaphore::new(limits.max_concurrent_window)),
|
||||
session_limiter: Arc::new(Semaphore::new(limits.max_concurrent_sessions)),
|
||||
job_limiter: Arc::new(Semaphore::new(limits.max_concurrent_jobs)),
|
||||
limits,
|
||||
response_cache,
|
||||
metering_sink,
|
||||
@@ -123,156 +116,6 @@ impl RuntimeExecutor {
|
||||
result
|
||||
}
|
||||
|
||||
pub async fn execute_window(
|
||||
&self,
|
||||
operation: &RuntimeOperation,
|
||||
input: &Value,
|
||||
) -> Result<WindowExecutionResult, RuntimeError> {
|
||||
self.execute_window_with_auth_and_context(operation, input, None, None)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn execute_window_with_auth(
|
||||
&self,
|
||||
operation: &RuntimeOperation,
|
||||
input: &Value,
|
||||
resolved_auth: Option<&ResolvedAuth>,
|
||||
) -> Result<WindowExecutionResult, RuntimeError> {
|
||||
self.execute_window_with_auth_and_context(operation, input, resolved_auth, None)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn execute_window_with_context(
|
||||
&self,
|
||||
operation: &RuntimeOperation,
|
||||
input: &Value,
|
||||
request_context: Option<&RuntimeRequestContext>,
|
||||
) -> Result<WindowExecutionResult, RuntimeError> {
|
||||
self.execute_window_with_auth_and_context(operation, input, None, request_context)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn execute_window_with_auth_and_context(
|
||||
&self,
|
||||
operation: &RuntimeOperation,
|
||||
input: &Value,
|
||||
resolved_auth: Option<&ResolvedAuth>,
|
||||
request_context: Option<&RuntimeRequestContext>,
|
||||
) -> Result<WindowExecutionResult, RuntimeError> {
|
||||
log_runtime_event("window.execute", operation, request_context);
|
||||
let _permit = self.acquire_window_permit()?;
|
||||
let started_at = Instant::now();
|
||||
let Some(streaming) = operation.execution_config.streaming.as_ref() else {
|
||||
let result = Err(RuntimeError::MissingStreamingConfig {
|
||||
operation_id: operation.operation_id.as_str().to_owned(),
|
||||
});
|
||||
self.record_metering(operation, request_context, &result, started_at)
|
||||
.await;
|
||||
return result;
|
||||
};
|
||||
|
||||
if streaming.mode != ExecutionMode::Window {
|
||||
let result = Err(RuntimeError::UnsupportedExecutionMode {
|
||||
operation_id: operation.operation_id.as_str().to_owned(),
|
||||
mode: streaming.mode,
|
||||
});
|
||||
self.record_metering(operation, request_context, &result, started_at)
|
||||
.await;
|
||||
return result;
|
||||
}
|
||||
|
||||
let prepared_request = self.prepare_request(operation, input)?;
|
||||
let prepared_request = apply_resolved_auth(prepared_request, resolved_auth);
|
||||
let adapter_response = if matches!(
|
||||
streaming.transport_behavior,
|
||||
TransportBehavior::ServerStream
|
||||
) {
|
||||
self.execute_window_adapter(operation, prepared_request, request_context)
|
||||
.await?
|
||||
} else {
|
||||
self.execute_adapter(operation, prepared_request, request_context)
|
||||
.await?
|
||||
};
|
||||
|
||||
let result = crate::aggregation::collect_window_result(&adapter_response.body, streaming);
|
||||
self.record_metering(operation, request_context, &result, started_at)
|
||||
.await;
|
||||
result
|
||||
}
|
||||
|
||||
pub async fn execute_session_seed(
|
||||
&self,
|
||||
operation: &RuntimeOperation,
|
||||
input: &Value,
|
||||
) -> Result<WindowExecutionResult, RuntimeError> {
|
||||
self.execute_session_seed_with_auth_and_context(operation, input, None, None)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn execute_session_seed_with_auth(
|
||||
&self,
|
||||
operation: &RuntimeOperation,
|
||||
input: &Value,
|
||||
resolved_auth: Option<&ResolvedAuth>,
|
||||
) -> Result<WindowExecutionResult, RuntimeError> {
|
||||
self.execute_session_seed_with_auth_and_context(operation, input, resolved_auth, None)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn execute_session_seed_with_context(
|
||||
&self,
|
||||
operation: &RuntimeOperation,
|
||||
input: &Value,
|
||||
request_context: Option<&RuntimeRequestContext>,
|
||||
) -> Result<WindowExecutionResult, RuntimeError> {
|
||||
self.execute_session_seed_with_auth_and_context(operation, input, None, request_context)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn execute_session_seed_with_auth_and_context(
|
||||
&self,
|
||||
operation: &RuntimeOperation,
|
||||
input: &Value,
|
||||
resolved_auth: Option<&ResolvedAuth>,
|
||||
request_context: Option<&RuntimeRequestContext>,
|
||||
) -> Result<WindowExecutionResult, RuntimeError> {
|
||||
log_runtime_event("session.seed", operation, request_context);
|
||||
let _permit = self.acquire_session_permit()?;
|
||||
let Some(streaming) = operation.execution_config.streaming.as_ref() else {
|
||||
return Err(RuntimeError::MissingStreamingConfig {
|
||||
operation_id: operation.operation_id.as_str().to_owned(),
|
||||
});
|
||||
};
|
||||
|
||||
if streaming.mode != ExecutionMode::Session {
|
||||
return Err(RuntimeError::UnsupportedExecutionMode {
|
||||
operation_id: operation.operation_id.as_str().to_owned(),
|
||||
mode: streaming.mode,
|
||||
});
|
||||
}
|
||||
|
||||
let batch_size = streaming.max_items.unwrap_or(10).max(1);
|
||||
let seed_limit = batch_size.saturating_mul(4);
|
||||
let mut seeded_operation = operation.clone();
|
||||
if let Some(config) = seeded_operation.execution_config.streaming.as_mut() {
|
||||
config.mode = ExecutionMode::Window;
|
||||
config.window_duration_ms = config
|
||||
.window_duration_ms
|
||||
.or(config.poll_interval_ms)
|
||||
.or(config.upstream_timeout_ms)
|
||||
.or(Some(operation.execution_config.timeout_ms));
|
||||
config.max_items = Some(seed_limit);
|
||||
}
|
||||
|
||||
self.execute_window_with_auth_and_context(
|
||||
&seeded_operation,
|
||||
input,
|
||||
resolved_auth,
|
||||
request_context,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn prepare_request(
|
||||
&self,
|
||||
operation: &RuntimeOperation,
|
||||
@@ -379,98 +222,14 @@ impl RuntimeExecutor {
|
||||
.map_err(|error| map_protocol_adapter_error(operation, error))
|
||||
}
|
||||
|
||||
async fn execute_window_adapter(
|
||||
&self,
|
||||
operation: &RuntimeOperation,
|
||||
prepared_request: PreparedRequest,
|
||||
request_context: Option<&RuntimeRequestContext>,
|
||||
) -> Result<AdapterResponse, RuntimeError> {
|
||||
log_runtime_event("window.adapter.dispatch", operation, request_context);
|
||||
let Some(streaming) = operation.execution_config.streaming.as_ref() else {
|
||||
return Err(RuntimeError::MissingStreamingConfig {
|
||||
operation_id: operation.operation_id.as_str().to_owned(),
|
||||
});
|
||||
};
|
||||
let adapter = self.adapter_for(operation)?;
|
||||
if !adapter.supports_mode(ExecutionMode::Window) {
|
||||
return Err(RuntimeError::UnsupportedExecutionMode {
|
||||
operation_id: operation.operation_id.as_str().to_owned(),
|
||||
mode: ExecutionMode::Window,
|
||||
});
|
||||
}
|
||||
|
||||
let prepared_request = adapter_prepared_request(
|
||||
operation,
|
||||
&prepared_request,
|
||||
request_context,
|
||||
streaming
|
||||
.upstream_timeout_ms
|
||||
.unwrap_or(operation.execution_config.timeout_ms),
|
||||
);
|
||||
let adapter_context = adapter_request_context(request_context);
|
||||
let result = adapter
|
||||
.invoke_window(
|
||||
&operation.target,
|
||||
&prepared_request.into(),
|
||||
streaming.window_duration_ms.unwrap_or_default(),
|
||||
streaming.max_items,
|
||||
&adapter_context,
|
||||
)
|
||||
.await
|
||||
.map_err(|error| map_protocol_adapter_error(operation, error))?;
|
||||
|
||||
Ok(AdapterResponse {
|
||||
status_code: 200,
|
||||
headers: BTreeMap::new(),
|
||||
body: json!({
|
||||
"summary": result.summary,
|
||||
"items": result.items,
|
||||
"cursor": result.cursor,
|
||||
"done": result.window_complete,
|
||||
}),
|
||||
data: Value::Null,
|
||||
})
|
||||
}
|
||||
|
||||
fn acquire_unary_permit(
|
||||
&self,
|
||||
operation: &RuntimeOperation,
|
||||
_operation: &RuntimeOperation,
|
||||
) -> Result<OwnedSemaphorePermit, RuntimeError> {
|
||||
let (kind, limit, limiter) = if operation
|
||||
.execution_config
|
||||
.streaming
|
||||
.as_ref()
|
||||
.is_some_and(|streaming| streaming.mode == ExecutionMode::AsyncJob)
|
||||
{
|
||||
(
|
||||
"async_job",
|
||||
self.limits.max_concurrent_jobs,
|
||||
Arc::clone(&self.job_limiter),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
"unary",
|
||||
self.limits.max_concurrent_unary,
|
||||
Arc::clone(&self.unary_limiter),
|
||||
)
|
||||
};
|
||||
|
||||
try_acquire_limit(limiter, kind, limit)
|
||||
}
|
||||
|
||||
fn acquire_window_permit(&self) -> Result<OwnedSemaphorePermit, RuntimeError> {
|
||||
try_acquire_limit(
|
||||
Arc::clone(&self.window_limiter),
|
||||
"window",
|
||||
self.limits.max_concurrent_window,
|
||||
)
|
||||
}
|
||||
|
||||
fn acquire_session_permit(&self) -> Result<OwnedSemaphorePermit, RuntimeError> {
|
||||
try_acquire_limit(
|
||||
Arc::clone(&self.session_limiter),
|
||||
"session",
|
||||
self.limits.max_concurrent_sessions,
|
||||
Arc::clone(&self.unary_limiter),
|
||||
"unary",
|
||||
self.limits.max_concurrent_unary,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -564,8 +323,6 @@ impl PreparedRequest {
|
||||
path_params: read_string_map(request.get("path"), "request.path")?,
|
||||
query_params: read_string_map(request.get("query"), "request.query")?,
|
||||
headers: read_string_map(request.get("headers"), "request.headers")?,
|
||||
grpc: non_empty_payload(request.get("grpc").cloned()),
|
||||
variables: non_empty_payload(request.get("variables").cloned()),
|
||||
body: request.get("body").and_then(non_empty_body).cloned(),
|
||||
timeout_ms: 0,
|
||||
})
|
||||
@@ -586,10 +343,8 @@ fn adapter_prepared_request(
|
||||
request_context: Option<&RuntimeRequestContext>,
|
||||
timeout_ms: u64,
|
||||
) -> PreparedRequest {
|
||||
let empty_headers = BTreeMap::new();
|
||||
let static_headers = match &operation.target {
|
||||
Target::Rest(target) => &target.static_headers,
|
||||
_ => &empty_headers,
|
||||
};
|
||||
|
||||
let mut prepared_request = prepared_request.clone();
|
||||
@@ -717,10 +472,6 @@ fn log_runtime_event(
|
||||
}
|
||||
|
||||
fn response_cache_ttl(operation: &RuntimeOperation) -> Option<Duration> {
|
||||
if operation.execution_config.streaming.is_some() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let is_cacheable_protocol =
|
||||
matches!(&operation.target, Target::Rest(target) if target.method == HttpMethod::Get);
|
||||
if !is_cacheable_protocol {
|
||||
@@ -747,8 +498,6 @@ fn response_cache_key(
|
||||
"query_params": prepared_request.query_params,
|
||||
"headers": prepared_request.headers,
|
||||
"body": prepared_request.body,
|
||||
"variables": prepared_request.variables,
|
||||
"grpc": prepared_request.grpc,
|
||||
});
|
||||
let fingerprint_bytes = serde_json::to_vec(&fingerprint).ok()?;
|
||||
let fingerprint_hash = URL_SAFE_NO_PAD.encode(Sha256::digest(fingerprint_bytes));
|
||||
@@ -844,15 +593,7 @@ fn non_empty_body(value: &Value) -> Option<&Value> {
|
||||
}
|
||||
}
|
||||
|
||||
fn non_empty_payload(value: Option<Value>) -> Option<Value> {
|
||||
match value {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(Value::Object(object)) if object.is_empty() => None,
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(any())]
|
||||
mod tests {
|
||||
use std::collections::BTreeMap;
|
||||
use std::io;
|
||||
|
||||
Reference in New Issue
Block a user