1
1
//! JSON-RPC router.
2
2
3
3
use crate :: {
4
- routes:: { MakeErasedHandler , RouteFuture } ,
5
- BoxedIntoRoute , ErasedIntoRoute , Handler , HandlerArgs , Method , MethodId , RegistrationError ,
6
- Route ,
4
+ routes:: { BatchFuture , MakeErasedHandler , RouteFuture } ,
5
+ types:: InboundData ,
6
+ BoxedIntoRoute , ErasedIntoRoute , Handler , HandlerArgs , HandlerCtx , Method , MethodId ,
7
+ RegistrationError , Route ,
7
8
} ;
8
9
use core:: fmt;
9
10
use serde_json:: value:: RawValue ;
@@ -193,7 +194,7 @@ where
193
194
where
194
195
T : Service <
195
196
HandlerArgs ,
196
- Response = Box < RawValue > ,
197
+ Response = Option < Box < RawValue > > ,
197
198
Error = Infallible ,
198
199
Future : Send + ' static ,
199
200
> + Clone
@@ -299,15 +300,35 @@ where
299
300
/// This is a convenience method, primarily for testing. Use in production
300
301
/// code is discouraged. Routers should not be left in incomplete states.
301
302
pub fn call_with_state ( & self , args : HandlerArgs , state : S ) -> RouteFuture {
302
- let id = args. req . id_owned ( ) ;
303
- let method = args. req . method ( ) ;
303
+ let id = args. req ( ) . id_owned ( ) ;
304
+ let method = args. req ( ) . method ( ) ;
304
305
305
- let span = debug_span ! ( "Router::call_with_state" , %method, % id) ;
306
- trace ! ( params = args. req. params( ) ) ;
306
+ let span = debug_span ! ( "Router::call_with_state" , %method, ? id) ;
307
+ trace ! ( params = args. req( ) . params( ) ) ;
307
308
308
309
self . inner . call_with_state ( args, state) . with_span ( span)
309
310
}
310
311
312
+ /// Call a method on the router, without providing state.
313
+ pub fn call_batch_with_state (
314
+ & self ,
315
+ ctx : HandlerCtx ,
316
+ inbound : InboundData ,
317
+ state : S ,
318
+ ) -> BatchFuture {
319
+ let mut fut = BatchFuture :: new_with_capacity ( inbound. single ( ) , inbound. len ( ) ) ;
320
+ // According to spec, non-parsable requests should still receive a
321
+ // response.
322
+ for req in inbound. iter ( ) {
323
+ let req = req. map ( |req| {
324
+ let args = HandlerArgs :: new ( ctx. clone ( ) , req) ;
325
+ self . call_with_state ( args, state. clone ( ) )
326
+ } ) ;
327
+ fut. push_parse_result ( req) ;
328
+ }
329
+ fut
330
+ }
331
+
311
332
/// Nest this router into a new Axum router, with the specified path.
312
333
#[ cfg( feature = "axum" ) ]
313
334
pub fn into_axum ( self , path : & str ) -> axum:: Router < S > {
@@ -316,22 +337,27 @@ where
316
337
}
317
338
318
339
impl Router < ( ) > {
319
- // // / Serve the router over a connection. This method returns a
320
- // // / [`ServerShutdown`], which will shut down the server when dropped.
321
- // // /
322
- // // / [`ServerShutdown`]: crate::pubsub::ServerShutdown
323
- // #[cfg(feature = "pubsub")]
324
- // pub async fn serve_pubsub<C: crate::pubsub::Connect>(
325
- // self,
326
- // connect: C,
327
- // ) -> Result<crate::pubsub::ServerShutdown, C::Error> {
328
- // connect.run (self).await
329
- // }
340
+ /// Serve the router over a connection. This method returns a
341
+ /// [`ServerShutdown`], which will shut down the server when dropped.
342
+ ///
343
+ /// [`ServerShutdown`]: crate::pubsub::ServerShutdown
344
+ #[ cfg( feature = "pubsub" ) ]
345
+ pub async fn serve_pubsub < C : crate :: pubsub:: Connect > (
346
+ self ,
347
+ connect : C ,
348
+ ) -> Result < crate :: pubsub:: ServerShutdown , C :: Error > {
349
+ connect. serve ( self ) . await
350
+ }
330
351
331
352
/// Call a method on the router.
332
353
pub fn handle_request ( & self , args : HandlerArgs ) -> RouteFuture {
333
354
self . call_with_state ( args, ( ) )
334
355
}
356
+
357
+ /// Call a batch of methods on the router.
358
+ pub fn handle_request_batch ( & self , ctx : HandlerCtx , batch : InboundData ) -> BatchFuture {
359
+ self . call_batch_with_state ( ctx, batch, ( ) )
360
+ }
335
361
}
336
362
337
363
impl < S > fmt:: Debug for Router < S > {
@@ -341,7 +367,7 @@ impl<S> fmt::Debug for Router<S> {
341
367
}
342
368
343
369
impl tower:: Service < HandlerArgs > for Router < ( ) > {
344
- type Response = Box < RawValue > ;
370
+ type Response = Option < Box < RawValue > > ;
345
371
type Error = Infallible ;
346
372
type Future = RouteFuture ;
347
373
@@ -355,7 +381,7 @@ impl tower::Service<HandlerArgs> for Router<()> {
355
381
}
356
382
357
383
impl tower:: Service < HandlerArgs > for & Router < ( ) > {
358
- type Response = Box < RawValue > ;
384
+ type Response = Option < Box < RawValue > > ;
359
385
type Error = Infallible ;
360
386
type Future = RouteFuture ;
361
387
@@ -517,7 +543,7 @@ impl<S> RouterInner<S> {
517
543
/// Call a method on the router, with the provided state.
518
544
#[ track_caller]
519
545
pub ( crate ) fn call_with_state ( & self , args : HandlerArgs , state : S ) -> RouteFuture {
520
- let method = args. req . method ( ) ;
546
+ let method = args. req ( ) . method ( ) ;
521
547
self . method_by_name ( method)
522
548
. unwrap_or ( & self . fallback )
523
549
. call_with_state ( args, state)
0 commit comments