Skip to content

Commit bf237eb

Browse files
authored
Fix always true condition for cleanup path LogSeriesLimiter
Fix inverted time check so that cleanup only runs once per interval: use `if (now - last_cleanup >= cleanup_delay_s)` (equivalently `if (last_cleanup < cutoff_time)`) instead of always-true condition.
1 parent 57b66b8 commit bf237eb

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/Common/LoggingFormatStringHelpers.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,18 @@ LogSeriesLimiter::LogSeriesLimiter(LoggerPtr logger_, size_t allowed_count_, tim
9090
}
9191

9292
time_t now = time(nullptr);
93+
static const time_t cleanup_delay_s = 600;
94+
time_t cutoff_time = now - cleanup_delay_s; // entries older than this are stale
95+
9396
UInt128 name_hash = sipHash128(logger->name().c_str(), logger->name().size());
9497

9598
std::lock_guard lock(mutex);
9699

97-
if (last_cleanup == 0)
98-
last_cleanup = now;
99-
100100
auto & series_records = getSeriesRecords();
101101

102-
static const time_t cleanup_delay_s = 600;
103-
if (last_cleanup + cleanup_delay_s >= now)
104-
{
105-
time_t old = now - cleanup_delay_s;
106-
std::erase_if(series_records, [old](const auto & elem) { return get<0>(elem.second) < old; });
102+
if (last_cleanup < cutoff_time) // will also be triggered when last_cleanup is zero
103+
{
104+
std::erase_if(series_records, [old](const auto & elem) { return get<0>(elem.second) < cutoff_time; });
107105
last_cleanup = now;
108106
}
109107

0 commit comments

Comments
 (0)