diff --git a/examples/rate_limit/Makefile b/examples/rate_limit/Makefile new file mode 100644 index 0000000..230c88d --- /dev/null +++ b/examples/rate_limit/Makefile @@ -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) diff --git a/examples/rate_limit/polaris.yaml b/examples/rate_limit/polaris.yaml new file mode 100644 index 0000000..057219f --- /dev/null +++ b/examples/rate_limit/polaris.yaml @@ -0,0 +1,8 @@ +global: + serverConnector: + addresses: + - 127.0.0.1:8091 +rateLimiter: + rateLimitCluster: + namespace: Polaris + service: polaris.limiter \ No newline at end of file diff --git a/examples/rate_limit/rate_limit.cpp b/examples/rate_limit/rate_limit.cpp index ad9c7b6..b051b4b 100644 --- a/examples/rate_limit/rate_limit.cpp +++ b/examples/rate_limit/rate_limit.cpp @@ -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 label2 qps" << std::endl + << " service_namespace service_name label1 label2 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 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计算每个请求耗时 @@ -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用于匹配限流规则 // 调用接口 diff --git a/polaris.yaml.template b/polaris.yaml.template index 6416322..5f76642 100644 --- a/polaris.yaml.template +++ b/polaris.yaml.template @@ -395,7 +395,7 @@ rateLimiter: # 限流服务器集群所在命名空间 namespace: Polaris # 限流服务器集群名字 - service: poalris.limiter + service: polaris.limiter # 描述:批量上报间隔 # 类型: string # 格式: ^\d+(ms|s|m|h)$ diff --git a/polaris/cache/report_client.cpp b/polaris/cache/report_client.cpp index 21c624c..fc03e87 100644 --- a/polaris/cache/report_client.cpp +++ b/polaris/cache/report_client.cpp @@ -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); @@ -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(DoTask, this, context_impl->GetReportClientInterval())); + return; + } ReturnCode ret_code = server_connector->AsyncReportClient(bind_ip, context_impl->GetApiDefaultTimeout(), polaris_callback); if (ret_code != kReturnOk) { diff --git a/polaris/context/context_impl.h b/polaris/context/context_impl.h index 868ff6f..7c75fc6 100644 --- a/polaris/context/context_impl.h +++ b/polaris/context/context_impl.h @@ -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_; } diff --git a/polaris/plugin/server_connector/grpc_server_connector.cpp b/polaris/plugin/server_connector/grpc_server_connector.cpp index 1f2919e..f6d7a6e 100644 --- a/polaris/plugin/server_connector/grpc_server_connector.cpp +++ b/polaris/plugin/server_connector/grpc_server_connector.cpp @@ -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(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()); } } diff --git a/test/quota/quota_manager_test.cpp b/test/quota/quota_manager_test.cpp index d66a6a3..fc26673 100644 --- a/test/quota/quota_manager_test.cpp +++ b/test/quota/quota_manager_test.cpp @@ -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();