pub struct JsonSocketBuilder<T> { /* private fields */ }Expand description
Route builder that expects and deserialises a JSON request body of type T.
Obtained from ServerMechanism::json. Optionally attach shared state via
state, or finalise immediately with
onconnect /
onconnect_sync.
Implementations§
Source§impl<T: DeserializeOwned + Send + 'static> JsonSocketBuilder<T>
impl<T: DeserializeOwned + Send + 'static> JsonSocketBuilder<T>
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 the deserialised JSON body.
Before the handler is called, the incoming request body is parsed as
Content-Type: application/json and deserialised into T. The handler receives
a ready-to-use T — no manual parsing is needed. If the body is absent or cannot
be decoded the request is rejected before the handler is ever invoked.
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 the deserialised JSON body.
The body is decoded into T before the handler is dispatched, identical to the
async variant. The closure may block but must complete quickly to avoid exhausting
the blocking thread pool.
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. Additionally, any panic inside the handler is silently converted
into a Rejection, masking runtime errors. Callers must ensure the handler completes
quickly and that adequate backpressure or rate limiting is applied externally.
Sourcepub fn state<S: Clone + Send + Sync + 'static>(
self,
state: S,
) -> StatefulJsonSocketBuilder<T, S>
pub fn state<S: Clone + Send + Sync + 'static>( self, state: S, ) -> StatefulJsonSocketBuilder<T, S>
Attaches shared state S, transitioning to StatefulJsonSocketBuilder.
Alternative ordering to .state(s).json::<T>() — both produce the same route.
A fresh clone of S is injected alongside the JSON-decoded T on every request.
The handler will receive (state: S, body: T).
S must be Clone + Send + Sync + 'static.