Skip to content

Commit 92c7559

Browse files
committed
Apply the soft-limit a little more aggressively
1 parent be3d7ae commit 92c7559

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

src/circular_buffer.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,12 @@ impl CircularBuffer {
141141

142142
pub fn apply_soft_limit(&mut self, limit: usize) {
143143
let limit = std::cmp::min(limit, self.max_capacity);
144-
if self.remaining() > limit || self.current_capacity() <= limit {
145-
return;
146-
}
147-
148-
if self.remaining() == 0 {
144+
if self.remaining() == 0 && self.current_capacity() > limit {
149145
self.buffer = Vec::new().into_boxed_slice();
150146
self.position = 0;
151-
return;
147+
} else if self.remaining() <= limit / 2 && self.current_capacity() >= 2 * limit {
148+
self.resize_buffer(limit);
152149
}
153-
154-
self.resize_buffer(limit);
155150
}
156151
}
157152

@@ -337,34 +332,71 @@ mod test {
337332
}
338333

339334
#[test]
340-
fn apply_soft_limit() {
335+
fn resize_buffer() {
341336
let mut b = CircularBuffer::new(0, 16);
342337
b.write_all(b"0123456789ABCDEF").unwrap();
343338
assert_eq!(b.current_capacity(), 16);
344339

345-
b.apply_soft_limit(16);
340+
b.resize_buffer(16);
346341
assert_eq!(b.current_capacity(), 16);
347342
assert_eq!(b.bytes(), b"0123456789ABCDEF");
348343

349344
b.advance(1);
350-
b.apply_soft_limit(16);
345+
b.resize_buffer(16);
351346
assert_eq!(b.current_capacity(), 16);
352347
assert_eq!(b.bytes(), b"123456789ABCDEF");
353348

354-
b.apply_soft_limit(15);
355-
assert_eq!(b.current_capacity(), 15);
356-
assert_eq!(b.bytes(), b"123456789ABCDEF");
357-
358-
b.apply_soft_limit(8);
349+
b.resize_buffer(15);
359350
assert_eq!(b.current_capacity(), 15);
360351
assert_eq!(b.bytes(), b"123456789ABCDEF");
361352

362353
b.advance(15);
363-
b.apply_soft_limit(15);
354+
b.resize_buffer(15);
364355
assert_eq!(b.current_capacity(), 15);
365356
assert_eq!(b.bytes(), b"");
366357

367-
b.apply_soft_limit(14);
358+
b.resize_buffer(0);
359+
assert_eq!(b.current_capacity(), 0);
360+
assert_eq!(b.bytes(), b"");
361+
}
362+
363+
#[test]
364+
fn apply_soft_limit() {
365+
let mut b = CircularBuffer::new(0, 16);
366+
b.write_all(b"0123456789ABCDEF").unwrap();
367+
assert_eq!(b.current_capacity(), 16);
368+
369+
b.apply_soft_limit(16);
370+
assert_eq!(b.current_capacity(), 16);
371+
assert_eq!(b.bytes(), b"0123456789ABCDEF");
372+
373+
b.apply_soft_limit(0);
374+
assert_eq!(b.current_capacity(), 16);
375+
assert_eq!(b.bytes(), b"0123456789ABCDEF");
376+
377+
b.advance(8);
378+
b.apply_soft_limit(8);
379+
assert_eq!(b.current_capacity(), 16);
380+
assert_eq!(b.bytes(), b"89ABCDEF");
381+
382+
b.advance(3);
383+
b.apply_soft_limit(8);
384+
assert_eq!(b.current_capacity(), 16);
385+
assert_eq!(b.bytes(), b"BCDEF");
386+
387+
b.advance(1);
388+
b.apply_soft_limit(8);
389+
assert_eq!(b.current_capacity(), 8);
390+
assert_eq!(b.bytes(), b"CDEF");
391+
392+
b.advance(4);
393+
b.apply_soft_limit(8);
394+
assert_eq!(b.current_capacity(), 8);
395+
assert_eq!(b.bytes(), b"");
396+
397+
assert!(b.write_all(b"0123456789ABCDEF").is_ok());
398+
b.advance(16);
399+
b.apply_soft_limit(8);
368400
assert_eq!(b.current_capacity(), 0);
369401
assert_eq!(b.bytes(), b"");
370402
}

src/connection.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -974,9 +974,7 @@ where
974974
}
975975
}
976976

977-
if self.in_buffer.is_empty() {
978-
self.in_buffer.apply_soft_limit(self.settings.in_buffer_capacity_soft_limit);
979-
}
977+
self.in_buffer.apply_soft_limit(self.settings.in_buffer_capacity_soft_limit);
980978
Ok(())
981979
}
982980

@@ -997,9 +995,7 @@ where
997995

998996
if let Some(len) = self.socket.try_write_buf(&mut self.out_buffer)? {
999997
trace!("Wrote {} bytes to {}", len, self.peer_addr());
1000-
if self.out_buffer.is_empty() {
1001-
self.out_buffer.apply_soft_limit(self.settings.out_buffer_capacity_soft_limit);
1002-
}
998+
self.out_buffer.apply_soft_limit(self.settings.out_buffer_capacity_soft_limit);
1003999

10041000
let finished = len == 0 || self.out_buffer.is_empty();
10051001
if finished {

0 commit comments

Comments
 (0)