reth_network/
budget.rs

1/// Default budget to try and drain streams.
2///
3/// Default is 10 iterations.
4pub const DEFAULT_BUDGET_TRY_DRAIN_STREAM: u32 = 10;
5
6/// Default budget to try and drain headers and bodies download streams.
7///
8/// Default is 2 iterations.
9pub const DEFAULT_BUDGET_TRY_DRAIN_DOWNLOADERS: u32 = 2;
10
11/// Default budget to try and drain [`Swarm`](crate::swarm::Swarm).
12///
13/// Default is 10 [`SwarmEvent`](crate::swarm::SwarmEvent)s.
14pub const DEFAULT_BUDGET_TRY_DRAIN_SWARM: u32 = 10;
15
16/// Default budget to try and drain pending messages from [`NetworkHandle`](crate::NetworkHandle)
17/// channel. Polling the [`TransactionsManager`](crate::transactions::TransactionsManager) future
18/// sends these types of messages.
19//
20// Default is 40 outgoing transaction messages.
21pub const DEFAULT_BUDGET_TRY_DRAIN_NETWORK_HANDLE_CHANNEL: u32 =
22    4 * DEFAULT_BUDGET_TRY_DRAIN_STREAM;
23
24/// Default budget to try and drain stream of
25/// [`NetworkTransactionEvent`](crate::transactions::NetworkTransactionEvent)s from
26/// [`NetworkManager`](crate::NetworkManager).
27///
28/// Default is 10 incoming transaction messages.
29pub const DEFAULT_BUDGET_TRY_DRAIN_NETWORK_TRANSACTION_EVENTS: u32 = DEFAULT_BUDGET_TRY_DRAIN_SWARM;
30
31/// Default budget to try and flush pending pool imports to pool. This number reflects the number
32/// of transactions that can be queued for import to pool in each iteration of the loop in the
33/// [`TransactionsManager`](crate::transactions::TransactionsManager) future.
34//
35// Default is 40 pending pool imports.
36pub const DEFAULT_BUDGET_TRY_DRAIN_PENDING_POOL_IMPORTS: u32 = 4 * DEFAULT_BUDGET_TRY_DRAIN_STREAM;
37
38/// Polls the given stream. Breaks with `true` if there maybe is more work.
39#[macro_export]
40macro_rules! poll_nested_stream_with_budget {
41    ($target:literal, $label:literal, $budget:ident, $poll_stream:expr, $on_ready_some:expr $(, $on_ready_none:expr;)? $(,)?) => {{
42        let mut budget: u32 = $budget;
43
44            loop {
45                match $poll_stream {
46                    Poll::Ready(Some(item)) => {
47                        $on_ready_some(item);
48
49                        budget -= 1;
50                        if budget == 0 {
51                            break true
52                        }
53                    }
54                    Poll::Ready(None) => {
55                        $($on_ready_none;)? // todo: handle error case with $target and $label
56                        break false
57                    }
58                    Poll::Pending => break false,
59                }
60            }
61    }};
62}
63
64/// Metered poll of the given stream. Breaks with `true` if there maybe is more work.
65#[macro_export]
66macro_rules! metered_poll_nested_stream_with_budget {
67    ($acc:expr, $target:literal, $label:literal, $budget:ident, $poll_stream:expr, $on_ready_some:expr $(, $on_ready_none:expr;)? $(,)?) => {{
68        $crate::duration_metered_exec!(
69            {
70                $crate::poll_nested_stream_with_budget!($target, $label, $budget, $poll_stream, $on_ready_some $(, $on_ready_none;)?)
71            },
72            $acc
73        )
74    }};
75}