pub struct LocalServiceBridge { /* private fields */ }Expand description
Adapt a crate::service::RequestService to IrohRequestProcessor.
Spins up a dedicated thread running a single-threaded tokio runtime with
a LocalSet. The service runs on that thread (compatible with both Send
and !Send services like inference); requests arriving on the iroh accept
loop are forwarded via an mpsc channel and awaited via oneshot.
Lifecycle: shutdown closes admission, drains queued and running requests within the same grace period as network RPC, then destroys residual LocalSet tasks and joins the thread. A retained client cannot pin the service. Final handle drop provides synchronous cleanup; explicit owners use async shutdown.
Implementations§
Source§impl LocalServiceBridge
impl LocalServiceBridge
Sourcepub fn spawn<S>(
service: S,
nonce_cache: Arc<InMemoryNonceCache>,
queue_depth: usize,
) -> Result<Self>where
S: RequestService + Send + 'static,
pub fn spawn<S>(
service: S,
nonce_cache: Arc<InMemoryNonceCache>,
queue_depth: usize,
) -> Result<Self>where
S: RequestService + Send + 'static,
Spawn a dedicated bridge thread that owns service and forwards
requests through crate::service::dispatch::process_request.
The response signing key is taken from service.signing_key() — there
is no separate parameter to avoid drift between the service’s identity
and the key used to sign responses.
queue_depth is the mpsc channel capacity — backpressure point when
the bridge falls behind. Pass 0 for default (128).
Sourcepub fn spawn_with<F, Fut, S>(
thread_name: impl Into<String>,
build: F,
nonce_cache: Arc<InMemoryNonceCache>,
queue_depth: usize,
) -> Result<(Self, Receiver<Result<()>>)>where
F: FnOnce() -> Fut + Send + 'static,
Fut: Future<Output = Result<S>>,
S: RequestService + 'static,
pub fn spawn_with<F, Fut, S>(
thread_name: impl Into<String>,
build: F,
nonce_cache: Arc<InMemoryNonceCache>,
queue_depth: usize,
) -> Result<(Self, Receiver<Result<()>>)>where
F: FnOnce() -> Fut + Send + 'static,
Fut: Future<Output = Result<S>>,
S: RequestService + 'static,
Like LocalServiceBridge::spawn, but constructs the service ON the
bridge thread via the async build closure. For services whose
construction is itself !Send or async (e.g. GPU init that must happen on
the serve thread, like InferenceService): the built service value never
has to be Send-moved across threads — only the builder’s captured inputs
do (F: Send), and the !Send result lives only on the bridge thread.
Returns the bridge plus a readiness receiver that resolves once build
completes: Ok(()) when the service is built and serving, or the build
error. Callers MUST await it before advertising the service (a build
failure otherwise surfaces only as “channel closed” on the first request).
Source§impl LocalServiceBridge
impl LocalServiceBridge
Sourcepub fn begin_shutdown(&self, deadline: Instant) -> Instant
pub fn begin_shutdown(&self, deadline: Instant) -> Instant
Begin draining immediately; subsequent callers reuse the first deadline.
Sourcepub async fn shutdown(&self) -> Result<(), BridgeShutdownError>
pub async fn shutdown(&self) -> Result<(), BridgeShutdownError>
Close admission, drain accepted work, and wait for service/runtime destruction. Joining runs off the caller’s runtime; no thread lock is held while waiting. Concurrent close calls share the same terminal result.
Trait Implementations§
Source§impl Drop for LocalServiceBridge
impl Drop for LocalServiceBridge
Source§impl IrohRequestProcessor for LocalServiceBridge
impl IrohRequestProcessor for LocalServiceBridge
Source§fn close_admission(&self)
fn close_admission(&self)
Source§fn begin_shutdown(&self, deadline: Instant) -> Instant
fn begin_shutdown(&self, deadline: Instant) -> Instant
fn process( &self, request: Bytes, carrier: CarrierContext, ) -> Pin<Box<dyn Future<Output = Result<Bytes>> + Send + '_>>
Auto Trait Implementations§
impl Freeze for LocalServiceBridge
impl !RefUnwindSafe for LocalServiceBridge
impl Send for LocalServiceBridge
impl Sync for LocalServiceBridge
impl Unpin for LocalServiceBridge
impl UnsafeUnpin for LocalServiceBridge
impl !UnwindSafe for LocalServiceBridge
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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