Skip to content

Commit 057a0bf

Browse files
committed
refactor(queue): move retain to DropTailQueue and track now_bytes
The retain method was a default trait method on PacketQueue but only ever called on the concrete DropTailQueue. Its implementations never updated now_bytes, so the byte counter drifted out of sync with the queue contents whenever packets were dropped. Remove retain from the PacketQueue trait (and its no-op impls in codel/drophead/infinite), and reimplement it as an inherent method on DropTailQueue that decrements now_bytes for each dropped packet.
1 parent dafb830 commit 057a0bf

5 files changed

Lines changed: 21 additions & 29 deletions

File tree

rattan-core/src/cells/bandwidth/queue/codel.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,4 @@ where
299299
fn length(&self) -> usize {
300300
self.queue.len()
301301
}
302-
303-
fn retain<F>(&mut self, mut f: F)
304-
where
305-
F: FnMut(&P) -> bool,
306-
{
307-
self.queue.retain(|packet| f(packet));
308-
}
309302
}

rattan-core/src/cells/bandwidth/queue/drophead.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,4 @@ where
137137
fn length(&self) -> usize {
138138
self.queue.len()
139139
}
140-
141-
fn retain<F>(&mut self, mut f: F)
142-
where
143-
F: FnMut(&P) -> bool,
144-
{
145-
self.queue.retain(|packet| f(packet));
146-
}
147140
}

rattan-core/src/cells/bandwidth/queue/droptail.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,30 @@ where
138138
fn length(&self) -> usize {
139139
self.queue.len()
140140
}
141+
}
141142

142-
fn retain<F>(&mut self, mut f: F)
143+
impl<P> DropTailQueue<P>
144+
where
145+
P: Packet,
146+
{
147+
/// Retain only the packets for which `f` returns `true`, dropping the rest
148+
/// while keeping `now_bytes` consistent with the remaining packets.
149+
///
150+
/// Used by the token bucket cell to drop packets that exceed the queue's
151+
/// `max_size` when it is shrunk (see `TokenBucketCellEgress::set_config`).
152+
pub fn retain<F>(&mut self, mut f: F)
143153
where
144154
F: FnMut(&P) -> bool,
145155
{
146-
self.queue.retain(|packet| f(packet));
156+
let extra_length = self.bw_type.extra_length();
157+
let now_bytes = &mut self.now_bytes;
158+
self.queue.retain(|packet| {
159+
if f(packet) {
160+
true
161+
} else {
162+
*now_bytes -= packet.l3_length() + extra_length;
163+
false
164+
}
165+
});
147166
}
148167
}

rattan-core/src/cells/bandwidth/queue/infinite.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,4 @@ where
7878
fn length(&self) -> usize {
7979
self.queue.len()
8080
}
81-
82-
fn retain<F>(&mut self, mut f: F)
83-
where
84-
F: FnMut(&P) -> bool,
85-
{
86-
self.queue.retain(|packet| f(packet));
87-
}
8881
}

rattan-core/src/cells/bandwidth/queue/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,4 @@ where
157157
fn get_front_size(&self) -> Option<usize>;
158158

159159
fn length(&self) -> usize;
160-
161-
fn retain<F>(&mut self, _f: F)
162-
where
163-
F: FnMut(&P) -> bool,
164-
{
165-
}
166160
}

0 commit comments

Comments
 (0)