Skip to content

Commit ebf9ee1

Browse files
authored
修复用例失败 (#35)
* feat: c++ 对齐其他语言SDK 1. 限流接口支持传入method 2. 去掉grpc以及动态权重调整的代码 3. 支持非二次寻址 4. 支持环境变量配置 * feat:修复用例失败问题 * feat: 默认地址导致用例失败问题 * feat: 优化并去掉二次寻址 * githu流水线配置低,等待的时间比较长 * feat: 修复样例错误
1 parent d4a7035 commit ebf9ee1

File tree

8 files changed

+57
-11
lines changed

8 files changed

+57
-11
lines changed

examples/rate_limit/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
BIT = $(shell getconf LONG_BIT)
2+
3+
POLARIS_CPP = ../..
4+
POLARIS_INCL = $(POLARIS_CPP)/include
5+
POLARIS_LIB_DIR = $(POLARIS_CPP)/build$(BIT)/lib
6+
POLARIS_LIB = $(POLARIS_LIB_DIR)/libpolaris_api.a
7+
PROTOBUF_LIB = $(POLARIS_CPP)/third_party/protobuf/build$(BIT)/libprotobuf.a
8+
9+
CXX = g++
10+
CXXFLAGS += -g -Wall -Wno-write-strings -Werror -std=c++11
11+
12+
SRC = $(wildcard *.cpp)
13+
OBJECTS = $(SRC:%.cpp=%)
14+
15+
all: $(OBJECTS)
16+
17+
%: %.cpp $(POLARIS_LIB)
18+
@echo -e Building $< ...
19+
$(CXX) $(CXXFLAGS) -I$(POLARIS_INCL) $< $(POLARIS_LIB) $(PROTOBUF_LIB) -pthread -lz -o $@
20+
21+
$(POLARIS_LIB):
22+
@echo build polaris-cpp lib
23+
make -C ${POLARIS_CPP}
24+
25+
clean:
26+
@echo -e Clean $(OBJECTS)
27+
@-rm -rf $(OBJECTS)

examples/rate_limit/polaris.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
global:
2+
serverConnector:
3+
addresses:
4+
- 127.0.0.1:8091
5+
rateLimiter:
6+
rateLimitCluster:
7+
namespace: Polaris
8+
service: polaris.limiter

examples/rate_limit/rate_limit.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,22 @@ void SignalHandler(int signum) {
3131
int main(int argc, char** argv) {
3232
if (argc < 3) {
3333
std::cout << "usage: " << argv[0] << std::endl
34-
<< " service_namespace service_name label1<key:value> label2<key:value> qps" << std::endl
34+
<< " service_namespace service_name <method> label1<key:value> label2<key:value> qps" << std::endl
3535
<< "example: " << argv[0] << std::endl
3636
<< " Test service_name labelK1:labelV1 100" << std::endl;
3737
return -1;
3838
}
3939
std::string service_namespace = argv[1];
4040
std::string service_name = argv[2];
41+
std::string method;
4142
std::map<std::string, std::string> labels;
42-
for (int i = 3; i < argc - 1; ++i) {
43-
std::string kv = argv[i];
44-
std::size_t pos = kv.find(':');
45-
labels.insert(std::make_pair(kv.substr(0, pos), kv.substr(pos + 1)));
43+
if (argc > 3) {
44+
method = argv[3];
45+
for (int i = 4; i < argc - 1; ++i) {
46+
std::string kv = argv[i];
47+
std::size_t pos = kv.find(':');
48+
labels.insert(std::make_pair(kv.substr(0, pos), kv.substr(pos + 1)));
49+
}
4650
}
4751
int qps = atoi(argv[argc - 1]);
4852
int interval = 1000 * 1000 / qps; // 根据传入qps计算每个请求耗时
@@ -60,6 +64,7 @@ int main(int argc, char** argv) {
6064
polaris::QuotaRequest quota_request; // 限流请求
6165
quota_request.SetServiceNamespace(service_namespace); // 设置限流规则对应服务的命名空间
6266
quota_request.SetServiceName(service_name); // 设置限流规则对应的服务名
67+
quota_request.SetMethod(method); // 设置限流规则对应的接口名
6368
quota_request.SetLabels(labels); // 设置label用于匹配限流规则
6469

6570
// 调用接口

polaris.yaml.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ rateLimiter:
395395
# 限流服务器集群所在命名空间
396396
namespace: Polaris
397397
# 限流服务器集群名字
398-
service: poalris.limiter
398+
service: polaris.limiter
399399
# 描述:批量上报间隔
400400
# 类型: string
401401
# 格式: ^\d+(ms|s|m|h)$

polaris/cache/report_client.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ void ReportClient::SetupTask() {
2828

2929
void ReportClient::DoTask() {
3030
ContextImpl* context_impl = context_->GetContextImpl();
31-
const std::string& bind_ip = context_impl->GetApiBindIp();
3231
ServerConnector* server_connector = context_impl->GetServerConnector();
3332
POLARIS_ASSERT(server_connector != nullptr);
3433

@@ -66,6 +65,12 @@ void ReportClient::DoTask() {
6665
reactor_->SubmitTask(new ReportTaskSubmit(this, context_impl->GetReportClientInterval()));
6766
};
6867

68+
const std::string& bind_ip = context_impl->GetApiBindIp();
69+
if (bind_ip.empty()) {
70+
//TODO: 当前通过连接自动获取IP并设置后,这里会获取不到,后续需要解决
71+
reactor_->AddTimingTask(new TimingFuncTask<ReportClient>(DoTask, this, context_impl->GetReportClientInterval()));
72+
return;
73+
}
6974
ReturnCode ret_code =
7075
server_connector->AsyncReportClient(bind_ip, context_impl->GetApiDefaultTimeout(), polaris_callback);
7176
if (ret_code != kReturnOk) {

polaris/context/context_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ class ContextImpl {
113113

114114
const v1::SDKToken& GetSdkToken() { return sdk_token_; }
115115

116+
void SetBindIP(const std::string bind_ip) { sdk_token_.set_ip(bind_ip); }
117+
116118
const ContextConfig& GetContextConfig() { return context_config_; }
117119

118120
const SystemVariables& GetSystemVariables() const { return system_variables_; }

polaris/plugin/server_connector/grpc_server_connector.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,8 @@ ReturnCode GrpcServerConnector::Init(Config* config, Context* context) {
210210
if (!NetClient::GetIpByConnect(&bind_ip, server_lists_)) {
211211
POLARIS_LOG(LOG_ERROR, "get client ip from polaris connection failed");
212212
} else {
213-
v1::SDKToken& sdkToken = const_cast<v1::SDKToken&>(contextImpl->GetSdkToken());
214-
sdkToken.set_ip(bind_ip);
215-
POLARIS_LOG(LOG_INFO, "get local ip address by connection return ip:%s", bind_ip.c_str());
213+
contextImpl->SetBindIP(bind_ip);
214+
POLARIS_LOG(LOG_INFO, "get local ip address by connection, sdk token ip:%s", contextImpl->GetApiBindIp().c_str());
216215
}
217216
}
218217

test/quota/quota_manager_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ TEST_F(QuotaManagerTest, TestWindowExpired) {
257257
mock_local_registry->service_data_list_.push_back(service_rate_limit);
258258
mock_local_registry->service_data_list_.push_back(service_rate_limit);
259259
TestUtils::FakeNowIncrement(61 * 1000); // 61s触发淘汰
260-
sleep(2); // 2s等待过期检查任务执行
260+
sleep(10); // 2s等待过期检查任务执行
261261

262262
// 第1个窗口,到了过期时间,已经限流,不淘汰,限流
263263
labels.clear();

0 commit comments

Comments
 (0)