// We don't want to send WINDOW_UPDATE frames for tiny changes, but instead // aggregate them when the changes are significant. Many implementations do // this by keeping a "ratio" of the update version the allowed window size. // // While some may wish to represent this ratio as percentage, using a f32, // we skip having to deal with float math and stick to integers. To do so, // the "ratio" is represented by 2 i32s, split into the numerator and // denominator. For example, a 50% ratio is simply represented as 1/2. // // An example applying this ratio: If a stream has an allowed window size of // 100 bytes, WINDOW_UPDATE frames are scheduled when the unclaimed change // becomes greater than 1/2, or 50 bytes. const UNCLAIMED_NUMERATOR: i32 = 1; const UNCLAIMED_DENOMINATOR: i32 = 2;
#[derive(Copy, Clone, Debug)] pubstruct FlowControl { /// Window the peer knows about. /// /// This can go negative if a SETTINGS_INITIAL_WINDOW_SIZE is received. /// /// For example, say the peer sends a request and uses 32kb of the window. /// We send a SETTINGS_INITIAL_WINDOW_SIZE of 16kb. The peer has to adjust /// its understanding of the capacity of the window, and that would be: /// /// ```notrust /// default (64kb) - used (32kb) - settings_diff (64kb - 16kb): -16kb /// ```
window_size: Window,
/// Window that we know about. /// /// This can go negative if a user declares a smaller target window than /// the peer knows about.
available: Window,
}
/// If a WINDOW_UPDATE frame should be sent, returns a positive number /// representing the increment to be used. /// /// If there is no available bytes to be reclaimed, or the number of /// available bytes does not reach the threshold, this returns `None`. /// /// This represents pending outbound WINDOW_UPDATE frames. pubfn unclaimed_capacity(&self) -> Option<WindowSize> { let available = self.available;
ifself.window_size >= available { return None;
}
let unclaimed = available.0 - self.window_size.0; let threshold = self.window_size.0 / UNCLAIMED_DENOMINATOR * UNCLAIMED_NUMERATOR;
if unclaimed < threshold {
None
} else {
Some(unclaimed as WindowSize)
}
}
/// Increase the window size. /// /// This is called after receiving a WINDOW_UPDATE frame pubfn inc_window(&mutself, sz: WindowSize) -> Result<(), Reason> { let (val, overflow) = self.window_size.0.overflowing_add(sz as i32);
if overflow { return Err(Reason::FLOW_CONTROL_ERROR);
}
if val > MAX_WINDOW_SIZE as i32 { return Err(Reason::FLOW_CONTROL_ERROR);
}
tracing::trace!( "inc_window; sz={}; old={}; new={}",
sz, self.window_size,
val
);
self.window_size = Window(val);
Ok(())
}
/// Decrement the send-side window size. /// /// This is called after receiving a SETTINGS frame with a lower /// INITIAL_WINDOW_SIZE value. pubfn dec_send_window(&mutself, sz: WindowSize) -> Result<(), Reason> {
tracing::trace!( "dec_window; sz={}; window={}, available={}",
sz, self.window_size, self.available
); // ~~This should not be able to overflow `window_size` from the bottom.~~ wrong. it can. self.window_size.decrease_by(sz)?;
Ok(())
}
/// Decrement the recv-side window size. /// /// This is called after receiving a SETTINGS ACK frame with a lower /// INITIAL_WINDOW_SIZE value. pubfn dec_recv_window(&mutself, sz: WindowSize) -> Result<(), Reason> {
tracing::trace!( "dec_recv_window; sz={}; window={}, available={}",
sz, self.window_size, self.available
); // This should not be able to overflow `window_size` from the bottom. self.window_size.decrease_by(sz)?; self.available.decrease_by(sz)?;
Ok(())
}
/// Decrements the window reflecting data has actually been sent. The caller /// must ensure that the window has capacity. pubfn send_data(&mutself, sz: WindowSize) -> Result<(), Reason> {
tracing::trace!( "send_data; sz={}; window={}; available={}",
sz, self.window_size, self.available
);
// If send size is zero it's meaningless to update flow control window if sz > 0 { // Ensure that the argument is correct
assert!(self.window_size.0 >= sz as i32);
/// The current capacity of a flow-controlled Window. /// /// This number can go negative when either side has used a certain amount /// of capacity when the other side advertises a reduction in size. /// /// This type tries to centralize the knowledge of addition and subtraction /// to this capacity, instead of having integer casts throughout the source. #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd)] pubstruct Window(i32);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.