Skip to content

Commit

Permalink
修复用例失败 (#35)
Browse files Browse the repository at this point in the history
* feat: c++ 对齐其他语言SDK

1. 限流接口支持传入method
2. 去掉grpc以及动态权重调整的代码
3. 支持非二次寻址
4. 支持环境变量配置

* feat:修复用例失败问题

* feat: 默认地址导致用例失败问题

* feat: 优化并去掉二次寻址

* githu流水线配置低,等待的时间比较长

* feat: 修复样例错误
  • Loading branch information
andrewshan authored Jul 29, 2022
1 parent d4a7035 commit ebf9ee1
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 11 deletions.
27 changes: 27 additions & 0 deletions examples/rate_limit/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
BIT = $(shell getconf LONG_BIT)

POLARIS_CPP = ../..
POLARIS_INCL = $(POLARIS_CPP)/include
POLARIS_LIB_DIR = $(POLARIS_CPP)/build$(BIT)/lib
POLARIS_LIB = $(POLARIS_LIB_DIR)/libpolaris_api.a
PROTOBUF_LIB = $(POLARIS_CPP)/third_party/protobuf/build$(BIT)/libprotobuf.a

CXX = g++
CXXFLAGS += -g -Wall -Wno-write-strings -Werror -std=c++11

SRC = $(wildcard *.cpp)
OBJECTS = $(SRC:%.cpp=%)

all: $(OBJECTS)

%: %.cpp $(POLARIS_LIB)
@echo -e Building $< ...
$(CXX) $(CXXFLAGS) -I$(POLARIS_INCL) $< $(POLARIS_LIB) $(PROTOBUF_LIB) -pthread -lz -o $@

$(POLARIS_LIB):
@echo build polaris-cpp lib
make -C ${POLARIS_CPP}

clean:
@echo -e Clean $(OBJECTS)
@-rm -rf $(OBJECTS)
8 changes: 8 additions & 0 deletions examples/rate_limit/polaris.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
global:
serverConnector:
addresses:
- 127.0.0.1:8091
rateLimiter:
rateLimitCluster:
namespace: Polaris
service: polaris.limiter
15 changes: 10 additions & 5 deletions examples/rate_limit/rate_limit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ void SignalHandler(int signum) {
int main(int argc, char** argv) {
if (argc < 3) {
std::cout << "usage: " << argv[0] << std::endl
<< " service_namespace service_name label1<key:value> label2<key:value> qps" << std::endl
<< " service_namespace service_name <method> label1<key:value> label2<key:value> qps" << std::endl
<< "example: " << argv[0] << std::endl
<< " Test service_name labelK1:labelV1 100" << std::endl;
return -1;
}
std::string service_namespace = argv[1];
std::string service_name = argv[2];
std::string method;
std::map<std::string, std::string> labels;
for (int i = 3; i < argc - 1; ++i) {
std::string kv = argv[i];
std::size_t pos = kv.find(':');
labels.insert(std::make_pair(kv.substr(0, pos), kv.substr(pos + 1)));
if (argc > 3) {
method = argv[3];
for (int i = 4; i < argc - 1; ++i) {
std::string kv = argv[i];
std::size_t pos = kv.find(':');
labels.insert(std::make_pair(kv.substr(0, pos), kv.substr(pos + 1)));
}
}
int qps = atoi(argv[argc - 1]);
int interval = 1000 * 1000 / qps; // 根据传入qps计算每个请求耗时
Expand All @@ -60,6 +64,7 @@ int main(int argc, char** argv) {
polaris::QuotaRequest quota_request; // 限流请求
quota_request.SetServiceNamespace(service_namespace); // 设置限流规则对应服务的命名空间
quota_request.SetServiceName(service_name); // 设置限流规则对应的服务名
quota_request.SetMethod(method); // 设置限流规则对应的接口名
quota_request.SetLabels(labels); // 设置label用于匹配限流规则

// 调用接口
Expand Down
2 changes: 1 addition & 1 deletion polaris.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ rateLimiter:
# 限流服务器集群所在命名空间
namespace: Polaris
# 限流服务器集群名字
service: poalris.limiter
service: polaris.limiter
# 描述:批量上报间隔
# 类型: string
# 格式: ^\d+(ms|s|m|h)$
Expand Down
7 changes: 6 additions & 1 deletion polaris/cache/report_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ void ReportClient::SetupTask() {

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

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

const std::string& bind_ip = context_impl->GetApiBindIp();
if (bind_ip.empty()) {
//TODO: 当前通过连接自动获取IP并设置后,这里会获取不到,后续需要解决
reactor_->AddTimingTask(new TimingFuncTask<ReportClient>(DoTask, this, context_impl->GetReportClientInterval()));
return;
}
ReturnCode ret_code =
server_connector->AsyncReportClient(bind_ip, context_impl->GetApiDefaultTimeout(), polaris_callback);
if (ret_code != kReturnOk) {
Expand Down
2 changes: 2 additions & 0 deletions polaris/context/context_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class ContextImpl {

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

void SetBindIP(const std::string bind_ip) { sdk_token_.set_ip(bind_ip); }

const ContextConfig& GetContextConfig() { return context_config_; }

const SystemVariables& GetSystemVariables() const { return system_variables_; }
Expand Down
5 changes: 2 additions & 3 deletions polaris/plugin/server_connector/grpc_server_connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,8 @@ ReturnCode GrpcServerConnector::Init(Config* config, Context* context) {
if (!NetClient::GetIpByConnect(&bind_ip, server_lists_)) {
POLARIS_LOG(LOG_ERROR, "get client ip from polaris connection failed");
} else {
v1::SDKToken& sdkToken = const_cast<v1::SDKToken&>(contextImpl->GetSdkToken());
sdkToken.set_ip(bind_ip);
POLARIS_LOG(LOG_INFO, "get local ip address by connection return ip:%s", bind_ip.c_str());
contextImpl->SetBindIP(bind_ip);
POLARIS_LOG(LOG_INFO, "get local ip address by connection, sdk token ip:%s", contextImpl->GetApiBindIp().c_str());
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/quota/quota_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ TEST_F(QuotaManagerTest, TestWindowExpired) {
mock_local_registry->service_data_list_.push_back(service_rate_limit);
mock_local_registry->service_data_list_.push_back(service_rate_limit);
TestUtils::FakeNowIncrement(61 * 1000); // 61s触发淘汰
sleep(2); // 2s等待过期检查任务执行
sleep(10); // 2s等待过期检查任务执行

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

0 comments on commit ebf9ee1

Please sign in to comment.