Skip to content

Commit 6a1bd05

Browse files
committed
refactor(http2): store bdp sampler in Body H2 variant
1 parent a82fd6c commit 6a1bd05

File tree

3 files changed

+7
-20
lines changed

3 files changed

+7
-20
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ futures-util = { version = "0.3", default-features = false }
2727
http = "0.2"
2828
http-body = "0.3.1"
2929
httparse = "1.0"
30-
h2 = "0.2.1"
30+
h2 = "0.2.2"
3131
itoa = "0.4.1"
3232
log = "0.4"
3333
pin-project = "0.4"

src/body/body.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum Kind {
3838
rx: mpsc::Receiver<Result<Bytes, crate::Error>>,
3939
},
4040
H2 {
41+
bdp: bdp::Sampler,
4142
content_length: DecodedLength,
4243
recv: h2::RecvStream,
4344
},
@@ -62,11 +63,6 @@ struct Extra {
6263
/// connection yet.
6364
delayed_eof: Option<DelayEof>,
6465
on_upgrade: OnUpgrade,
65-
66-
/// Records bytes read to compute the BDP.
67-
///
68-
/// Only used with `H2` variant.
69-
h2_bdp: bdp::Sampler,
7066
}
7167

7268
type DelayEofUntil = oneshot::Receiver<Never>;
@@ -186,15 +182,12 @@ impl Body {
186182
content_length: DecodedLength,
187183
bdp: bdp::Sampler,
188184
) -> Self {
189-
let mut body = Body::new(Kind::H2 {
185+
let body = Body::new(Kind::H2 {
186+
bdp,
190187
content_length,
191188
recv,
192189
});
193190

194-
if bdp.is_enabled() {
195-
body.extra_mut().h2_bdp = bdp;
196-
}
197-
198191
body
199192
}
200193

@@ -220,7 +213,6 @@ impl Body {
220213
Box::new(Extra {
221214
delayed_eof: None,
222215
on_upgrade: OnUpgrade::none(),
223-
h2_bdp: bdp::disabled(),
224216
})
225217
})
226218
}
@@ -273,15 +265,14 @@ impl Body {
273265
}
274266
}
275267
Kind::H2 {
268+
ref bdp,
276269
recv: ref mut h2,
277270
content_length: ref mut len,
278271
} => match ready!(h2.poll_data(cx)) {
279272
Some(Ok(bytes)) => {
280273
let _ = h2.flow_control().release_capacity(bytes.len());
281274
len.sub_if(bytes.len() as u64);
282-
if let Some(ref extra) = self.extra {
283-
extra.h2_bdp.sample(bytes.len());
284-
}
275+
bdp.sample(bytes.len());
285276
Poll::Ready(Some(Ok(bytes)))
286277
}
287278
Some(Err(e)) => Poll::Ready(Some(Err(crate::Error::new_body(e)))),

src/proto/h2/bdp.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type WindowSize = u32;
3030
/// Any higher than this likely will be hitting the TCP flow control.
3131
const BDP_LIMIT: usize = 1024 * 1024 * 16;
3232

33-
pub(crate) fn disabled() -> Sampler {
33+
pub(super) fn disabled() -> Sampler {
3434
Sampler {
3535
shared: Weak::new(),
3636
}
@@ -105,10 +105,6 @@ impl Sampler {
105105

106106
inner.bytes += bytes;
107107
}
108-
109-
pub(crate) fn is_enabled(&self) -> bool {
110-
self.shared.strong_count() > 0
111-
}
112108
}
113109

114110
impl Estimator {

0 commit comments

Comments
 (0)