From e6eebd495996c1b71840e8293fc58b6a5f9cc4c7 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 22 Jul 2024 15:03:00 +0800 Subject: [PATCH 1/3] default port --- include/cinatra/coro_http_client.hpp | 21 +++++++++++---------- include/cinatra/uri.hpp | 4 ++-- lang/coro_http_client_introduction.md | 2 +- tests/test_cinatra.cpp | 2 +- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 063ccb68..239e1217 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -1546,20 +1546,21 @@ class coro_http_client : public std::enable_shared_from_this { if (!proxy_host_.empty() && !proxy_port_.empty()) { if (!proxy_request_uri_.empty()) proxy_request_uri_.clear(); - if (u.get_port() == "http") { - proxy_request_uri_ += "http://" + u.get_host() + ":"; - proxy_request_uri_ += "80"; + if (u.get_port() == "80") { + proxy_request_uri_.append("http://").append(u.get_host()).append(":80"); } - else if (u.get_port() == "https") { - proxy_request_uri_ += "https://" + u.get_host() + ":"; - proxy_request_uri_ += "443"; + else if (u.get_port() == "443") { + proxy_request_uri_.append("https://") + .append(u.get_host()) + .append(":443"); } else { // all be http - proxy_request_uri_ += "http://" + u.get_host() + ":"; - proxy_request_uri_ += u.get_port(); + proxy_request_uri_.append("http://") + .append(u.get_host()) + .append(u.get_port()); } - proxy_request_uri_ += u.get_path(); + proxy_request_uri_.append(u.get_path()); u.path = std::string_view(proxy_request_uri_); } } @@ -2424,7 +2425,7 @@ class coro_http_client : public std::enable_shared_from_this { bool enable_follow_redirect_ = false; bool enable_timeout_ = false; std::chrono::steady_clock::duration conn_timeout_duration_ = - std::chrono::seconds(30); + std::chrono::seconds(8); std::chrono::steady_clock::duration req_timeout_duration_ = std::chrono::seconds(60); bool enable_tcp_no_delay_ = true; diff --git a/include/cinatra/uri.hpp b/include/cinatra/uri.hpp index 4c1aedaa..089ce4b3 100644 --- a/include/cinatra/uri.hpp +++ b/include/cinatra/uri.hpp @@ -232,7 +232,7 @@ class uri_t { std::string port_str; if (is_ssl) { if (port.empty()) { - port_str = "https"; + port_str = "443"; } else { port_str = std::string(port); @@ -240,7 +240,7 @@ class uri_t { } else { if (port.empty()) { - port_str = "http"; + port_str = "80"; } else { port_str = std::string(port); diff --git a/lang/coro_http_client_introduction.md b/lang/coro_http_client_introduction.md index 9bcdebc6..8e74f3b2 100644 --- a/lang/coro_http_client_introduction.md +++ b/lang/coro_http_client_introduction.md @@ -252,7 +252,7 @@ async_simple::coro::Lazy test_async_client() { // 通过重连复用client1 r = async_simple::coro::syncAwait(client1.connect("http://cn.bing.com")); CHECK(client1.get_host() == "cn.bing.com"); - CHECK(client1.get_port() == "http"); + CHECK(client1.get_port() == "80"); CHECK(r.status == 200); ``` diff --git a/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index 08c3de7c..d066acfa 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -753,7 +753,7 @@ TEST_CASE("test coro_http_client async_http_connect") { r = async_simple::coro::syncAwait(client1.connect("http://cn.bing.com")); CHECK(client1.get_host() == "cn.bing.com"); - CHECK(client1.get_port() == "http"); + CHECK(client1.get_port() == "80"); CHECK(r.status >= 200); r = async_simple::coro::syncAwait(client1.connect("http://www.baidu.com")); From cce50d08707e746e9cf4aea650edda2fe0e48d5c Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 21 Aug 2024 16:58:45 +0800 Subject: [PATCH 2/3] fix --- include/cinatra/ylt/metric/summary.hpp | 70 +++++++++++++------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/include/cinatra/ylt/metric/summary.hpp b/include/cinatra/ylt/metric/summary.hpp index 9ac1a980..d5a5d452 100644 --- a/include/cinatra/ylt/metric/summary.hpp +++ b/include/cinatra/ylt/metric/summary.hpp @@ -29,6 +29,7 @@ REFLECTION(json_summary_t, name, help, type, metrics); #endif struct block_t { + std::atomic is_coro_started_ = false; std::atomic stop_ = false; ylt::detail::moodycamel::ConcurrentQueue sample_queue_; std::shared_ptr quantile_values_; @@ -75,7 +76,7 @@ class summary_t : public static_metric { block_->sample_queue_.enqueue(value); bool expected = false; - if (is_coro_started_.compare_exchange_strong(expected, true)) { + if (block_->is_coro_started_.compare_exchange_strong(expected, true)) { start(block_).via(excutor_->get_executor()).start([](auto &&) { }); } @@ -220,13 +221,13 @@ class summary_t : public static_metric { } if (block->sample_queue_.size_approx() == 0) { - is_coro_started_ = false; + block_->is_coro_started_ = false; if (block->sample_queue_.size_approx() == 0) { break; } bool expected = false; - if (!is_coro_started_.compare_exchange_strong(expected, true)) { + if (!block_->is_coro_started_.compare_exchange_strong(expected, true)) { break; } @@ -243,7 +244,6 @@ class summary_t : public static_metric { std::shared_ptr block_; static inline std::shared_ptr excutor_ = coro_io::create_io_context_pool(1); - std::atomic is_coro_started_ = false; bool has_observe_ = false; }; @@ -260,6 +260,10 @@ struct sum_and_count_t { template struct labels_block_t { + summary_t::Quantiles quantiles_; // readonly + std::chrono::milliseconds max_age_; + uint16_t age_buckets_; + std::atomic is_coro_started_ = false; std::atomic stop_ = false; ylt::detail::moodycamel::ConcurrentQueue> sample_queue_; @@ -280,12 +284,16 @@ class basic_dynamic_summary : public dynamic_metric { std::array labels_name, std::chrono::milliseconds max_age = std::chrono::seconds{60}, uint16_t age_buckets = 5) - : quantiles_{std::move(quantiles)}, - dynamic_metric(MetricType::Summary, std::move(name), std::move(help), - std::move(labels_name)), - max_age_(max_age), - age_buckets_(age_buckets) { - init_block(labels_block_); + : dynamic_metric(MetricType::Summary, std::move(name), std::move(help), + std::move(labels_name)) { + labels_block_ = std::make_shared>(); + labels_block_->quantiles_ = std::move(quantiles); + labels_block_->max_age_ = max_age; + labels_block_->age_buckets_ = age_buckets; + + start(labels_block_).via(excutor_->get_executor()).start([](auto &&) { + }); + g_user_metric_count++; } @@ -307,7 +315,8 @@ class basic_dynamic_summary : public dynamic_metric { labels_block_->sample_queue_.enqueue({std::move(labels_value), value}); bool expected = false; - if (is_coro_started_.compare_exchange_strong(expected, true)) { + if (labels_block_->is_coro_started_.compare_exchange_strong(expected, + true)) { start(labels_block_).via(excutor_->get_executor()).start([](auto &&) { }); } @@ -327,7 +336,7 @@ class basic_dynamic_summary : public dynamic_metric { const std::array &labels_value, double &sum, uint64_t &count) { std::vector vec; - if (quantiles_.empty()) { + if (labels_block_->quantiles_.empty()) { co_return std::vector{}; } @@ -339,7 +348,7 @@ class basic_dynamic_summary : public dynamic_metric { } sum = labels_block_->sum_and_count_[labels_value].sum; count = labels_block_->sum_and_count_[labels_value].count; - for (const auto &quantile : quantiles_) { + for (const auto &quantile : labels_block_->quantiles_) { vec.push_back(it->second->get(quantile.quantile)); } }, @@ -359,13 +368,6 @@ class basic_dynamic_summary : public dynamic_metric { } #endif private: - template - void init_block(std::shared_ptr &block) { - block = std::make_shared(); - start(block).via(excutor_->get_executor()).start([](auto &&) { - }); - } - async_simple::coro::Lazy start( std::shared_ptr> label_block) { summary_label_sample sample; @@ -376,8 +378,9 @@ class basic_dynamic_summary : public dynamic_metric { auto &ptr = label_block->label_quantile_values_[sample.labels_value]; if (ptr == nullptr) { - ptr = std::make_shared(quantiles_, max_age_, - age_buckets_); + ptr = std::make_shared( + label_block->quantiles_, label_block->max_age_, + label_block->age_buckets_); } ptr->insert(sample.value); @@ -393,13 +396,14 @@ class basic_dynamic_summary : public dynamic_metric { co_await async_simple::coro::Yield{}; if (label_block->sample_queue_.size_approx() == 0) { - is_coro_started_ = false; + label_block->is_coro_started_ = false; if (label_block->sample_queue_.size_approx() == 0) { break; } bool expected = false; - if (!is_coro_started_.compare_exchange_strong(expected, true)) { + if (!label_block->is_coro_started_.compare_exchange_strong(expected, + true)) { break; } @@ -412,7 +416,7 @@ class basic_dynamic_summary : public dynamic_metric { } async_simple::coro::Lazy serialize_async_with_label(std::string &str) { - if (quantiles_.empty()) { + if (labels_block_->quantiles_.empty()) { co_return; } @@ -432,13 +436,14 @@ class basic_dynamic_summary : public dynamic_metric { double sum = 0; uint64_t count = 0; auto rates = co_await get_rates(labels_value, sum, count); - for (size_t i = 0; i < quantiles_.size(); i++) { + for (size_t i = 0; i < labels_block_->quantiles_.size(); i++) { str.append(name_); str.append("{"); build_label_string(str, labels_name_, labels_value); str.append(","); str.append("quantile=\""); - str.append(std::to_string(quantiles_[i].quantile)).append("\"} "); + str.append(std::to_string(labels_block_->quantiles_[i].quantile)) + .append("\"} "); str.append(std::to_string(rates[i])).append("\n"); } @@ -459,7 +464,7 @@ class basic_dynamic_summary : public dynamic_metric { #ifdef CINATRA_ENABLE_METRIC_JSON async_simple::coro::Lazy serialize_to_json_with_label_async( std::string &str) { - if (quantiles_.empty()) { + if (labels_block_->quantiles_.empty()) { co_return; } @@ -482,11 +487,12 @@ class basic_dynamic_summary : public dynamic_metric { auto rates = co_await get_rates(labels_value, sum, count); metric.count = count; metric.sum = sum; - for (size_t i = 0; i < quantiles_.size(); i++) { + for (size_t i = 0; i < labels_block_->quantiles_.size(); i++) { for (size_t i = 0; i < labels_value.size(); i++) { metric.labels[labels_name_[i]] = labels_value[i]; } - metric.quantiles.emplace(quantiles_[i].quantile, rates[i]); + metric.quantiles.emplace(labels_block_->quantiles_[i].quantile, + rates[i]); } summary.metrics.push_back(std::move(metric)); @@ -495,13 +501,9 @@ class basic_dynamic_summary : public dynamic_metric { } #endif - Quantiles quantiles_; // readonly std::shared_ptr> labels_block_; static inline std::shared_ptr excutor_ = coro_io::create_io_context_pool(1); - std::chrono::milliseconds max_age_; - uint16_t age_buckets_; - std::atomic is_coro_started_ = false; bool has_observe_ = false; }; From ab07d681f9b33771ba23182f1fce0cba3f85b173 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 21 Aug 2024 17:00:46 +0800 Subject: [PATCH 3/3] revert --- include/cinatra/coro_http_client.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index c3511fa9..a8a6f668 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -2444,7 +2444,7 @@ class coro_http_client : public std::enable_shared_from_this { bool enable_follow_redirect_ = false; bool enable_timeout_ = false; std::chrono::steady_clock::duration conn_timeout_duration_ = - std::chrono::seconds(8); + std::chrono::seconds(30); std::chrono::steady_clock::duration req_timeout_duration_ = std::chrono::seconds(60); bool enable_tcp_no_delay_ = true;