Skip to content

Commit

Permalink
simplify clean metric method
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jun 18, 2024
1 parent ffc7443 commit ec0b26f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions include/ylt/metric/metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/metric/tests/test_metric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -787,8 +787,7 @@ TEST_CASE("check clean metrics") {
std::vector<std::string>{"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));

Expand Down
1 change: 1 addition & 0 deletions website/docs/zh/metric/metrict_introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit ec0b26f

Please sign in to comment.