@@ -8,7 +8,7 @@ use serde_json::value::RawValue;
88use tokio:: { pin, select, sync:: mpsc, task:: JoinHandle } ;
99use tokio_stream:: StreamExt ;
1010use tokio_util:: sync:: WaitForCancellationFutureOwned ;
11- use tracing:: { debug, debug_span, error, instrument , trace, Instrument } ;
11+ use tracing:: { debug, debug_span, error, trace, Instrument } ;
1212
1313/// Default notification buffer size per task.
1414pub const DEFAULT_NOTIFICATION_BUFFER_PER_CLIENT : usize = 16 ;
@@ -163,8 +163,7 @@ where
163163 /// and routes them to the router. For each request, a new task is spawned
164164 /// to handle the request, and given a sender to the [`WriteTask`]. This
165165 /// ensures that requests can be handled concurrently.
166- #[ instrument( name = "RouteTask" , skip( self ) , fields( conn_id = self . conn_id) ) ]
167- pub async fn task_future ( self , cancel : WaitForCancellationFutureOwned ) {
166+ pub ( crate ) async fn task_future ( self , cancel : WaitForCancellationFutureOwned ) {
168167 let RouteTask {
169168 router,
170169 mut requests,
@@ -200,27 +199,24 @@ where
200199 // enforces the specification.
201200 let reqs = InboundData :: try_from( item) . unwrap_or_default( ) ;
202201
203- let span = debug_span!( "pubsub request handling" , reqs = reqs. len( ) ) ;
204-
205- let ctx =
206- HandlerCtx :: new(
207- Some ( write_task. clone( ) ) ,
208- children. clone( ) ,
209- ) ;
210-
211- let fut = router. handle_request_batch( ctx, reqs) ;
212- let write_task = write_task. clone( ) ;
213-
214202 // Acquiring the permit before spawning the task means that
215203 // the write task can backpressure the route task. I.e.
216204 // if the client stops accepting responses, we do not keep
217205 // handling inbound requests.
218- let Ok ( permit) = write_task. reserve_owned( ) . await else {
206+ let Ok ( permit) = write_task. clone ( ) . reserve_owned( ) . await else {
219207 tracing:: error!( "write task dropped while waiting for permit" ) ;
220208 break ;
221209 } ;
222210
211+ let ctx =
212+ HandlerCtx :: new(
213+ Some ( write_task. clone( ) ) ,
214+ children. clone( ) ,
215+ ) ;
216+
223217 // Run the future in a new task.
218+ let fut = router. handle_request_batch( ctx, reqs) ;
219+
224220 children. spawn_cancellable(
225221 async move {
226222 // Send the response to the write task.
@@ -232,7 +228,6 @@ where
232228 ) ;
233229 }
234230 }
235- . instrument( span)
236231 ) ;
237232 }
238233 }
@@ -277,7 +272,6 @@ impl<T: Listener> WriteTask<T> {
277272 /// [`ServerShutdown`] struct.
278273 ///
279274 /// [`ServerShutdown`]: crate::pubsub::ServerShutdown
280- #[ instrument( skip( self ) , fields( conn_id = self . conn_id) ) ]
281275 pub ( crate ) async fn task_future ( self ) {
282276 let WriteTask {
283277 tasks,
@@ -299,8 +293,9 @@ impl<T: Listener> WriteTask<T> {
299293 tracing:: error!( "Json stream has closed" ) ;
300294 break ;
301295 } ;
302- if let Err ( err) = connection. send_json( json) . await {
303- debug!( %err, "Failed to send json" ) ;
296+ let span = debug_span!( "WriteTask" , conn_id = self . conn_id) ;
297+ if let Err ( err) = connection. send_json( json) . instrument( span) . await {
298+ debug!( %err, conn_id = self . conn_id, "Failed to send json" ) ;
304299 break ;
305300 }
306301 }
0 commit comments