From a7a4083c3ba9d9c9c5baef28c7440c0979707b14 Mon Sep 17 00:00:00 2001 From: stickz Date: Wed, 10 Jul 2024 22:19:53 -0400 Subject: [PATCH] libtorrent: Increase max throttle speed to UINT_MAX This commit increases the maximum throttle speed to just over 500MB/s. It also sets a minimum throttle speed of approximately 256kb/s to modernize the throttle code. --- libtorrent/src/torrent/throttle.cc | 33 +++++++----------------------- libtorrent/src/torrent/throttle.h | 8 +++++--- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/libtorrent/src/torrent/throttle.cc b/libtorrent/src/torrent/throttle.cc index c0971060..238417b3 100644 --- a/libtorrent/src/torrent/throttle.cc +++ b/libtorrent/src/torrent/throttle.cc @@ -36,6 +36,8 @@ #include "config.h" +#include + #include #include "net/throttle_internal.h" @@ -77,18 +79,18 @@ Throttle::create_slave() { bool Throttle::is_throttled() { - return m_maxRate != 0 && m_maxRate != std::numeric_limits::max(); + return m_maxRate != 0 && m_maxRate < UINT_MAX; } void -Throttle::set_max_rate(uint32_t v) { +Throttle::set_max_rate(uint64_t v) { if (v == m_maxRate) return; - if (v > (1 << 30)) - throw input_error("Throttle rate must be between 0 and 2^30."); + if (v != 0 && (v < THROTTLE_MIN_VALUE || v > (UINT_MAX - 1))) + throw input_error("Throttle rate must be between 2097152 and 4294967295."); - uint32_t oldRate = m_maxRate; + uint64_t oldRate = m_maxRate; m_maxRate = v; m_throttleList->set_min_chunk_size(calculate_min_chunk_size()); @@ -110,27 +112,6 @@ Throttle::rate() const { uint32_t Throttle::calculate_min_chunk_size() const { - // Just for each modification, make this into a function, rather - // than if-else chain. - if (m_maxRate <= (8 << 10)) - return (1 << 9); - - else if (m_maxRate <= (32 << 10)) - return (2 << 9); - - else if (m_maxRate <= (64 << 10)) - return (3 << 9); - - else if (m_maxRate <= (128 << 10)) - return (4 << 9); - - else if (m_maxRate <= (512 << 10)) - return (8 << 9); - - else if (m_maxRate <= (2048 << 10)) - return (16 << 9); - - else return (32 << 9); } diff --git a/libtorrent/src/torrent/throttle.h b/libtorrent/src/torrent/throttle.h index b9b58da8..fc80cb7f 100644 --- a/libtorrent/src/torrent/throttle.h +++ b/libtorrent/src/torrent/throttle.h @@ -54,8 +54,8 @@ class LIBTORRENT_EXPORT Throttle { bool is_throttled(); // 0 == UNLIMITED. - uint32_t max_rate() const { return m_maxRate; } - void set_max_rate(uint32_t v); + uint64_t max_rate() const { return m_maxRate; } + void set_max_rate(uint64_t v); const Rate* rate() const; @@ -64,6 +64,8 @@ class LIBTORRENT_EXPORT Throttle { protected: Throttle() {} ~Throttle() {} + + const uint32_t THROTTLE_MIN_VALUE = 2097152; ThrottleInternal* m_ptr() { return reinterpret_cast(this); } const ThrottleInternal* c_ptr() const { return reinterpret_cast(this); } @@ -72,7 +74,7 @@ class LIBTORRENT_EXPORT Throttle { uint32_t calculate_max_chunk_size() const LIBTORRENT_NO_EXPORT; uint32_t calculate_interval() const LIBTORRENT_NO_EXPORT; - uint32_t m_maxRate; + uint64_t m_maxRate; ThrottleList* m_throttleList; };