log: make m_limiter a shared_ptr

This allows us to safely and explicitly manage the dual dependency
on the limiter: one for the Logger, and one for the CScheduler.

Github-Pull: #33011
Rebased-From: 3d630c2544
This commit is contained in:
stickies-v
2025-07-23 22:06:37 +01:00
committed by fanquake
parent 81751341e9
commit acfa83d9d0
4 changed files with 26 additions and 12 deletions

View File

@@ -371,12 +371,19 @@ static size_t MemUsage(const BCLog::Logger::BufferedLog& buflog)
memusage::MallocUsage(sizeof(memusage::list_node<BCLog::Logger::BufferedLog>));
}
BCLog::LogRateLimiter::LogRateLimiter(
SchedulerFunction scheduler_func,
uint64_t max_bytes,
std::chrono::seconds reset_window) : m_max_bytes{max_bytes}, m_reset_window{reset_window}
BCLog::LogRateLimiter::LogRateLimiter(uint64_t max_bytes, std::chrono::seconds reset_window)
: m_max_bytes{max_bytes}, m_reset_window{reset_window} {}
std::shared_ptr<BCLog::LogRateLimiter> BCLog::LogRateLimiter::Create(
SchedulerFunction&& scheduler_func, uint64_t max_bytes, std::chrono::seconds reset_window)
{
scheduler_func([this] { Reset(); }, reset_window);
auto limiter{std::shared_ptr<LogRateLimiter>(new LogRateLimiter(max_bytes, reset_window))};
std::weak_ptr<LogRateLimiter> weak_limiter{limiter};
auto reset = [weak_limiter] {
if (auto shared_limiter{weak_limiter.lock()}) shared_limiter->Reset();
};
scheduler_func(reset, limiter->m_reset_window);
return limiter;
}
BCLog::LogRateLimiter::Status BCLog::LogRateLimiter::Consume(