/src/ztunnel/src/assertions.rs
Line | Count | Source |
1 | | // Copyright Istio Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | // Below helper functions are used to help make about the size of types. |
16 | | // There are some compile time ways to do this, but they don't work in the way we need for the most part; |
17 | | // analyzing the size of Futures which we don't have explicit declarations for. |
18 | | // Future size is determined by the max required stack size for the async function. This means deeply |
19 | | // branched code can create huge Future's, leading to high per-connection memory usage in ztunnel. |
20 | | // Debugging these usages can be done by `RUSTFLAGS=-Zprint-type-sizes cargo +nightly build -j 1`, |
21 | | // or by logging with the functions below. |
22 | | |
23 | | #[cfg(all(any(test, feature = "testing"), debug_assertions))] |
24 | | pub fn size_between_ref<T>(min: usize, max: usize, t: &T) { |
25 | | let size = std::mem::size_of_val(t); |
26 | | if size < min || size > max { |
27 | | // If it is too small: that is good, we just want to update the assertion to be more aggressive |
28 | | // If it is too big: that is bad. We may need to increase the limit, or consider refactors. |
29 | | panic!( |
30 | | "type {} size is unexpected, wanted {min}..{max}, got {size}", |
31 | | std::any::type_name::<T>(), |
32 | | ) |
33 | | } |
34 | | tracing::trace!( |
35 | | "type {} size is within expectations, wanted {min}..{max}, got {size}", |
36 | | std::any::type_name::<T>(), |
37 | | ) |
38 | | } |
39 | | |
40 | | #[cfg(not(all(any(test, feature = "testing"), debug_assertions)))] |
41 | 0 | pub fn size_between_ref<T>(_min: usize, _max: usize, _t: &T) {}Unexecuted instantiation: ztunnel::assertions::size_between_ref::<tracing::instrument::Instrumented<<ztunnel::proxy::inbound_passthrough::InboundPassthrough>::run::{closure#0}::{closure#0}::{closure#0}::{closure#0}>>Unexecuted instantiation: ztunnel::assertions::size_between_ref::<tracing::instrument::Instrumented<<ztunnel::proxy::socks5::Socks5>::run::{closure#0}::{closure#0}::{closure#0}::{closure#0}>>Unexecuted instantiation: ztunnel::assertions::size_between_ref::<tracing::instrument::Instrumented<<ztunnel::proxy::outbound::Outbound>::run::{closure#0}::{closure#0}::{closure#0}::{closure#0}>>Unexecuted instantiation: ztunnel::assertions::size_between_ref::<tracing::instrument::Instrumented<<ztunnel::proxy::inbound::Inbound>::serve_connect::{closure#0}>>Unexecuted instantiation: ztunnel::assertions::size_between_ref::<ztunnel::proxy::h2::server::serve_connection<<ztunnel::proxy::inbound::Inbound>::run::{closure#0}::{closure#0}::{closure#0}::{closure#0}::{closure#0}, tracing::instrument::Instrumented<<ztunnel::proxy::inbound::Inbound>::serve_connect::{closure#0}>>::{closure#0}>Unexecuted instantiation: ztunnel::assertions::size_between_ref::<<ztunnel::proxy::inbound::Inbound>::run::{closure#0}::{closure#0}::{closure#0}::{closure#0}> |
42 | | |
43 | | #[inline(always)] |
44 | 0 | pub fn size_between<T>(min: usize, max: usize, t: T) -> T { |
45 | 0 | size_between_ref(min, max, &t); |
46 | 0 | t |
47 | 0 | } |