Skip to content
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

Use budgeted poll macro #11649

Closed
emhane opened this issue Oct 10, 2024 · 3 comments
Closed

Use budgeted poll macro #11649

emhane opened this issue Oct 10, 2024 · 3 comments
Labels
A-networking Related to networking in general C-debt Refactor of code section that is hard to understand or maintain D-good-first-issue Nice and easy! A great choice to get started

Comments

@emhane
Copy link
Member

emhane commented Oct 10, 2024

Describe the feature

Use existing pattern for budgeted poll of sub streams + remove magic number for budget in #11647

For example as has been done in the transactions manager

let maybe_more_pool_imports = metered_poll_nested_stream_with_budget!(
poll_durations.acc_pending_imports,
"net::tx",
"Batched pool imports stream",
DEFAULT_BUDGET_TRY_DRAIN_PENDING_POOL_IMPORTS,
this.pool_imports.poll_next_unpin(cx),
|batch_results| this.on_batch_import_result(batch_results)
);

A macro without use of metrics also exists

/// Polls the given stream. Breaks with `true` if there maybe is more work.
#[macro_export]
macro_rules! poll_nested_stream_with_budget {
($target:literal, $label:literal, $budget:ident, $poll_stream:expr, $on_ready_some:expr $(, $on_ready_none:expr;)? $(,)?) => {{
let mut budget: u32 = $budget;
loop {
match $poll_stream {
Poll::Ready(Some(item)) => {
#[allow(unused_mut)]
let mut f = $on_ready_some;
f(item);
budget = budget.saturating_sub(1);
if budget == 0 {
break true
}
}
Poll::Ready(None) => {
$($on_ready_none;)? // todo: handle error case with $target and $label
break false
}
Poll::Pending => break false,
}
}
}};
}

Additional context

No response

@emhane emhane added D-good-first-issue Nice and easy! A great choice to get started C-debt Refactor of code section that is hard to understand or maintain A-networking Related to networking in general labels Oct 10, 2024
@Rjected
Copy link
Member

Rjected commented Oct 10, 2024

rather than a macro, could we extract the macro functionality to a struct somehow? macros can be hard to debug / make it harder to read and understand what's actually going on

@ayoubed
Copy link

ayoubed commented Oct 10, 2024

I can take this.

@mattsse
Copy link
Collaborator

mattsse commented Oct 14, 2024

I think we have this everywhere where we can/want to use this

@mattsse mattsse closed this as completed Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-networking Related to networking in general C-debt Refactor of code section that is hard to understand or maintain D-good-first-issue Nice and easy! A great choice to get started
Projects
Archived in project
Development

No branches or pull requests

4 participants