use crate::{Duration, RwndDecision, RwndTrace};
use dyn_clone::DynClone;
#[cfg_attr(feature = "serde", typetag::serde)]
pub trait RwndTraceConfig: DynClone + Send {
fn into_model(self: Box<Self>) -> Box<dyn RwndTrace>;
}
dyn_clone::clone_trait_object!(RwndTraceConfig);
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct StaticRwnd {
pub decision: RwndDecision,
pub duration: Option<Duration>,
}
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(default, deny_unknown_fields)
)]
#[derive(Debug, Clone, Default)]
pub struct StaticRwndConfig {
#[cfg_attr(
feature = "human",
serde(with = "humantime_serde"),
serde(skip_serializing_if = "Option::is_none")
)]
#[cfg_attr(
all(feature = "serde", not(feature = "human")),
serde(skip_serializing_if = "Option::is_none")
)]
pub duration: Option<Duration>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub set_rcv_buf: Option<u64>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub app_read_bytes: Option<u64>,
}
pub struct RepeatedRwndPattern {
pub pattern: Vec<Box<dyn RwndTraceConfig>>,
pub count: usize,
current_model: Option<Box<dyn RwndTrace>>,
current_cycle: usize,
current_pattern: usize,
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(default))]
#[derive(Default, Clone)]
pub struct RepeatedRwndPatternConfig {
pub pattern: Vec<Box<dyn RwndTraceConfig>>,
pub count: usize,
}
impl RwndTrace for StaticRwnd {
fn next_rwnd(&mut self) -> Option<(RwndDecision, Duration)> {
if let Some(duration) = self.duration.take() {
if duration.is_zero() {
None
} else {
Some((self.decision, duration))
}
} else {
None
}
}
}
impl RwndTrace for RepeatedRwndPattern {
fn next_rwnd(&mut self) -> Option<(RwndDecision, Duration)> {
let pattern_len = self.pattern.len();
let mut budget = pattern_len + 1;
loop {
if pattern_len == 0 || (self.count != 0 && self.current_cycle >= self.count) {
return None;
}
if budget == 0 {
return None;
}
if self.current_model.is_none() {
self.current_model = Some(self.pattern[self.current_pattern].clone().into_model());
}
match self.current_model.as_mut().unwrap().next_rwnd() {
Some(item) => return Some(item),
None => {
self.current_model = None;
budget -= 1;
self.current_pattern += 1;
if self.current_pattern >= pattern_len {
self.current_pattern = 0;
self.current_cycle += 1;
if self.count != 0 && self.current_cycle >= self.count {
return None;
}
}
}
}
}
}
}
impl StaticRwndConfig {
pub fn new() -> Self {
Self {
duration: None,
set_rcv_buf: None,
app_read_bytes: None,
}
}
pub fn duration(mut self, duration: Duration) -> Self {
self.duration = Some(duration);
self
}
pub fn set_rcv_buf(mut self, set_rcv_buf: u64) -> Self {
self.set_rcv_buf = Some(set_rcv_buf);
self
}
pub fn app_read(mut self, bytes: u64) -> Self {
self.app_read_bytes = Some(bytes);
self
}
pub fn build(self) -> StaticRwnd {
StaticRwnd {
decision: RwndDecision {
set_rcv_buf: self.set_rcv_buf,
app_read_bytes: self.app_read_bytes,
},
duration: Some(self.duration.unwrap_or_else(|| Duration::from_secs(1))),
}
}
}
impl RepeatedRwndPatternConfig {
pub fn new() -> Self {
Self {
pattern: vec![],
count: 0,
}
}
pub fn pattern(mut self, pattern: Vec<Box<dyn RwndTraceConfig>>) -> Self {
self.pattern = pattern;
self
}
pub fn count(mut self, count: usize) -> Self {
self.count = count;
self
}
pub fn build(self) -> RepeatedRwndPattern {
RepeatedRwndPattern {
pattern: self.pattern,
count: self.count,
current_model: None,
current_cycle: 0,
current_pattern: 0,
}
}
}
macro_rules! impl_rwnd_trace_config {
($name:ident) => {
#[cfg_attr(feature = "serde", typetag::serde)]
impl RwndTraceConfig for $name {
fn into_model(self: Box<$name>) -> Box<dyn RwndTrace> {
Box::new(self.build())
}
}
};
}
impl_rwnd_trace_config!(StaticRwndConfig);
impl_rwnd_trace_config!(RepeatedRwndPatternConfig);
#[cfg(test)]
mod test {
use super::*;
use crate::RwndTrace;
#[test]
fn test_static_rwnd_model_buffer_and_app_read() {
let mut static_rwnd = StaticRwndConfig::new()
.set_rcv_buf(65536)
.app_read(1024)
.duration(Duration::from_secs(1))
.build();
let (decision, duration) = static_rwnd.next_rwnd().unwrap();
assert_eq!(decision.set_rcv_buf, Some(65536));
assert_eq!(decision.app_read_bytes, Some(1024));
assert_eq!(duration, Duration::from_secs(1));
assert_eq!(static_rwnd.next_rwnd(), None);
}
#[test]
fn test_static_rwnd_model_buffer_only() {
let mut static_rwnd = StaticRwndConfig::new()
.set_rcv_buf(32768)
.duration(Duration::from_secs(2))
.build();
let (decision, duration) = static_rwnd.next_rwnd().unwrap();
assert_eq!(decision.set_rcv_buf, Some(32768));
assert_eq!(decision.app_read_bytes, None);
assert_eq!(duration, Duration::from_secs(2));
assert_eq!(static_rwnd.next_rwnd(), None);
}
#[test]
fn test_static_rwnd_model_carries_forward() {
let mut model = StaticRwndConfig::new()
.duration(Duration::from_secs(1))
.build();
let (decision, duration) = model.next_rwnd().unwrap();
assert_eq!(decision.set_rcv_buf, None);
assert_eq!(decision.app_read_bytes, None);
assert_eq!(duration, Duration::from_secs(1));
}
#[test]
fn test_repeated_rwnd_pattern() {
let pat = vec![
Box::new(
StaticRwndConfig::new()
.set_rcv_buf(65536)
.duration(Duration::from_secs(1)),
) as Box<dyn RwndTraceConfig>,
Box::new(
StaticRwndConfig::new()
.app_read(1024)
.duration(Duration::from_secs(1)),
) as Box<dyn RwndTraceConfig>,
];
let mut model = RepeatedRwndPatternConfig::new()
.pattern(pat)
.count(2)
.build();
let next = model.next_rwnd().unwrap();
assert_eq!(next.0.set_rcv_buf, Some(65536));
assert_eq!(next.1, Duration::from_secs(1));
assert_eq!(model.next_rwnd().unwrap().0.app_read_bytes, Some(1024));
assert_eq!(model.next_rwnd().unwrap().0.set_rcv_buf, Some(65536));
assert_eq!(model.next_rwnd().unwrap().0.app_read_bytes, Some(1024));
assert_eq!(model.next_rwnd(), None);
}
#[test]
#[cfg(feature = "serde")]
fn test_serde_roundtrip_buffer_only() {
let cfg = Box::new(
StaticRwndConfig::new()
.set_rcv_buf(65536)
.duration(Duration::from_secs(1)),
) as Box<dyn RwndTraceConfig>;
let ser_str = serde_json::to_string(&cfg).unwrap();
#[cfg(feature = "human")]
let expected = "{\"StaticRwndConfig\":{\"duration\":\"1s\",\"set_rcv_buf\":65536}}";
#[cfg(not(feature = "human"))]
let expected =
"{\"StaticRwndConfig\":{\"duration\":{\"secs\":1,\"nanos\":0},\"set_rcv_buf\":65536}}";
assert_eq!(ser_str, expected);
let des: Box<dyn RwndTraceConfig> = serde_json::from_str(&ser_str).unwrap();
let mut model = des.into_model();
let (decision, duration) = model.next_rwnd().unwrap();
assert_eq!(decision.set_rcv_buf, Some(65536));
assert_eq!(decision.app_read_bytes, None);
assert_eq!(duration, Duration::from_secs(1));
}
#[test]
#[cfg(feature = "serde")]
fn test_serde_roundtrip_both_fields() {
let json = "{\"StaticRwndConfig\":{\"set_rcv_buf\":131072,\"app_read_bytes\":4096}}";
let des: Box<dyn RwndTraceConfig> = serde_json::from_str(json).unwrap();
let mut model = des.into_model();
let (decision, _) = model.next_rwnd().unwrap();
assert_eq!(decision.set_rcv_buf, Some(131072));
assert_eq!(decision.app_read_bytes, Some(4096));
}
#[test]
#[cfg(feature = "serde")]
fn test_serde_omits_absent_fields() {
let cfg = Box::new(StaticRwndConfig::new().app_read(0)) as Box<dyn RwndTraceConfig>;
let ser_str = serde_json::to_string(&cfg).unwrap();
assert!(!ser_str.contains("set_rcv_buf"), "got: {ser_str}");
assert!(ser_str.contains("app_read_bytes"), "got: {ser_str}");
}
#[test]
#[cfg(feature = "serde")]
fn test_serde_rejects_the_old_rwnd_remaining_field() {
let json = "{\"StaticRwndConfig\":{\"rwnd_remaining\":32768}}";
let result: Result<Box<dyn RwndTraceConfig>, _> = serde_json::from_str(json);
let err = result
.err()
.expect("a trace using the removed field should not deserialize")
.to_string();
assert!(err.contains("rwnd_remaining"), "got: {err}");
}
#[test]
fn test_repeated_rwnd_pattern_all_zero_duration_terminates() {
let pat = vec![
Box::new(
StaticRwndConfig::new()
.app_read(1024)
.duration(Duration::ZERO),
) as Box<dyn RwndTraceConfig>,
Box::new(
StaticRwndConfig::new()
.set_rcv_buf(32768)
.duration(Duration::ZERO),
) as Box<dyn RwndTraceConfig>,
];
let mut model = RepeatedRwndPatternConfig::new()
.pattern(pat)
.count(0) .build();
assert_eq!(model.next_rwnd(), None);
}
}