Skip to content

Commit

Permalink
Sync code (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Aug 21, 2024
1 parent 98528db commit 6323967
Show file tree
Hide file tree
Showing 19 changed files with 2,936 additions and 1,465 deletions.
39 changes: 17 additions & 22 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,9 @@ async_simple::coro::Lazy<void> basic_usage() {

void use_metric() {
using namespace ylt::metric;
auto c =
std::make_shared<counter_t>("request_count", "request count",
std::vector<std::string>{"method", "url"});
auto failed = std::make_shared<gauge_t>(
"not_found_request_count", "not found request count",
std::vector<std::string>{"method", "code", "url"});
auto c = std::make_shared<counter_t>("request_count", "request count");
auto failed = std::make_shared<gauge_t>("not_found_request_count",
"not found request count");
auto total =
std::make_shared<counter_t>("total_request_count", "total request count");

Expand All @@ -448,19 +445,19 @@ void use_metric() {
summary_t::Quantiles{
{0.5, 0.05}, {0.9, 0.01}, {0.95, 0.005}, {0.99, 0.001}});

default_metric_manager::register_metric_dynamic(c);
default_metric_manager::register_metric_dynamic(total);
default_metric_manager::register_metric_dynamic(failed);
default_metric_manager::register_metric_dynamic(h);
default_metric_manager::register_metric_dynamic(summary);
default_static_metric_manager::instance().register_metric(c);
default_static_metric_manager::instance().register_metric(total);
default_static_metric_manager::instance().register_metric(failed);
default_static_metric_manager::instance().register_metric(h);
default_static_metric_manager::instance().register_metric(summary);

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distr(1, 100);

std::thread thd([&] {
while (true) {
c->inc({"GET", "/test"});
c->inc();
total->inc();
h->observe(distr(gen));
summary->observe(distr(gen));
Expand All @@ -473,9 +470,7 @@ void use_metric() {
server.set_default_handler(
[&](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
failed->inc({std::string(req.get_method()),
std::to_string((int)status_type::not_found),
std::string(req.get_url())});
failed->inc();
total->inc();
resp.set_status_and_content(status_type::not_found, "not found");
co_return;
Expand All @@ -484,14 +479,14 @@ void use_metric() {
server.set_http_handler<GET>(
"/get", [&](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "ok");
c->inc({std::string(req.get_method()), std::string(req.get_url())});
c->inc();
total->inc();
});

server.set_http_handler<GET>(
"/test", [&](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "ok");
c->inc({std::string(req.get_method()), std::string(req.get_url())});
c->inc();
total->inc();
});

Expand All @@ -514,13 +509,13 @@ void metrics_example() {
"get_req_count", "get req count",
std::map<std::string, std::string>{{"url", "/get"}});
auto get_req_qps = std::make_shared<gauge_t>("get_req_qps", "get req qps");
// default_metric_manager::register_metric_static(get_req_counter,
// default_static_metric_manager::instance().register_metric_static(get_req_counter,
// get_req_qps);
int64_t last = 0;
std::thread thd([&] {
while (true) {
std::this_thread::sleep_for(1s);
auto value = get_req_counter->value({"/get"});
auto value = get_req_counter->value();
get_req_qps->update(value - last);
last = value;
}
Expand Down Expand Up @@ -551,8 +546,8 @@ async_simple::coro::Lazy<void> use_channel() {
server.async_start();
std::this_thread::sleep_for(100ms);

auto channel = std::make_shared<coro_io::channel<coro_http_client>>(
coro_io::channel<coro_http_client>::create(
auto channel = std::make_shared<coro_io::load_blancer<coro_http_client>>(
coro_io::load_blancer<coro_http_client>::create(
{"127.0.0.1:9001"}, {.lba = coro_io::load_blance_algorithm::random}));
std::string url = "http://127.0.0.1:9001/";
co_await channel->send_request(
Expand All @@ -573,7 +568,7 @@ async_simple::coro::Lazy<void> use_pool() {
server.use_metrics();
server.async_start();

auto map = default_metric_manager::metric_map_static();
auto map = default_static_metric_manager::instance().metric_map();
for (auto &[k, m] : map) {
std::cout << k << ", ";
std::cout << m->help() << "\n";
Expand Down
4 changes: 2 additions & 2 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
co_return;
}
std::string file_data;
detail::resize(file_data, std::min(max_single_part_size_, length));
detail::resize(file_data, (std::min)(max_single_part_size_, length));
coro_io::coro_file file{};
file.open(source, std::ios::in);
file.seek(offset, std::ios::cur);
Expand All @@ -895,7 +895,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
std::size_t size;
while (length > 0) {
if (std::tie(ec, size) = co_await file.async_read(
file_data.data(), std::min(file_data.size(), length));
file_data.data(), (std::min)(file_data.size(), length));
ec) {
// bad request, file may smaller than content-length
break;
Expand Down
46 changes: 24 additions & 22 deletions include/cinatra/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include "cinatra/mime_types.hpp"
#include "cinatra_log_wrapper.hpp"
#include "coro_http_connection.hpp"
#include "ylt/coro_io/channel.hpp"
#include "ylt/coro_io/coro_file.hpp"
#include "ylt/coro_io/coro_io.hpp"
#include "ylt/coro_io/io_context_pool.hpp"
#include "ylt/coro_io/load_blancer.hpp"
#include "ylt/metric/system_metric.hpp"

namespace cinatra {
Expand Down Expand Up @@ -185,9 +185,9 @@ class coro_http_server {
void use_metrics(bool enable_json = false,
std::string url_path = "/metrics") {
init_metrics();
using root =
ylt::metric::metric_collector_t<ylt::metric::default_metric_manager,
ylt::metric::system_metric_manager>;
using root = ylt::metric::metric_collector_t<
ylt::metric::default_static_metric_manager,
ylt::metric::system_metric_manager>;
set_http_handler<http_method::GET>(
url_path,
[enable_json](coro_http_request &req, coro_http_response &res) {
Expand Down Expand Up @@ -216,14 +216,15 @@ class coro_http_server {
throw std::invalid_argument("not config hosts yet!");
}

auto channel = std::make_shared<coro_io::channel<coro_http_client>>(
coro_io::channel<coro_http_client>::create(hosts, {.lba = type},
weights));
auto load_blancer =
std::make_shared<coro_io::load_blancer<coro_http_client>>(
coro_io::load_blancer<coro_http_client>::create(
hosts, {.lba = type}, weights));
auto handler =
[this, channel, type](
[this, load_blancer, type](
coro_http_request &req,
coro_http_response &response) -> async_simple::coro::Lazy<void> {
co_await channel->send_request(
co_await load_blancer->send_request(
[this, &req, &response](
coro_http_client &client,
std::string_view host) -> async_simple::coro::Lazy<void> {
Expand Down Expand Up @@ -255,14 +256,15 @@ class coro_http_server {
throw std::invalid_argument("not config hosts yet!");
}

auto channel = std::make_shared<coro_io::channel<coro_http_client>>(
coro_io::channel<coro_http_client>::create(hosts, {.lba = type},
weights));
auto load_blancer =
std::make_shared<coro_io::load_blancer<coro_http_client>>(
coro_io::load_blancer<coro_http_client>::create(
hosts, {.lba = type}, weights));

set_http_handler<cinatra::GET>(
url_path,
[channel](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
[load_blancer](coro_http_request &req, coro_http_response &resp)
-> async_simple::coro::Lazy<void> {
websocket_result result{};
while (true) {
result = co_await req.get_conn()->read_websocket();
Expand All @@ -275,7 +277,7 @@ class coro_http_server {
break;
}

co_await channel->send_request(
co_await load_blancer->send_request(
[&req, result](
coro_http_client &client,
std::string_view host) -> async_simple::coro::Lazy<void> {
Expand Down Expand Up @@ -928,20 +930,20 @@ class coro_http_server {
using namespace ylt::metric;

cinatra_metric_conf::enable_metric = true;
default_metric_manager::create_metric_static<counter_t>(
default_static_metric_manager::instance().create_metric_static<counter_t>(
cinatra_metric_conf::server_total_req, "");
default_metric_manager::create_metric_static<counter_t>(
default_static_metric_manager::instance().create_metric_static<counter_t>(
cinatra_metric_conf::server_failed_req, "");
default_metric_manager::create_metric_static<counter_t>(
default_static_metric_manager::instance().create_metric_static<counter_t>(
cinatra_metric_conf::server_total_recv_bytes, "");
default_metric_manager::create_metric_static<counter_t>(
default_static_metric_manager::instance().create_metric_static<counter_t>(
cinatra_metric_conf::server_total_send_bytes, "");
default_metric_manager::create_metric_static<gauge_t>(
default_static_metric_manager::instance().create_metric_static<gauge_t>(
cinatra_metric_conf::server_total_fd, "");
default_metric_manager::create_metric_static<histogram_t>(
default_static_metric_manager::instance().create_metric_static<histogram_t>(
cinatra_metric_conf::server_req_latency, "",
std::vector<double>{30, 40, 50, 60, 70, 80, 90, 100, 150});
default_metric_manager::create_metric_static<histogram_t>(
default_static_metric_manager::instance().create_metric_static<histogram_t>(
cinatra_metric_conf::server_read_latency, "",
std::vector<double>{3, 5, 7, 9, 13, 18, 23, 35, 50});
#if defined(__GNUC__)
Expand Down
42 changes: 26 additions & 16 deletions include/cinatra/metric_conf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#include "ylt/metric/gauge.hpp"
#include "ylt/metric/histogram.hpp"
#include "ylt/metric/metric.hpp"
#include "ylt/metric/metric_manager.hpp"
#include "ylt/metric/summary.hpp"
#include "ylt/metric/system_metric.hpp"

namespace cinatra {
struct cinatra_metric_conf {
Expand All @@ -25,8 +27,9 @@ struct cinatra_metric_conf {
return;
}

static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::counter_t>(server_total_req);
static auto m =
ylt::metric::default_static_metric_manager::instance()
.get_metric_static<ylt::metric::counter_t>(server_total_req);
if (m == nullptr) {
return;
}
Expand All @@ -37,8 +40,9 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::counter_t>(server_failed_req);
static auto m =
ylt::metric::default_static_metric_manager::instance()
.get_metric_static<ylt::metric::counter_t>(server_failed_req);
if (m == nullptr) {
return;
}
Expand All @@ -49,8 +53,9 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::gauge_t>(server_total_fd);
static auto m =
ylt::metric::default_static_metric_manager::instance()
.get_metric_static<ylt::metric::gauge_t>(server_total_fd);
if (m == nullptr) {
return;
}
Expand All @@ -61,8 +66,9 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::gauge_t>(server_total_fd);
static auto m =
ylt::metric::default_static_metric_manager::instance()
.get_metric_static<ylt::metric::gauge_t>(server_total_fd);
if (m == nullptr) {
return;
}
Expand All @@ -73,8 +79,9 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::counter_t>(server_total_recv_bytes);
static auto m =
ylt::metric::default_static_metric_manager::instance()
.get_metric_static<ylt::metric::counter_t>(server_total_recv_bytes);
if (m == nullptr) {
return;
}
Expand All @@ -85,8 +92,9 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::counter_t>(server_total_send_bytes);
static auto m =
ylt::metric::default_static_metric_manager::instance()
.get_metric_static<ylt::metric::counter_t>(server_total_send_bytes);
if (m == nullptr) {
return;
}
Expand All @@ -97,8 +105,9 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::histogram_t>(server_req_latency);
static auto m =
ylt::metric::default_static_metric_manager::instance()
.get_metric_static<ylt::metric::histogram_t>(server_req_latency);
if (m == nullptr) {
return;
}
Expand All @@ -109,8 +118,9 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::histogram_t>(server_read_latency);
static auto m =
ylt::metric::default_static_metric_manager::instance()
.get_metric_static<ylt::metric::histogram_t>(server_read_latency);
if (m == nullptr) {
return;
}
Expand Down
Loading

0 comments on commit 6323967

Please sign in to comment.