Drain a dispatched request's bounded response channel, enforcing a total-payload byte ceiling across streamed partials. Returns the final Response (non-streaming: pass-through; streaming: concatenated payload) or an error if the channel closed without a final chunk or if the accumulated payload would exceed the ceiling.
(
rx: &mut tokio::sync::mpsc::Receiver<Response>,
max_result_bytes: usize,
)
| 23 | /// concatenated payload) or an error if the channel closed without a |
| 24 | /// final chunk or if the accumulated payload would exceed the ceiling. |
| 25 | pub(crate) async fn collect_bounded_response( |
| 26 | rx: &mut tokio::sync::mpsc::Receiver<Response>, |
| 27 | max_result_bytes: usize, |
| 28 | ) -> Result<Response, DispatchCollectError> { |
| 29 | let mut combined_payload: Vec<u8> = Vec::new(); |
| 30 | let mut final_response_meta: Option<Response> = None; |
| 31 | let mut final_streaming = false; |
| 32 | |
| 33 | loop { |
| 34 | let Some(resp) = rx.recv().await else { break }; |
| 35 | if resp.partial { |
| 36 | combined_payload.extend_from_slice(&resp.payload); |
| 37 | if combined_payload.len() > max_result_bytes { |
| 38 | return Err(DispatchCollectError::OverBudget { |
| 39 | bytes: combined_payload.len(), |
| 40 | }); |
| 41 | } |
| 42 | } else if combined_payload.is_empty() { |
| 43 | return Ok(resp); |
| 44 | } else { |
| 45 | combined_payload.extend_from_slice(&resp.payload); |
| 46 | if combined_payload.len() > max_result_bytes { |
| 47 | return Err(DispatchCollectError::OverBudget { |
| 48 | bytes: combined_payload.len(), |
| 49 | }); |
| 50 | } |
| 51 | final_response_meta = Some(resp); |
| 52 | final_streaming = true; |
| 53 | break; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if final_streaming { |
| 58 | let meta = final_response_meta.expect("final_streaming ⇒ meta set"); |
| 59 | return Ok(Response { |
| 60 | payload: Payload::from_vec(combined_payload), |
| 61 | ..meta |
| 62 | }); |
| 63 | } |
| 64 | Err(DispatchCollectError::ChannelClosed) |
| 65 | } |
| 66 | |
| 67 | /// Current wall-clock time as milliseconds since Unix epoch. |
| 68 | /// |
no test coverage detected