Skip to content

Commit

Permalink
fixed unused content + refactored the use of Now
Browse files Browse the repository at this point in the history
  • Loading branch information
Aperence committed Dec 12, 2023
1 parent 83d1d25 commit 67b60d8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
12 changes: 2 additions & 10 deletions src/aioquic/quic/congestion/congestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ def is_rtt_increasing(self, rtt: float, now: float) -> bool:
elif delta > 0:
self._increases = 0
return False


def Now():
return datetime.timestamp(datetime.now())


class QuicCongestionControl:

Expand All @@ -89,17 +84,14 @@ def set_recovery(self, recovery):
# recovery is a QuicPacketRecovery instance
self.recovery = recovery

def on_init(self, *args, **kwargs):
pass

def on_packet_acked(self, packet: QuicSentPacket):
def on_packet_acked(self, packet: QuicSentPacket, now : float):
if self.callback:
self.callback(CongestionEvent.ACK, self)
if type(self) == QuicCongestionControl:
# don't call this if it is a superclass that runs
self.data_in_flight -= packet.sent_bytes

def on_packet_sent(self, packet: QuicSentPacket) -> None:
def on_packet_sent(self, packet: QuicSentPacket, now : float) -> None:
if self.callback:
self.callback(CongestionEvent.PACKET_SENT, self)
if type(self) == QuicCongestionControl:
Expand Down
30 changes: 15 additions & 15 deletions src/aioquic/quic/congestion/cubic.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from .congestion import QuicCongestionControl, QuicRttMonitor, K_INITIAL_WINDOW, K_MINIMUM_WINDOW, Now
from .congestion import QuicCongestionControl, QuicRttMonitor, K_INITIAL_WINDOW, K_MINIMUM_WINDOW
from ..packet_builder import QuicSentPacket
from typing import Iterable, Optional, Dict, Any

# cubic specific variables (see https://www.rfc-editor.org/rfc/rfc9438.html#name-definitions)
K_CUBIC_K = 1
K_CUBIC_C = 0.4
K_CUBIC_LOSS_REDUCTION_FACTOR = 0.7
K_CUBIC_ADDITIVE_INCREASE = None # bytes corresponding to 1 segment
K_CUBIC_MAX_IDLE_TIME = 2 # reset the cwnd after 2 seconds of inactivity

class CubicCongestionControl(QuicCongestionControl):
Expand All @@ -16,8 +15,7 @@ class CubicCongestionControl(QuicCongestionControl):

def __init__(self, max_datagram_size : int, callback=None, reno_friendly_activated = True) -> None:
super().__init__(max_datagram_size=max_datagram_size, callback=callback)
global K_CUBIC_ADDITIVE_INCREASE
K_CUBIC_ADDITIVE_INCREASE = max_datagram_size
self.additive_increase_factor = max_datagram_size # increase by one segment

self._max_datagram_size = max_datagram_size
self.bytes_in_flight = 0
Expand All @@ -32,6 +30,8 @@ def __init__(self, max_datagram_size : int, callback=None, reno_friendly_activat

self.last_ack = None

self.now = 0

def better_cube_root(self, x):
if (x < 0):
# avoid precision errors that make the cube root returns an imaginary number
Expand Down Expand Up @@ -67,14 +67,13 @@ def reset(self):
self._cwnd_epoch = 0
self._t_epoch = 0
self._W_max = self.congestion_window

def on_packet_acked(self, packet: QuicSentPacket) -> None:
self.on_packet_acked_timed(packet, Now(), self.recovery._rtt_smoothed)
super().on_packet_acked(packet)

def on_packet_acked_timed(self, packet: QuicSentPacket, now: float, rtt : float) -> None:
def on_packet_acked(self, packet: QuicSentPacket, now: float) -> None:
super().on_packet_acked(packet, now)
rtt = self.recovery._rtt_smoothed
self.bytes_in_flight -= packet.sent_bytes
self.last_ack = now
self.now = now

if self.ssthresh is None or self.congestion_window < self.ssthresh:
# slow start
Expand Down Expand Up @@ -107,7 +106,7 @@ def on_packet_acked_timed(self, packet: QuicSentPacket, now: float, rtt : float)
self.K = self.better_cube_root((W_max_segments - cwnd_epoch_segments)/K_CUBIC_C)


self._W_est = self._W_est + K_CUBIC_ADDITIVE_INCREASE*(packet.sent_bytes/self.congestion_window)
self._W_est = self._W_est + self.additive_increase_factor*(packet.sent_bytes/self.congestion_window)

t = now - self._t_epoch

Expand All @@ -130,12 +129,12 @@ def on_packet_acked_timed(self, packet: QuicSentPacket, now: float, rtt : float)
# convex region of cubic (https://www.rfc-editor.org/rfc/rfc9438.html#name-convex-region)
self.congestion_window = self.congestion_window + ((target - self.congestion_window)*(self._max_datagram_size/self.congestion_window))

def on_packet_sent(self, packet: QuicSentPacket) -> None:
super().on_packet_sent(packet)
def on_packet_sent(self, packet: QuicSentPacket, now : float) -> None:
super().on_packet_sent(packet, now)
self.bytes_in_flight += packet.sent_bytes
if self.last_ack == None:
return
elapsed_idle = Now() - self.last_ack
elapsed_idle = now - self.last_ack
if (elapsed_idle >= K_CUBIC_MAX_IDLE_TIME):
self.reset()

Expand All @@ -145,6 +144,7 @@ def on_packets_expired(self, packets: Iterable[QuicSentPacket]) -> None:
self.bytes_in_flight -= packet.sent_bytes

def on_packets_lost(self, packets: Iterable[QuicSentPacket], now: float) -> None:
self.now = now
super().on_packets_lost(packets, now)
lost_largest_time = 0.0
for packet in packets:
Expand Down Expand Up @@ -178,6 +178,7 @@ def on_packets_lost(self, packets: Iterable[QuicSentPacket], now: float) -> None


def on_rtt_measurement(self, latest_rtt: float, now: float) -> None:
self.now = now
super().on_rtt_measurement(latest_rtt, now)
# check whether we should exit slow start
if self.ssthresh is None and self._rtt_monitor.is_rtt_increasing(
Expand Down Expand Up @@ -208,8 +209,7 @@ def log_callback(self) -> Dict[str, Any]:


if self.ssthresh != None:
now = Now()
t = now - self._t_epoch
t = self.now - self._t_epoch

# saving the phase
if (self.ssthresh == None):
Expand Down
8 changes: 4 additions & 4 deletions src/aioquic/quic/congestion/reno.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def __init__(self, * , max_datagram_size: int, callback=None) -> None:
self._rtt_monitor = QuicRttMonitor()


def on_packet_acked(self, packet: QuicSentPacket) -> None:
super().on_packet_acked(packet)
def on_packet_acked(self, packet: QuicSentPacket, now : float) -> None:
super().on_packet_acked(packet, now)
self.bytes_in_flight -= packet.sent_bytes

# don't increase window in congestion recovery
Expand All @@ -38,8 +38,8 @@ def on_packet_acked(self, packet: QuicSentPacket) -> None:
self._congestion_stash -= count * self.congestion_window
self.congestion_window += count * self._max_datagram_size

def on_packet_sent(self, packet: QuicSentPacket) -> None:
super().on_packet_sent(packet)
def on_packet_sent(self, packet: QuicSentPacket, now : float) -> None:
super().on_packet_sent(packet, now)
self.bytes_in_flight += packet.sent_bytes

def on_packets_expired(self, packets: Iterable[QuicSentPacket]) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/aioquic/quic/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def datagrams_to_send(self, now: float) -> List[Tuple[bytes, NetworkAddress]]:
for packet in packets:
packet.sent_time = now
self._loss.on_packet_sent(
packet=packet, space=self._spaces[packet.epoch]
packet=packet, space=self._spaces[packet.epoch], now=now
)
if packet.epoch == tls.Epoch.HANDSHAKE:
sent_handshake = True
Expand Down
7 changes: 3 additions & 4 deletions src/aioquic/quic/recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ def __init__(
self._cc = congestion_control
self._cc._max_datagram_size = max_datagram_size
self._cc.set_recovery(self)
self._cc.on_init()

self._pacer = QuicPacketPacer(max_datagram_size=max_datagram_size)

Expand Down Expand Up @@ -213,7 +212,7 @@ def on_ack_received(
is_ack_eliciting = True
space.ack_eliciting_in_flight -= 1
if packet.in_flight:
self._cc.on_packet_acked(packet)
self._cc.on_packet_acked(packet, now=now)
largest_newly_acked = packet_number
largest_sent_time = packet.sent_time

Expand Down Expand Up @@ -277,7 +276,7 @@ def on_loss_detection_timeout(self, now: float) -> None:
self._pto_count += 1
self.reschedule_data(now=now)

def on_packet_sent(self, packet: QuicSentPacket, space: QuicPacketSpace) -> None:
def on_packet_sent(self, packet: QuicSentPacket, space: QuicPacketSpace, now : float) -> None:
space.sent_packets[packet.packet_number] = packet

if packet.is_ack_eliciting:
Expand All @@ -287,7 +286,7 @@ def on_packet_sent(self, packet: QuicSentPacket, space: QuicPacketSpace) -> None
self._time_of_last_sent_ack_eliciting_packet = packet.sent_time

# add packet to bytes in flight
self._cc.on_packet_sent(packet)
self._cc.on_packet_sent(packet, now=now)

if self._quic_logger is not None:
self._log_metrics_updated()
Expand Down

0 comments on commit 67b60d8

Please sign in to comment.