runtime: add executor builder
This commit is contained in:
@@ -3,7 +3,6 @@ use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
|
||||
use crank_adapter_rest::RestAdapter;
|
||||
use crank_core::{
|
||||
AdapterRegistry, CachedHeader, CachedResponse, ExecutionMode, HttpMethod, ResponseCacheStore,
|
||||
SharedProtocolAdapter, Target, TransportBehavior,
|
||||
@@ -37,18 +36,28 @@ impl Default for RuntimeExecutor {
|
||||
|
||||
impl RuntimeExecutor {
|
||||
pub fn new() -> Self {
|
||||
Self::with_limits(RuntimeLimits::default())
|
||||
crate::executor_builder::community_default().build()
|
||||
}
|
||||
|
||||
pub fn with_limits(limits: RuntimeLimits) -> Self {
|
||||
crate::executor_builder::community_default()
|
||||
.with_limits(limits)
|
||||
.build()
|
||||
}
|
||||
|
||||
pub(crate) fn from_builder_parts(
|
||||
limits: RuntimeLimits,
|
||||
adapters: AdapterRegistry,
|
||||
response_cache: Option<Arc<dyn ResponseCacheStore>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
adapters: community_adapter_registry(),
|
||||
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: None,
|
||||
response_cache,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -514,10 +523,6 @@ impl PreparedRequest {
|
||||
}
|
||||
}
|
||||
|
||||
fn community_adapter_registry() -> AdapterRegistry {
|
||||
AdapterRegistry::empty().register(Arc::new(RestAdapter::new()) as SharedProtocolAdapter)
|
||||
}
|
||||
|
||||
fn adapter_request_context(
|
||||
request_context: Option<&RuntimeRequestContext>,
|
||||
) -> crank_core::RuntimeRequestContext {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crank_adapter_rest::RestAdapter;
|
||||
use crank_core::{AdapterRegistry, ResponseCacheStore, SharedProtocolAdapter};
|
||||
|
||||
use crate::{RuntimeExecutor, RuntimeLimits};
|
||||
|
||||
pub struct RuntimeExecutorBuilder {
|
||||
limits: RuntimeLimits,
|
||||
adapters: AdapterRegistry,
|
||||
response_cache: Option<Arc<dyn ResponseCacheStore>>,
|
||||
}
|
||||
|
||||
impl RuntimeExecutorBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
limits: RuntimeLimits::default(),
|
||||
adapters: AdapterRegistry::empty(),
|
||||
response_cache: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_limits(mut self, limits: RuntimeLimits) -> Self {
|
||||
self.limits = limits;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn register_adapter(mut self, adapter: SharedProtocolAdapter) -> Self {
|
||||
self.adapters = self.adapters.register(adapter);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_response_cache(mut self, store: Arc<dyn ResponseCacheStore>) -> Self {
|
||||
self.response_cache = Some(store);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> RuntimeExecutor {
|
||||
RuntimeExecutor::from_builder_parts(self.limits, self.adapters, self.response_cache)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RuntimeExecutorBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn community_default() -> RuntimeExecutorBuilder {
|
||||
RuntimeExecutorBuilder::new()
|
||||
.register_adapter(Arc::new(RestAdapter::new()) as SharedProtocolAdapter)
|
||||
}
|
||||
@@ -3,6 +3,7 @@ mod auth;
|
||||
mod cache;
|
||||
mod error;
|
||||
mod executor;
|
||||
mod executor_builder;
|
||||
mod limits;
|
||||
mod model;
|
||||
mod rate_limit;
|
||||
@@ -19,6 +20,7 @@ pub use cache::{
|
||||
};
|
||||
pub use error::RuntimeError;
|
||||
pub use executor::RuntimeExecutor;
|
||||
pub use executor_builder::{RuntimeExecutorBuilder, community_default};
|
||||
pub use limits::{RuntimeLimits, RuntimeLimitsConfigError};
|
||||
pub use model::{AdapterResponse, PreparedRequest, RuntimeOperation};
|
||||
pub use rate_limit::{
|
||||
|
||||
Reference in New Issue
Block a user