Skip to main content

XetRuntime

Struct XetRuntime 

Source
pub struct XetRuntime { /* private fields */ }
Expand description

XetRuntime is the async execution backend: it either owns a Tokio multi-thread runtime or wraps an external TokioRuntimeHandle (see RuntimeMode).

It exposes Self::bridge_async and Self::bridge_sync to run work on the pool, Self::spawn for fire-and-forget tasks, and Self::perform_sigint_shutdown / Self::in_sigint_shutdown so callers can align with process-wide SIGINT teardown.

§Example

use xet_runtime::config::XetConfig;
use xet_runtime::core::XetRuntime;

let pool = XetRuntime::new(&XetConfig::new()).expect("Error initializing runtime.");

let result = pool
    .bridge_sync(async {
        // Your async code here
        42
    })
    .expect("Task Error.");

assert_eq!(result, 42);

Implementations§

Source§

impl XetRuntime

Source

pub fn new(config: &XetConfig) -> Result<Arc<Self>, RuntimeError>

Creates a new owned tokio thread pool with the given configuration.

Source

pub fn from_external_with_config( rt_handle: TokioRuntimeHandle, config: &XetConfig, ) -> Result<Arc<Self>, RuntimeError>

Wraps an existing tokio handle with a new XetRuntime, using the config for system monitor setup.

Source

pub fn from_external(rt_handle: TokioRuntimeHandle) -> Arc<Self>

Wraps an existing tokio handle without system monitoring.

Source

pub fn current_if_exists() -> Option<Arc<Self>>

Returns the current thread’s active owned XetRuntime, if any.

This is populated on owned runtime worker threads and on spawn_blocking threads created by an owned runtime. External runtimes do not set this.

Source

pub fn handle(&self) -> TokioRuntimeHandle

Source

pub fn num_worker_threads(&self) -> usize

Source

pub fn external_executor_count(&self) -> usize

Gives the number of concurrent sync bridge callers (external_run_async_task and bridge_sync).

Source

pub fn perform_sigint_shutdown(&self)

Cancels and shuts down the runtime. All tasks currently running will be aborted.

A concurrent bridge_sync or in-flight bridge_async may still hold a cloned Arc to the tokio runtime until that call returns, so teardown of the reactor may complete only after those finish.

Source

pub fn discard_runtime(&self)

Discards the runtime without shutdown; to be used after fork-exec or spawn.

Source

pub fn in_sigint_shutdown(&self) -> bool

Returns true if we’re in the middle of a sigint shutdown, and false otherwise.

Source

pub fn external_run_async_task<F>( &self, future: F, ) -> Result<F::Output, RuntimeError>
where F: Future + Send + 'static, F::Output: Send + 'static,

This function should ONLY be used by threads outside of tokio; it should not be called from within a task running on the runtime worker pool. Doing so can lead to deadlocking.

Source

pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
where F: Future + Send + 'static, F::Output: Send + 'static,

Spawn an async task to run in the background on the current pool of worker threads.

Source

pub async fn bridge_async<T, F>( &self, task_name: &'static str, fut: F, ) -> Result<T, RuntimeError>
where F: Future<Output = T> + Send + 'static, T: Send + 'static,

Run a future on the appropriate runtime for this XetRuntime.

  • External mode: the future is awaited directly on the caller’s executor.
  • Owned mode: the future is spawned onto the owned thread pool and the result is delivered via a oneshot channel (compatible with any executor).

This is the primary async entry point. Session-level async methods should call ctx.runtime.bridge_async(...).

Source

pub fn bridge_sync<F>(&self, future: F) -> Result<F::Output, RuntimeError>
where F: Future + Send + 'static, F::Output: Send + 'static,

Run an async future synchronously, blocking the calling thread until completion.

Only supported on Owned runtimes. Returns RuntimeError::InvalidRuntime when called on an External-mode runtime.

The caller must not be on a tokio worker thread (calling from spawn_blocking threads, OS threads, or the main thread is fine).

This is the primary sync entry point. Session-level _blocking methods should simply call ctx.runtime.bridge_sync(...).

Source

pub fn spawn_blocking<F, R>(self: &Arc<Self>, f: F) -> JoinHandle<R>
where F: FnOnce() -> R + Send + 'static, R: Send + 'static,

Spawn a blocking task on the runtime’s blocking thread pool. Installs a weak thread-local reference to this pool for the duration of f.

Source

pub fn mode(&self) -> RuntimeMode

Returns the runtime mode (Owned or External).

Source

pub fn from_validated_external( rt_handle: TokioRuntimeHandle, config: &XetConfig, ) -> Result<Arc<Self>, RuntimeError>

Wraps a caller-provided tokio handle after validating that it meets requirements.

Source

pub fn handle_meets_requirements(handle: &TokioRuntimeHandle) -> bool

Probe whether a tokio runtime handle meets the requirements for use as an External-mode runtime.

Checks:

  1. Multi-threaded flavor.
  2. Time driver – required for timeouts, retry backoff, and progress intervals.
  3. IO driver – required for all network I/O via reqwest/hyper.

Driver availability is probed by entering the handle’s context and polling a driver-dependent future once inside catch_unwind. Tokio panics synchronously on the first poll when a driver is absent, so the result is immediate.

Fragility note: this probing technique relies on tokio panicking synchronously on the first poll of tokio::time::sleep / tokio::net::TcpListener::bind when the corresponding driver is absent. This is undocumented internal behavior validated against tokio 1.x.

Trait Implementations§

Source§

impl Debug for XetRuntime

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for XetRuntime

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for XetRuntime

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> DropFlavorWrapper<T> for T

Source§

type Flavor = MayDrop

The DropFlavor that wraps T into Self
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

impl<T> Identity for T
where T: ?Sized,

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<E> ResultError for E
where E: Send + Debug + Sync,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more