use crate::runtime::buffer_pool::BufferPool;
use crate::types::ByteSize;
use aws_smithy_types::error::operation::BuildError;
#[non_exhaustive]
#[derive(Debug)]
pub struct RuntimeConfig {
kind: Kind,
}
#[derive(Debug)]
enum Kind {
Managed(Managed),
MultiThreadTokio,
}
impl RuntimeConfig {
pub fn managed(managed: Managed) -> Self {
Self {
kind: Kind::Managed(managed),
}
}
pub fn multi_thread_tokio() -> Self {
Self {
kind: Kind::MultiThreadTokio,
}
}
pub(crate) fn managed_config(&self) -> Option<&Managed> {
match &self.kind {
Kind::Managed(managed) => Some(managed),
Kind::MultiThreadTokio => None,
}
}
#[cfg(not(target_family = "wasm"))]
pub(crate) fn managed_config_mut(&mut self) -> Option<&mut Managed> {
match &mut self.kind {
Kind::Managed(managed) => Some(managed),
Kind::MultiThreadTokio => None,
}
}
}
impl Default for RuntimeConfig {
fn default() -> Self {
Self::managed(Managed::default())
}
}
#[non_exhaustive]
#[derive(Debug, Default)]
pub struct Managed {
#[cfg_attr(
target_family = "wasm",
expect(dead_code, reason = "wasm targets build no managed runtime")
)]
source: Source,
}
#[derive(Debug, Default)]
enum Source {
#[default]
Automatic,
#[cfg(feature = "__unstable_ext")]
Adopted(Option<aws_smithy_execution::ManagedRuntime>),
}
impl Managed {
#[cfg(feature = "__unstable_ext")]
pub(crate) fn from_runtime(runtime: aws_smithy_execution::ManagedRuntime) -> Self {
Self {
source: Source::Adopted(Some(runtime)),
}
}
#[cfg(not(target_family = "wasm"))]
pub(crate) fn build(
&mut self,
) -> Result<aws_smithy_execution::ManagedRuntime, aws_smithy_runtime_api::box_error::BoxError>
{
match &mut self.source {
Source::Automatic => aws_smithy_execution::ManagedRuntime::builder()
.thread_name_prefix("s3-tm")
.pin_threads(false)
.build()
.map_err(Into::into),
#[cfg(feature = "__unstable_ext")]
Source::Adopted(owner) => {
Ok(owner.take().expect("adopted managed runtime already taken"))
}
}
}
}
#[cfg(all(test, feature = "__unstable_ext", not(miri)))]
mod tests {
use super::*;
use aws_smithy_execution::{topology::Topology, ManagedRuntime};
#[test]
fn adoption_preserves_identity_topology_and_names_and_takes_owner_once() {
let runtime = ManagedRuntime::builder()
.topology(Topology::detect().unwrap().with_max_placements(1))
.thread_name_prefix("external")
.pin_threads(false)
.build()
.unwrap();
let before = runtime.handle();
let mut managed = Managed::from_runtime(runtime);
let runtime = managed.build().unwrap();
let after = runtime.handle();
assert_eq!(before.id(), after.id());
assert_eq!(
before.topology().placements(),
after.topology().placements()
);
let caller = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let partition = after.partitions()[0].clone();
let expected = partition.id();
let observed = after.clone();
caller
.block_on(partition.spawn(async move {
assert_eq!(observed.current_partition().unwrap().id(), expected);
assert!(std::thread::current()
.name()
.unwrap()
.starts_with("external-"));
}))
.unwrap();
let repeated = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| managed.build()));
assert!(
repeated.is_err(),
"a consumed owner must not become automatic execution"
);
caller.block_on(runtime.shutdown()).unwrap();
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Default)]
pub enum ConcurrencyMode {
#[default]
Auto,
TargetThroughput(TargetThroughput),
Explicit(usize),
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TargetThroughput {
gbps: f64,
}
impl TargetThroughput {
pub fn gbps(gbps: f64) -> Result<Self, BuildError> {
if gbps.is_finite() && gbps > 0.0 {
Ok(Self { gbps })
} else {
Err(BuildError::invalid_field(
"gbps",
format!("throughput target must be finite and positive, got {gbps}"),
))
}
}
pub fn as_gbps(&self) -> f64 {
self.gbps
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Default)]
pub enum MemoryConfig {
#[default]
Auto,
Explicit(BufferPool),
}
#[non_exhaustive]
#[derive(Debug, Clone, Default)]
pub enum MemoryBudgetConfig {
#[default]
Auto,
Fraction(f64),
Limit(ByteSize),
}