From 65d8ae53577428c07c9740f38dcce6d4b97adbef Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 16 Nov 2023 23:55:29 -0600 Subject: [PATCH] refactor: add a tr_direction arg to tr_torrent::is_queued() (#6263) --- libtransmission/session.cc | 23 +++++-------- libtransmission/torrent.h | 67 ++++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 50 deletions(-) diff --git a/libtransmission/session.cc b/libtransmission/session.cc index b693b50f3..2f46b6d5b 100644 --- a/libtransmission/session.cc +++ b/libtransmission/session.cc @@ -603,16 +603,7 @@ std::vector get_next_queued_torrents(tr_torrents& torrents, tr_dire { TR_ASSERT(tr_isDirection(dir)); - // build an array of the candidates - auto candidates = std::vector{}; - candidates.reserve(std::size(torrents)); - for (auto* const tor : torrents) - { - if (tor->is_queued() && (dir == tor->queue_direction())) - { - candidates.push_back(tor); - } - } + auto candidates = torrents.get_matching([dir](auto const* const tor) { return tor->is_queued(dir); }); // find the best n candidates num_wanted = std::min(num_wanted, std::size(candidates)); @@ -648,16 +639,20 @@ size_t tr_session::count_queue_free_slots(tr_direction dir) const noexcept auto const now = tr_time(); for (auto const* const tor : torrents()) { - /* is it the right activity? */ + // is it the right activity? if (activity != tor->activity()) { continue; } - /* is it stalled? */ - if (stalled_enabled && difftime(now, std::max(tor->startDate, tor->activityDate)) >= stalled_if_idle_for_n_seconds) + // is it stalled? + if (stalled_enabled) { - continue; + auto const idle_seconds = tor->idle_seconds(now); + if (idle_seconds && *idle_seconds >= stalled_if_idle_for_n_seconds) + { + continue; + } } ++active_count; diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index bd2ff3e80..8fda55a61 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -597,9 +597,14 @@ public: [[nodiscard]] tr_stat stats() const; - [[nodiscard]] constexpr auto is_queued() const noexcept + [[nodiscard]] constexpr auto queue_direction() const noexcept { - return this->is_queued_; + return is_done() ? TR_UP : TR_DOWN; + } + + [[nodiscard]] constexpr auto is_queued(tr_direction const dir) const noexcept + { + return is_queued_ && dir == queue_direction(); } void set_is_queued(bool queued = true) noexcept @@ -612,11 +617,6 @@ public: } } - [[nodiscard]] constexpr auto queue_direction() const noexcept - { - return this->is_done() ? TR_UP : TR_DOWN; - } - [[nodiscard]] constexpr auto allows_pex() const noexcept { return this->is_public() && this->session->allows_pex(); @@ -667,8 +667,6 @@ public: [[nodiscard]] constexpr auto activity() const noexcept { - bool const is_seed = this->is_done(); - if (verify_state_ == VerifyState::Active) { return TR_STATUS_CHECK; @@ -679,22 +677,19 @@ public: return TR_STATUS_CHECK_WAIT; } - if (this->is_running()) + if (is_running()) { - return is_seed ? TR_STATUS_SEED : TR_STATUS_DOWNLOAD; + return is_done() ? TR_STATUS_SEED : TR_STATUS_DOWNLOAD; } - if (this->is_queued()) + if (is_queued(TR_UP) && session->queueEnabled(TR_UP)) { - if (is_seed && this->session->queueEnabled(TR_UP)) - { - return TR_STATUS_SEED_WAIT; - } + return TR_STATUS_SEED_WAIT; + } - if (!is_seed && this->session->queueEnabled(TR_DOWN)) - { - return TR_STATUS_DOWNLOAD_WAIT; - } + if (is_queued(TR_DOWN) && session->queueEnabled(TR_DOWN)) + { + return TR_STATUS_DOWNLOAD_WAIT; } return TR_STATUS_STOPPED; @@ -803,6 +798,22 @@ public: return idle_limit_minutes_; } + [[nodiscard]] constexpr std::optional idle_seconds(time_t now) const noexcept + { + auto const activity = this->activity(); + + if (activity == TR_STATUS_DOWNLOAD || activity == TR_STATUS_SEED) + { + if (auto const latest = std::max(startDate, activityDate); latest != 0) + { + TR_ASSERT(now >= latest); + return now - latest; + } + } + + return {}; + } + [[nodiscard]] constexpr std::optional idle_seconds_left(time_t now) const noexcept { auto const idle_limit_minutes = effective_idle_limit_minutes(); @@ -1154,22 +1165,6 @@ private: return {}; } - [[nodiscard]] constexpr std::optional idle_seconds(time_t now) const noexcept - { - auto const activity = this->activity(); - - if (activity == TR_STATUS_DOWNLOAD || activity == TR_STATUS_SEED) - { - if (auto const latest = std::max(startDate, activityDate); latest != 0) - { - TR_ASSERT(now >= latest); - return now - latest; - } - } - - return {}; - } - [[nodiscard]] constexpr bool is_piece_transfer_allowed(tr_direction direction) const noexcept { if (uses_speed_limit(direction) && speed_limit(direction).is_zero())