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/// Default budget to try and stream hashes of successfully imported transactions from the pool.
39///
40/// Default is naturally same as the number of transactions to attempt importing,
41/// [`DEFAULT_BUDGET_TRY_DRAIN_PENDING_POOL_IMPORTS`], so 40 pool imports.
42pub const DEFAULT_BUDGET_TRY_DRAIN_POOL_IMPORTS: u32 =
43    DEFAULT_BUDGET_TRY_DRAIN_PENDING_POOL_IMPORTS;
44
45/// Polls the given stream. Breaks with `true` if there maybe is more work.
46#[macro_export]
47macro_rules! poll_nested_stream_with_budget {
48    ($target:literal, $label:literal, $budget:ident, $poll_stream:expr, $on_ready_some:expr $(, $on_ready_none:expr;)? $(,)?) => {{
49        let mut budget: u32 = $budget;
50
51            loop {
52                match $poll_stream {
53                    Poll::Ready(Some(item)) => {
54                        $on_ready_some(item);
55
56                        budget -= 1;
57                        if budget == 0 {
58                            break true
59                        }
60                    }
61                    Poll::Ready(None) => {
62                        $($on_ready_none;)? // todo: handle error case with $target and $label
63                        break false
64                    }
65                    Poll::Pending => break false,
66                }
67            }
68    }};
69}
70
71/// Metered poll of the given stream. Breaks with `true` if there maybe is more work.
72#[macro_export]
73macro_rules! metered_poll_nested_stream_with_budget {
74    ($acc:expr, $target:literal, $label:literal, $budget:ident, $poll_stream:expr, $on_ready_some:expr $(, $on_ready_none:expr;)? $(,)?) => {{
75        $crate::duration_metered_exec!(
76            {
77                $crate::poll_nested_stream_with_budget!($target, $label, $budget, $poll_stream, $on_ready_some $(, $on_ready_none;)?)
78            },
79            $acc
80        )
81    }};
82}