pub struct StatefulJsonSocketBuilder<T, S> { /* private fields */ }Expand description
Route builder that carries shared state S and expects a JSON body of type T.
Obtained from JsonSocketBuilder::state. S must be Clone + Send + Sync + 'static.
T must implement serde::de::DeserializeOwned. Finalise with
onconnect /
onconnect_sync.
Implementations§
Source§impl<T: DeserializeOwned + Send + 'static, S: Clone + Send + Sync + 'static> StatefulJsonSocketBuilder<T, S>
impl<T: DeserializeOwned + Send + 'static, S: Clone + Send + Sync + 'static> StatefulJsonSocketBuilder<T, S>
Sourcepub fn onconnect<F, Fut, Re>(self, handler: F) -> SocketType
pub fn onconnect<F, Fut, Re>(self, handler: F) -> SocketType
Finalises this route with an async handler that receives (state: S, body: T).
On each request the incoming JSON body is deserialised into T and a fresh clone
of S is prepared — both are handed to the handler together. The handler is only
called when the body can be decoded; a missing or malformed body is rejected first.
The handler must return Result<impl Reply, Rejection>.
Returns a SocketType ready to be passed to Server::mechanism.
Sourcepub unsafe fn onconnect_sync<F, Re>(self, handler: F) -> SocketType
pub unsafe fn onconnect_sync<F, Re>(self, handler: F) -> SocketType
Finalises this route with a synchronous handler that receives
(state: S, body: T).
The body is decoded into T and a clone of S is prepared before the blocking
handler is dispatched. If S wraps a lock, keep it held only briefly. See
ServerMechanism::onconnect_sync for the full thread-pool safety notes.
Returns a SocketType ready to be passed to Server::mechanism.
§Safety
Every incoming request spawns an independent task on Tokio’s blocking thread pool.
The pool caps the number of live OS threads (default 512), but the queue of waiting
tasks is unbounded — under a traffic surge, tasks accumulate without limit, consuming
unbounded memory and causing severe latency spikes or OOM crashes before any queued task
gets a chance to run. When the handler acquires a lock on S (e.g. Arc<Mutex<_>>),
concurrent blocking tasks contending on the same lock can stall indefinitely, causing the
thread pool queue to grow without bound and compounding the exhaustion risk. Additionally,
any panic inside the handler is silently converted into a Rejection, masking runtime
errors. Callers must ensure the handler completes quickly, that lock contention on S
cannot produce indefinite stalls, and that adequate backpressure or rate limiting is
applied externally.