-
Notifications
You must be signed in to change notification settings - Fork 0
Implement transport-level fragmentation and inbound reassembly #397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
leynos
merged 5 commits into
main
from
terragon/implement-transport-fragmentation-0if8br
Nov 26, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
166d25c
feat(fragment): add transport-agnostic Reassembler for inbound messag…
leynos aad0d1a
refactor(fragment tests): unify fragment header usage in tests
leynos 3ed15cd
feat(fragment): refactor reassembler to simplify fragment handling
leynos da43b0d
test(fragment/reassembly): add focused reassembly tests and reorganiz…
leynos cb4c566
refactor(tests/worlds/fragment): refactor reassembly error assertions…
leynos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,265 @@ | ||
| //! Inbound helper that stitches fragments back into complete messages. | ||
| //! | ||
| //! [`Reassembler`] mirrors the outbound [`Fragmenter`](crate::fragment::Fragmenter) by | ||
| //! collecting fragment payloads keyed by [`MessageId`](crate::fragment::MessageId). | ||
| //! It enforces ordering via [`FragmentSeries`](crate::fragment::FragmentSeries), guards | ||
| //! against unbounded allocation with a configurable cap, and purges stale partial | ||
| //! assemblies after a fixed timeout. The helper is transport-agnostic so codecs and | ||
| //! behavioural tests can reuse it without depending on socket types. | ||
|
|
||
| use std::{ | ||
| collections::{ | ||
| HashMap, | ||
| hash_map::{Entry, OccupiedEntry}, | ||
| }, | ||
| num::NonZeroUsize, | ||
| time::{Duration, Instant}, | ||
| }; | ||
|
|
||
| use bincode::error::DecodeError; | ||
|
|
||
| use super::{FragmentHeader, FragmentSeries, FragmentStatus, MessageId, ReassemblyError}; | ||
| use crate::message::Message; | ||
|
|
||
| #[derive(Debug)] | ||
| struct PartialMessage { | ||
| series: FragmentSeries, | ||
| buffer: Vec<u8>, | ||
| started_at: Instant, | ||
| } | ||
|
|
||
| impl PartialMessage { | ||
| fn new(series: FragmentSeries, payload: &[u8], started_at: Instant) -> Self { | ||
| Self { | ||
| series, | ||
| buffer: payload.to_vec(), | ||
| started_at, | ||
| } | ||
| } | ||
|
|
||
| fn push(&mut self, payload: &[u8]) { self.buffer.extend_from_slice(payload); } | ||
|
|
||
| fn len(&self) -> usize { self.buffer.len() } | ||
|
|
||
| fn started_at(&self) -> Instant { self.started_at } | ||
|
|
||
| fn into_buffer(self) -> Vec<u8> { self.buffer } | ||
| } | ||
|
|
||
| /// Container for a fully re-assembled message payload. | ||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub struct ReassembledMessage { | ||
| message_id: MessageId, | ||
| payload: Vec<u8>, | ||
| } | ||
|
|
||
| impl ReassembledMessage { | ||
| /// Construct a new [`ReassembledMessage`]. | ||
| #[must_use] | ||
| pub fn new(message_id: MessageId, payload: Vec<u8>) -> Self { | ||
| Self { | ||
| message_id, | ||
| payload, | ||
| } | ||
| } | ||
|
|
||
| /// Identifier shared by the fragments that formed this message. | ||
| #[must_use] | ||
| pub const fn message_id(&self) -> MessageId { self.message_id } | ||
|
|
||
| /// Borrow the re-assembled payload. | ||
| #[must_use] | ||
| pub fn payload(&self) -> &[u8] { self.payload.as_slice() } | ||
|
|
||
| /// Consume the message, returning the owned payload bytes. | ||
| #[must_use] | ||
| pub fn into_payload(self) -> Vec<u8> { self.payload } | ||
|
|
||
| /// Decode the payload into a strongly typed message. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns any [`DecodeError`] raised while deserialising the payload. | ||
| pub fn decode<M: Message>(&self) -> Result<M, DecodeError> { | ||
| let (message, _) = M::from_bytes(self.payload())?; | ||
| Ok(message) | ||
| } | ||
| } | ||
|
|
||
| /// Stateful fragment re-assembler with timeout-based eviction. | ||
| #[derive(Debug)] | ||
| pub struct Reassembler { | ||
| max_message_size: NonZeroUsize, | ||
| timeout: Duration, | ||
| buffers: HashMap<MessageId, PartialMessage>, | ||
| } | ||
|
|
||
| impl Reassembler { | ||
| /// Create a new re-assembler that enforces a maximum reconstructed payload size. | ||
| #[must_use] | ||
| pub fn new(max_message_size: NonZeroUsize, timeout: Duration) -> Self { | ||
| Self { | ||
| max_message_size, | ||
| timeout, | ||
| buffers: HashMap::new(), | ||
| } | ||
| } | ||
|
|
||
| /// Process a fragment using the current time. | ||
| /// | ||
| /// Returns `Ok(Some(_))` when the fragment completes the message, `Ok(None)` | ||
| /// while more fragments are required, or an error if ordering or size | ||
| /// invariants are violated. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`ReassemblyError`] when a fragment arrives out of order or would | ||
| /// push the reconstructed payload beyond the configured limit. | ||
| pub fn push( | ||
| &mut self, | ||
| header: FragmentHeader, | ||
| payload: impl AsRef<[u8]>, | ||
| ) -> Result<Option<ReassembledMessage>, ReassemblyError> { | ||
| self.push_at(header, payload, Instant::now()) | ||
| } | ||
|
|
||
| /// Process a fragment using an explicit clock reading. | ||
| /// | ||
| /// Accepting an explicit `now` simplifies deterministic testing and allows | ||
| /// callers to co-ordinate eviction sweeps with their own timers. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`ReassemblyError`] when the fragment violates ordering or | ||
| /// exceeds the configured reassembly cap. | ||
| pub fn push_at( | ||
| &mut self, | ||
| header: FragmentHeader, | ||
| payload: impl AsRef<[u8]>, | ||
| now: Instant, | ||
| ) -> Result<Option<ReassembledMessage>, ReassemblyError> { | ||
| self.purge_expired_at(now); | ||
|
|
||
| let payload = payload.as_ref(); | ||
|
|
||
| match self.buffers.entry(header.message_id()) { | ||
| Entry::Occupied(mut occupied) => { | ||
| let status = occupied | ||
| .get_mut() | ||
| .series | ||
| .accept(header) | ||
| .map_err(ReassemblyError::from); | ||
|
|
||
| match status { | ||
| Ok(FragmentStatus::Incomplete) => Self::append_and_maybe_complete( | ||
| self.max_message_size, | ||
| occupied, | ||
| header.message_id(), | ||
| payload, | ||
| false, | ||
| ), | ||
| Ok(FragmentStatus::Complete) => Self::append_and_maybe_complete( | ||
| self.max_message_size, | ||
| occupied, | ||
| header.message_id(), | ||
| payload, | ||
| true, | ||
| ), | ||
| Err(err) => { | ||
| occupied.remove(); | ||
| Err(err) | ||
| } | ||
| } | ||
| } | ||
| Entry::Vacant(vacant) => { | ||
| let mut series = FragmentSeries::new(header.message_id()); | ||
| let status = series.accept(header).map_err(ReassemblyError::from)?; | ||
| let attempted = payload.len(); | ||
| Self::assert_within_limit(self.max_message_size, header.message_id(), attempted)?; | ||
|
|
||
| match status { | ||
| FragmentStatus::Incomplete => { | ||
| vacant.insert(PartialMessage::new(series, payload, now)); | ||
| Ok(None) | ||
| } | ||
| FragmentStatus::Complete => Ok(Some(ReassembledMessage::new( | ||
| header.message_id(), | ||
| payload.to_vec(), | ||
| ))), | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Remove any partial messages that exceeded the configured timeout. | ||
| /// | ||
| /// Returns the identifiers of messages that were evicted. | ||
| pub fn purge_expired(&mut self) -> Vec<MessageId> { self.purge_expired_at(Instant::now()) } | ||
|
|
||
| /// Remove any partial messages that exceeded the configured timeout using | ||
| /// an explicit clock reading. | ||
| /// | ||
| /// Returns the identifiers of messages that were evicted. | ||
| pub fn purge_expired_at(&mut self, now: Instant) -> Vec<MessageId> { | ||
| let mut evicted = Vec::new(); | ||
| let timeout = self.timeout; | ||
|
|
||
| self.buffers.retain(|message_id, partial| { | ||
| let expired = now.saturating_duration_since(partial.started_at()) >= timeout; | ||
| if expired { | ||
| evicted.push(*message_id); | ||
| } | ||
| !expired | ||
| }); | ||
|
|
||
| evicted | ||
| } | ||
|
|
||
| /// Number of partial messages currently buffered. | ||
| #[must_use] | ||
| pub fn buffered_len(&self) -> usize { self.buffers.len() } | ||
|
|
||
| fn assert_within_limit( | ||
| limit: NonZeroUsize, | ||
| message_id: MessageId, | ||
| attempted: usize, | ||
| ) -> Result<(), ReassemblyError> { | ||
| if attempted > limit.get() { | ||
| return Err(ReassemblyError::MessageTooLarge { | ||
| message_id, | ||
| attempted, | ||
| limit, | ||
| }); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn append_and_maybe_complete( | ||
| limit: NonZeroUsize, | ||
| mut occupied: OccupiedEntry<'_, MessageId, PartialMessage>, | ||
| message_id: MessageId, | ||
| payload: &[u8], | ||
| completes: bool, | ||
| ) -> Result<Option<ReassembledMessage>, ReassemblyError> { | ||
| let Some(attempted) = occupied.get().len().checked_add(payload.len()) else { | ||
| occupied.remove(); | ||
| return Err(ReassemblyError::MessageTooLarge { | ||
| message_id, | ||
| attempted: usize::MAX, | ||
| limit, | ||
| }); | ||
| }; | ||
| if let Err(err) = Self::assert_within_limit(limit, message_id, attempted) { | ||
| occupied.remove(); | ||
| return Err(err); | ||
| } | ||
|
|
||
| occupied.get_mut().push(payload); | ||
| if completes { | ||
| let buffer = occupied.remove().into_buffer(); | ||
| Ok(Some(ReassembledMessage::new(message_id, buffer))) | ||
| } else { | ||
| Ok(None) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.