From 6132e74a36792acc564a27a5267ef327b039f508 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 27 Feb 2023 14:32:40 -0600 Subject: [PATCH] fix: 4.0.0 regression that broke speed limits for utp peers Fixes #4701. --- libtransmission/peer-io.cc | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libtransmission/peer-io.cc b/libtransmission/peer-io.cc index 9e631de80e4..219f8af1beb 100644 --- a/libtransmission/peer-io.cc +++ b/libtransmission/peer-io.cc @@ -733,7 +733,16 @@ void tr_peerIo::utp_init([[maybe_unused]] struct_utp_context* ctx) { if (auto const* const io = static_cast(utp_get_userdata(args->socket)); io != nullptr) { - return std::size(io->inbuf_); + // We use this callback to enforce speed limits by telling + // libutp to read no more than `target_dl_bytes` bytes. + auto const target_dl_bytes = io->bandwidth_.clamp(TR_DOWN, RcvBuf); + + // libutp's private function get_rcv_window() allows libutp + // to read up to (UTP_RCVBUF - READ_BUFFER_SIZE) bytes and + // UTP_RCVBUF is set to `RcvBuf` by tr_peerIo::utp_init(). + // So to limit dl to `target_dl_bytes`, we need to return + // N where (`target_dl_bytes` == RcvBuf - N). + return RcvBuf - target_dl_bytes; } return {}; });