proto: Fix double-boxing of congestion::ControllerFactory

Also allows callers who already have a trait object to pass it on
without yet more boxing.
This commit is contained in:
Benjamin Saunders
2023-08-20 16:21:32 -07:00
parent 6d4830eaef
commit 33fa6bb24d
6 changed files with 17 additions and 14 deletions
+4 -4
View File
@@ -50,7 +50,7 @@ pub struct TransportConfig {
#[cfg(test)]
pub(crate) deterministic_packet_numbers: bool,
pub(crate) congestion_controller_factory: Box<dyn congestion::ControllerFactory + Send + Sync>,
pub(crate) congestion_controller_factory: Arc<dyn congestion::ControllerFactory + Send + Sync>,
pub(crate) enable_segmentation_offload: bool,
}
@@ -293,9 +293,9 @@ impl TransportConfig {
/// ```
pub fn congestion_controller_factory(
&mut self,
factory: impl congestion::ControllerFactory + Send + Sync + 'static,
factory: Arc<dyn congestion::ControllerFactory + Send + Sync + 'static>,
) -> &mut Self {
self.congestion_controller_factory = Box::new(factory);
self.congestion_controller_factory = factory;
self
}
@@ -349,7 +349,7 @@ impl Default for TransportConfig {
#[cfg(test)]
deterministic_packet_numbers: false,
congestion_controller_factory: Box::new(Arc::new(congestion::CubicConfig::default())),
congestion_controller_factory: Arc::new(congestion::CubicConfig::default()),
enable_segmentation_offload: true,
}
+2 -1
View File
@@ -2,6 +2,7 @@
use crate::connection::RttEstimator;
use std::any::Any;
use std::sync::Arc;
use std::time::Instant;
mod bbr;
@@ -77,7 +78,7 @@ pub trait Controller: Send {
/// Constructs controllers on demand
pub trait ControllerFactory {
/// Construct a fresh `Controller`
fn build(&self, now: Instant, current_mtu: u16) -> Box<dyn Controller>;
fn build(self: Arc<Self>, now: Instant, current_mtu: u16) -> Box<dyn Controller>;
}
const BASE_DATAGRAM_SIZE: u64 = 1200;
+3 -3
View File
@@ -522,9 +522,9 @@ impl Default for BbrConfig {
}
}
impl ControllerFactory for Arc<BbrConfig> {
fn build(&self, _now: Instant, current_mtu: u16) -> Box<dyn Controller> {
Box::new(Bbr::new(self.clone(), current_mtu))
impl ControllerFactory for BbrConfig {
fn build(self: Arc<Self>, _now: Instant, current_mtu: u16) -> Box<dyn Controller> {
Box::new(Bbr::new(self, current_mtu))
}
}
+3 -3
View File
@@ -259,8 +259,8 @@ impl Default for CubicConfig {
}
}
impl ControllerFactory for Arc<CubicConfig> {
fn build(&self, now: Instant, current_mtu: u16) -> Box<dyn Controller> {
Box::new(Cubic::new(self.clone(), now, current_mtu))
impl ControllerFactory for CubicConfig {
fn build(self: Arc<Self>, now: Instant, current_mtu: u16) -> Box<dyn Controller> {
Box::new(Cubic::new(self, now, current_mtu))
}
}
+3 -3
View File
@@ -157,8 +157,8 @@ impl Default for NewRenoConfig {
}
}
impl ControllerFactory for Arc<NewRenoConfig> {
fn build(&self, now: Instant, current_mtu: u16) -> Box<dyn Controller> {
Box::new(NewReno::new(self.clone(), now, current_mtu))
impl ControllerFactory for NewRenoConfig {
fn build(self: Arc<Self>, now: Instant, current_mtu: u16) -> Box<dyn Controller> {
Box::new(NewReno::new(self, now, current_mtu))
}
}
+2
View File
@@ -281,6 +281,7 @@ impl Connection {
config.initial_rtt,
config
.congestion_controller_factory
.clone()
.build(now, config.get_initial_mtu()),
config.get_initial_mtu(),
config.min_mtu,
@@ -2873,6 +2874,7 @@ impl Connection {
self.config.initial_rtt,
self.config
.congestion_controller_factory
.clone()
.build(now, self.config.get_initial_mtu()),
self.config.get_initial_mtu(),
self.config.min_mtu,