Skip to content

Commit 18e1c94

Browse files
committed
disable ticker thread if set custom time function
1 parent a5f096b commit 18e1c94

File tree

6 files changed

+15
-16
lines changed

6 files changed

+15
-16
lines changed

polaris/quota/quota_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ ReturnCode QuotaManager::Init(Context* context, Config* config) {
7878

7979
context_ = context;
8080
ContextMode context_mode = context->GetContextMode();
81-
if (context_mode != kLimitContext && context_mode != kShareContext) {
81+
if (context_mode == kPrivateContext) { // provider或consumer私有时不创建quota manger线程
8282
return kReturnOk;
8383
}
8484
bool is_enable = config->GetBoolOrDefault(kRateLimitEnableKey, kRateLimitEnableDefault);

polaris/utils/time_clock.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include "utils/time_clock.h"
1515

1616
#include <features.h>
17-
#include <google/protobuf/duration.pb.h>
18-
#include <google/protobuf/timestamp.pb.h>
1917
#include <pthread.h>
2018
#include <stdio.h>
2119
#include <stdlib.h>
@@ -112,7 +110,7 @@ void Time::TrySetUpClock() {
112110
#if !defined(POLARIS_DISABLE_TIME_TICKER) // 不使用自定义时钟
113111
pthread_mutex_lock(&g_custom_clock_lock);
114112
g_custom_clock_ref_count++;
115-
if (g_custom_clock_update_tid == 0) { // 创建更新线程
113+
if (g_custom_clock_update_tid == 0 && current_time_impl == clock_real_time) { // 创建更新线程
116114
pthread_atfork(ForkPrepare, NULL, ForkChild); // 注册Fork事件回调
117115
// 这里必须先初始化自定义时钟,线程启动后会立即替换
118116
timespec clock_time;

polaris/utils/time_clock.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@
1717
#include <stdint.h>
1818
#include <time.h>
1919

20-
namespace google {
21-
namespace protobuf {
22-
class Duration;
23-
class Timestamp;
24-
} // namespace protobuf
25-
} // namespace google
20+
#include <google/protobuf/duration.pb.h>
21+
#include <google/protobuf/timestamp.pb.h>
2622

2723
namespace polaris {
2824

test/plugin/local_registry/local_registry_test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ TEST_F(InMemoryLocalRegistryTest, TestServiceExpire) {
372372
TestUtils::FakeNowIncrement(1);
373373
local_registry_->RemoveExpireServiceData(Time::GetCurrentTimeMs());
374374
ASSERT_TRUE(mock_server_connector_->saved_handler_ == NULL);
375+
TestUtils::TearDownFakeTime();
375376
}
376377

377378
TEST_F(InMemoryLocalRegistryTest, TestOldServiceDataGc) {
@@ -422,6 +423,7 @@ TEST_F(InMemoryLocalRegistryTest, TestOldServiceDataGc) {
422423
// 旧服务虽然被缓存删除并被释放,但更新前在使用,所以还需要再次释放
423424
ASSERT_EQ(init_service_data->DecrementAndGetRef(), 1);
424425
delete mock_server_connector_->saved_handler_;
426+
TestUtils::TearDownFakeTime();
425427
}
426428

427429
} // namespace polaris

test/quota/quota_manager_test.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ namespace polaris {
2828

2929
class QuotaManagerTest : public ::testing::Test {
3030
virtual void SetUp() {
31-
context_ = TestContext::CreateContext(kLimitContext);
32-
TestUtils::SetUpFakeTime();
31+
context_ = TestContext::CreateContext();
3332
ASSERT_TRUE(context_ != NULL);
3433
service_key_.namespace_ = "test_namespace";
3534
service_key_.name_ = "test_name";
@@ -45,7 +44,6 @@ class QuotaManagerTest : public ::testing::Test {
4544
delete quota_manager_;
4645
quota_manager_ = NULL;
4746
}
48-
TestUtils::TearDownFakeTime();
4947
}
5048

5149
protected:
@@ -143,9 +141,6 @@ TEST_F(QuotaManagerTest, GetQuotaWithRule) {
143141
}
144142

145143
TEST_F(QuotaManagerTest, PrepareQuotaInfo) {
146-
delete context_;
147-
context_ = TestContext::CreateContext();
148-
ASSERT_TRUE(context_ != NULL);
149144
CreateQuotaManager(true);
150145

151146
MockLocalRegistry *mock_local_registry = TestContext::SetupMockLocalRegistry(context_);

test/utils/time_clock_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,12 @@ TEST_F(TimeClockTest, MultiThreadTest) {
7171
thread_list.clear();
7272
}
7373

74+
TEST_F(TimeClockTest, CustomClockFunc) {
75+
TestUtils::SetUpFakeTime(); // 设置自定义时间函数
76+
Time::TrySetUpClock();
77+
ASSERT_EQ(g_custom_clock_update_tid, 0); // 自定义时间函数时不会启动内部时间线程
78+
Time::TryShutdomClock();
79+
TestUtils::TearDownFakeTime();
80+
}
81+
7482
} // namespace polaris

0 commit comments

Comments
 (0)