1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00

Drop buffers in clear if 'too big'

This commit is contained in:
asonix 2024-05-19 19:24:37 -05:00
parent 43fca317d3
commit 94c5d4d641
2 changed files with 12 additions and 2 deletions

View File

@ -2,6 +2,9 @@ use std::collections::VecDeque;
use bytes::{Buf, BufMut, Bytes, BytesMut}; use bytes::{Buf, BufMut, Bytes, BytesMut};
// 64KB max capacity (arbitrarily chosen)
const MAX_CAPACITY: usize = 1024 * 64;
pub(crate) struct BigBytes { pub(crate) struct BigBytes {
buffer: BytesMut, buffer: BytesMut,
frozen: VecDeque<Bytes>, frozen: VecDeque<Bytes>,
@ -18,10 +21,17 @@ impl BigBytes {
} }
// Clear the internal queue and buffer, resetting length to zero // Clear the internal queue and buffer, resetting length to zero
pub(super) fn clear(&mut self) { //
// if the internal buffer capacity exceeds 64KB or new_capacity, whichever is greater, it will
// be freed and a new buffer of capacity `new_capacity` will be allocated
pub(super) fn clear(&mut self, new_capacity: usize) {
std::mem::take(&mut self.frozen); std::mem::take(&mut self.frozen);
self.frozen_len = 0; self.frozen_len = 0;
self.buffer.clear(); self.buffer.clear();
if self.buffer.capacity() > new_capacity.max(MAX_CAPACITY) {
self.buffer = BytesMut::with_capacity(new_capacity);
}
} }
// Return a mutable reference to the underlying buffer. This should only be used when dealing // Return a mutable reference to the underlying buffer. This should only be used when dealing

View File

@ -347,7 +347,7 @@ where
} }
// everything has written to I/O; clear buffer // everything has written to I/O; clear buffer
write_buf.clear(); write_buf.clear(HW_BUFFER_SIZE);
// flush the I/O and check if get blocked // flush the I/O and check if get blocked
io.poll_flush(cx) io.poll_flush(cx)