-
Notifications
You must be signed in to change notification settings - Fork 0
Implement memory budget enforcement across message assembly (8.3.2) #474
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
664806a
feat(message_assembler): enforce per-message and aggregate memory bud…
leynos 7cc96db
refactor(message_assembler): introduce macro to simplify first-frame …
leynos 577429e
feat(message_assembler): enforce aggregate budgets with comprehensive…
leynos a26373f
test(fixtures): improve default budget limit to yield clearer errors
leynos 001fd22
test(message_assembler,budget_enforcement): parametrise tests for con…
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,105 @@ | ||
| //! Budget enforcement helpers for message assembly. | ||
| //! | ||
| //! This module provides aggregate budget checks applied during frame | ||
| //! acceptance. Per-message size limits are handled by the existing | ||
| //! `check_size_limit` function in [`super::state`]; this module adds | ||
| //! per-connection and in-flight aggregate budget enforcement. | ||
|
|
||
| use std::num::NonZeroUsize; | ||
|
|
||
| use super::{MessageKey, error::MessageAssemblyError}; | ||
|
|
||
| /// Paired connection and in-flight budget limits. | ||
| /// | ||
| /// Bundled into a struct so call-sites pass a single value instead of two | ||
| /// `Option<NonZeroUsize>` parameters. | ||
| #[derive(Clone, Copy, Debug)] | ||
| pub(super) struct AggregateBudgets { | ||
| pub(super) connection: Option<NonZeroUsize>, | ||
| pub(super) in_flight: Option<NonZeroUsize>, | ||
| } | ||
|
|
||
| impl AggregateBudgets { | ||
| /// Returns `true` when at least one aggregate budget limit is configured. | ||
| pub(super) const fn is_enabled(&self) -> bool { | ||
| self.connection.is_some() || self.in_flight.is_some() | ||
| } | ||
| } | ||
|
|
||
| /// Check whether accepting `additional_bytes` for `key` would exceed | ||
| /// the connection budget or in-flight budget. | ||
| /// | ||
| /// Both budgets are checked against the same `current_total` because, | ||
| /// at this layer, all buffered bytes are assembly bytes. The dimensions | ||
| /// are kept separate so future work (streaming body buffers, transport | ||
| /// buffering) can diverge them. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`MessageAssemblyError::ConnectionBudgetExceeded`] or | ||
| /// [`MessageAssemblyError::InFlightBudgetExceeded`] when the respective | ||
| /// limit would be exceeded. | ||
| pub(super) fn check_aggregate_budgets( | ||
| key: MessageKey, | ||
| current_total: usize, | ||
| additional_bytes: usize, | ||
| budgets: &AggregateBudgets, | ||
| ) -> Result<(), MessageAssemblyError> { | ||
| let new_total = current_total.saturating_add(additional_bytes); | ||
|
|
||
| if let Some(limit) = budgets.connection | ||
| && new_total > limit.get() | ||
| { | ||
| return Err(MessageAssemblyError::ConnectionBudgetExceeded { | ||
| key, | ||
| attempted: new_total, | ||
| limit, | ||
| }); | ||
| } | ||
|
|
||
| if let Some(limit) = budgets.in_flight | ||
| && new_total > limit.get() | ||
| { | ||
| return Err(MessageAssemblyError::InFlightBudgetExceeded { | ||
| key, | ||
| attempted: new_total, | ||
| limit, | ||
| }); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Check if accumulated size plus new body would exceed the per-message | ||
| /// size limit. | ||
| /// | ||
| /// Returns the new total size on success. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`MessageAssemblyError::MessageTooLarge`] when the new total | ||
| /// would exceed `max_message_size`. | ||
| pub(super) fn check_size_limit( | ||
| max_message_size: NonZeroUsize, | ||
| key: MessageKey, | ||
| accumulated: usize, | ||
| body_len: usize, | ||
| ) -> Result<usize, MessageAssemblyError> { | ||
| let Some(new_len) = accumulated.checked_add(body_len) else { | ||
| return Err(MessageAssemblyError::MessageTooLarge { | ||
| key, | ||
| attempted: usize::MAX, | ||
| limit: max_message_size, | ||
| }); | ||
| }; | ||
|
|
||
| if new_len > max_message_size.get() { | ||
| return Err(MessageAssemblyError::MessageTooLarge { | ||
| key, | ||
| attempted: new_len, | ||
| limit: max_message_size, | ||
| }); | ||
| } | ||
|
|
||
| Ok(new_len) | ||
| } |
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.