@@ -13,7 +13,7 @@ use tokio_util::sync::CancellationToken;
1313use tokio_util:: task:: AbortOnDropHandle ;
1414use tonic:: Streaming ;
1515use tonic:: transport:: Channel ;
16- use tracing:: error;
16+ use tracing:: { error, warn } ;
1717
1818use super :: {
1919 ParentMessageInfo , SharedMapTaskContext , UserDefinedMessage , create_response_stream,
@@ -23,24 +23,34 @@ use super::{
2323/// Type alias for the response - raw results from the UDF
2424pub ( in crate :: mapper) type UnaryMapResponse = Vec < map:: map_response:: Result > ;
2525
26+ /// Type aliases for HashMap used to track the oneshot response sender for each request keyed by
27+ /// message id.
2628type ResponseSenderMap = HashMap < String , oneshot:: Sender < Result < UnaryMapResponse > > > ;
2729
30+ /// Shared state for tracking batch map senders between the sender and the receiver tasks.
31+ /// We have BiDi gRPC stream so we have 2 different set of tasks for sending and receiving.
2832#[ derive( Default ) ]
2933pub ( in crate :: mapper) struct UnarySenderMapState {
34+ /// Map of oneshot response senders keyed by message id.
3035 map : ResponseSenderMap ,
36+ /// Flag to indicate whether the rx task has closed the stream and cleared the `map`.
37+ /// This is because `tx.send()` could return `Ok()` even after the receiver task has closed the
38+ /// stream.
3139 closed : bool ,
3240}
3341
34- /// MapUnaryTask encapsulates all the context needed to execute a unary map operation.
42+ /// MapUnaryTask encapsulates all the context needed to execute a unary map operation per message .
3543pub ( in crate :: mapper) struct MapUnaryTask {
3644 pub mapper : UserDefinedUnaryMap ,
45+ /// Permit to achieve structured concurrency by ensuring we do not exceed the concurrency limit
46+ /// and all the tasks are cleaned up when the component is shutting down.
3747 pub permit : OwnedSemaphorePermit ,
3848 pub message : Message ,
3949 pub shared_ctx : Arc < SharedMapTaskContext > ,
4050}
4151
4252impl MapUnaryTask {
43- /// Spawns the unary map task as a tokio task.
53+ /// Spawns the unary map task as a tokio task per [MapUnaryTask] .
4454 /// The task will process the message through the UDF and send results downstream.
4555 pub fn spawn ( self ) {
4656 tokio:: spawn ( async move {
@@ -229,7 +239,9 @@ impl UserDefinedUnaryMap {
229239 if !senders_guard. closed {
230240 senders_guard. map . insert ( key. clone ( ) , tx) ;
231241 } else {
232- let _ = tx. send ( Err ( Error :: Mapper ( "mapper closed" . to_string ( ) ) ) ) ;
242+ let _ = tx
243+ . send ( Err ( Error :: Mapper ( "mapper closed" . to_string ( ) ) ) )
244+ . inspect ( |_| warn ! ( "failed to send error to oneshot receiver" ) ) ;
233245 }
234246 } ;
235247
0 commit comments