1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-23 00:04:06 +00:00

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;
};