From ec0b26f696ad298fe129419990f3502bb9704746 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Tue, 18 Jun 2024 15:17:13 +0800 Subject: [PATCH] simplify clean metric method --- include/ylt/metric/metric.hpp | 5 +++-- src/metric/tests/test_metric.cpp | 3 +-- website/docs/zh/metric/metrict_introduction.md | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/ylt/metric/metric.hpp b/include/ylt/metric/metric.hpp index f818ed7e8..28bb99f1a 100644 --- a/include/ylt/metric/metric.hpp +++ b/include/ylt/metric/metric.hpp @@ -252,7 +252,7 @@ struct metric_manager_t { std::chrono::steady_clock::duration check_duration = std::chrono::minutes(5)) { metric_max_age_ = max_age; - metric_check_duration_ = check_duration; + metric_check_duration_ = (std::min)(max_age, check_duration); start_check(); } @@ -566,8 +566,9 @@ struct metric_manager_t { } static void check_impl() { + auto timer = check_timer_; check_timer_->expires_after(metric_check_duration_); - check_timer_->async_wait([](std::error_code ec) { + check_timer_->async_wait([timer](std::error_code ec) { if (ec) { return; } diff --git a/src/metric/tests/test_metric.cpp b/src/metric/tests/test_metric.cpp index 35f1d498c..9acec6603 100644 --- a/src/metric/tests/test_metric.cpp +++ b/src/metric/tests/test_metric.cpp @@ -787,8 +787,7 @@ TEST_CASE("check clean metrics") { std::vector{"method", "url"}); CHECK(metric_mgr::metric_count_dynamic() == 4); - metric_mgr::set_metric_max_age(std::chrono::milliseconds(10), - std::chrono::milliseconds(10)); + metric_mgr::set_metric_max_age(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(100)); diff --git a/website/docs/zh/metric/metrict_introduction.md b/website/docs/zh/metric/metrict_introduction.md index 54b600cd8..338e7c5c8 100644 --- a/website/docs/zh/metric/metrict_introduction.md +++ b/website/docs/zh/metric/metrict_introduction.md @@ -321,6 +321,7 @@ struct metric_manager_t { // 启用metric 定时清理功能,在使用metric之前设置 // max_age:设置metric的过期时间,过期之后metric会被清理 // check_duration:设置定期监测metric过期的时间间隔 + // 超时检测时间为std::min(max_age, std::chrono::minutes(5)) static void set_metric_max_age(std::chrono::steady_clock::duration max_age, std::chrono::steady_clock::duration check_duration = std::chrono::minutes(5));