refactor: add a tr_direction arg to tr_torrent::is_queued() (#6263)

This commit is contained in:
Charles Kerr 2023-11-16 23:55:29 -06:00 committed by GitHub
parent 966756ed4b
commit 65d8ae5357
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 50 deletions

View File

@ -603,16 +603,7 @@ std::vector<tr_torrent*> get_next_queued_torrents(tr_torrents& torrents, tr_dire
{
TR_ASSERT(tr_isDirection(dir));
// build an array of the candidates
auto candidates = std::vector<tr_torrent*>{};
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;

View File

@ -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<size_t> 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<size_t> 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<size_t> 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())