Skip to main content

StreamingContext

Struct StreamingContext 

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

Per-invocation streaming context. Owns the producer end of the mpsc::Sender<Vec<u8>> channel into which guest-emitted chunks are pushed; the API gateway holds the matching receiver and forwards the bytes onto the wire.

Cloning is cheap (a refcount bump on each of the two Arc fields). The atomic byte-counter is shared across clones so the per-invocation cap remains a single source of truth even if the context is duplicated across host-fn closures.

Implementations§

Source§

impl StreamingContext

Source

pub fn disabled() -> Self

Construct a context with streaming disabled. Every emit_chunk returns -1; useful as the default for invocations the gateway did not opt into the streaming response path.

Source

pub fn with_channel(sender: Sender<Vec<u8>>) -> Self

Construct a context wired to sender. Bytes pushed via emit_chunk are forwarded onto the channel; the gateway-side receiver drives the SSE / chunked response body.

The total-bytes cap defaults to MAX_TOTAL_STREAM_BYTES — embedders that need a different cap should construct the context directly via Self::with_channel_and_cap.

Source

pub fn with_channel_and_cap(sender: Sender<Vec<u8>>, max_total: u64) -> Self

Construct a context wired to sender with an explicit total-bytes cap. Intended primarily for tests that exercise the cap branch without having to emit 64 MiB of data.

Source

pub async fn emit_chunk(&self, bytes: Vec<u8>) -> i32

Forward bytes to the downstream receiver if streaming is enabled.

Returns:

  • 0 on success (bytes accepted). The chunk’s contribution is added to Self::bytes_emitted.
  • -1 if streaming is disabled (no channel attached).
  • -2 if accepting this chunk would push the total past max_total. The counter is rolled back afterward.
  • -3 if the receiver has been dropped — the downstream HTTP client disconnected. The counter is rolled back symmetrically.

Concurrency note: the running total is maintained with an optimistic fetch_add followed by a rollback on the failure branches, so it reflects only successfully forwarded bytes once quiescent. While concurrent emits are in flight, a reader of Self::bytes_emitted may briefly observe a value above the bytes actually forwarded — each in-flight emit can overshoot by its own chunk size before its rollback lands, so with the per-call cap the transient overshoot is bounded by roughly 2 * MAX_CHUNK_BYTES per racing emit. The overshoot is reconciled (rolled back) before each failing call returns; it never causes the cap to under-count accepted bytes.

Source

pub fn flush(&self) -> i32

Flush any buffered chunks. The scaffold uses an unbuffered mpsc::Sender whose send already delivers per-call, so this is a no-op. v0.4 may introduce a coalescing buffer in front of the channel; the flush hook is reserved for that.

Returns 0 on success, -1 when streaming is disabled.

Source

pub fn bytes_emitted(&self) -> u64

Snapshot of bytes successfully emitted so far. Reads through Ordering::SeqCst for symmetry with Self::emit_chunk.

Source

pub fn is_enabled(&self) -> bool

true if a receiver channel is attached (i.e. the invocation was dispatched through the streaming response path).

Trait Implementations§

Source§

impl Clone for StreamingContext

Source§

fn clone(&self) -> StreamingContext

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for StreamingContext

Source§

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

Formats the value using the given formatter. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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