refactor: save stats.json periodically and when closing session #5476 (#5490)

This commit is contained in:
kmikita 2023-05-05 23:33:23 +03:00 committed by GitHub
parent e407bcc501
commit 693d202a64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 19 deletions

View File

@ -1323,7 +1323,7 @@ void tr_session::closeImplPart2(std::promise<void>* closed_promise, std::chrono:
this->announcer_.reset();
this->announcer_udp_.reset();
stats().saveIfDirty();
stats().save();
peer_mgr_.reset();
openFiles().closeAll();
tr_utpClose(this);
@ -2169,7 +2169,7 @@ tr_session::tr_session(std::string_view config_dir, tr_variant* settings_dict)
tr_torrentSave(tor);
}
stats().saveIfDirty();
stats().save();
});
save_timer_->startRepeating(SaveIntervalSecs);

View File

@ -82,7 +82,6 @@ void tr_stats::save() const
void tr_stats::clear()
{
single_ = old_ = Zero;
is_dirty_ = true;
start_time_ = tr_time();
}

View File

@ -30,7 +30,7 @@ public:
~tr_stats()
{
saveIfDirty();
save();
}
void clear();
@ -45,35 +45,23 @@ public:
constexpr void addUploaded(uint32_t n_bytes) noexcept
{
single_.uploadedBytes += n_bytes;
is_dirty_ = true;
}
constexpr void addDownloaded(uint32_t n_bytes) noexcept
{
single_.downloadedBytes += n_bytes;
is_dirty_ = true;
}
constexpr void addFileCreated() noexcept
{
++single_.filesAdded;
is_dirty_ = true;
}
void saveIfDirty()
{
if (is_dirty_)
{
save();
is_dirty_ = false;
}
}
void save() const;
private:
static tr_session_stats add(tr_session_stats const& a, tr_session_stats const& b);
void save() const;
static tr_session_stats loadOldStats(std::string_view config_dir);
std::string const config_dir_;
@ -82,6 +70,4 @@ private:
static constexpr auto Zero = tr_session_stats{ TR_RATIO_NA, 0U, 0U, 0U, 0U, 0U };
tr_session_stats single_ = Zero;
tr_session_stats old_ = Zero;
bool is_dirty_ = false;
};