fix: Band-Aid fix for utp speed limit (#7541)

* fix: don't clamp in `can_read_wrapper()` for TCP

* fix: notify bandwidth different in utp and TCP
This commit is contained in:
Yat Ho
2025-10-16 11:19:51 +08:00
committed by GitHub
parent 894c0859aa
commit bae63e4cfc
+12 -4
View File
@@ -350,7 +350,7 @@ void tr_peerIo::can_read_wrapper(size_t bytes_transferred)
auto done = false;
auto err = false;
if (bytes_transferred > 0U)
if (socket_.is_tcp() && bytes_transferred > 0U)
{
bandwidth().notify_bandwidth_consumed(TR_DOWN, bytes_transferred, false, now);
}
@@ -358,19 +358,27 @@ void tr_peerIo::can_read_wrapper(size_t bytes_transferred)
// In normal conditions, only continue processing if we still have bandwidth
// quota for it.
//
// The read buffer will grow indefinitely if libutp or the TCP stack keeps buffering
// data faster than the bandwidth limit allows. To safeguard against that, we keep
// The read buffer will grow indefinitely if libutp keeps buffering data faster
// than the bandwidth limit allows. To safeguard against that, we keep
// processing if the read buffer is more than twice as large as the target size.
while (!done && !err && (read_buffer_size() > RcvBuf * 2U || bandwidth().clamp(TR_DOWN, read_buffer_size()) != 0U))
while (!done && !err &&
(socket_.is_tcp() || read_buffer_size() > RcvBuf * 2U || bandwidth().clamp(TR_DOWN, read_buffer_size()) != 0U))
{
auto piece = size_t{};
auto const old_size = read_buffer_size();
auto const read_state = can_read_ != nullptr ? can_read_(this, user_data_, &piece) : ReadState::Err;
auto const used = old_size - read_buffer_size();
if (piece > 0U)
{
bandwidth().notify_bandwidth_consumed(TR_DOWN, piece, true, now);
}
if (socket_.is_utp() && used > 0U)
{
bandwidth().notify_bandwidth_consumed(TR_DOWN, used, false, now);
}
switch (read_state)
{
case ReadState::Now: