Skip to content

Commit

Permalink
fix and tests (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Nov 25, 2024
1 parent 762923d commit 2e27edb
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 427 deletions.
122 changes: 0 additions & 122 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
#include <vector>

#include "../include/cinatra.hpp"
#include "cinatra/metric_conf.hpp"

using namespace cinatra;
using namespace ylt::metric;
using namespace std::chrono_literals;

void create_file(std::string filename, size_t file_size = 64) {
Expand Down Expand Up @@ -372,8 +370,6 @@ async_simple::coro::Lazy<void> basic_usage() {
response.set_status_and_content(status_type::ok, "ok");
});

server.use_metrics();

person_t person{};
server.set_http_handler<GET>("/person", &person_t::foo, person);

Expand Down Expand Up @@ -427,121 +423,12 @@ async_simple::coro::Lazy<void> basic_usage() {
result.net_err.value() assert(result.status == 200);
#endif
}

void use_metric() {
using namespace ylt::metric;
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");

auto h =
std::make_shared<histogram_t>(std::string("test"), std::string("help"),
std::vector{5.0, 10.0, 20.0, 50.0, 100.0});

auto summary = std::make_shared<summary_t>(std::string("test_summary"),
std::string("summary help"),
std::vector{0.5, 0.9, 0.95, 0.99});

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();
total->inc();
h->observe(distr(gen));
summary->observe(distr(gen));
std::this_thread::sleep_for(1s);
}
});
thd.detach();

coro_http_server server(1, 9001);
server.set_default_handler(
[&](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
failed->inc();
total->inc();
resp.set_status_and_content(status_type::not_found, "not found");
co_return;
});

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();
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();
total->inc();
});

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

server.set_http_handler<GET, POST>(
"/metrics", [](coro_http_request &req, coro_http_response &resp) {
resp.need_date_head(false);
resp.set_status_and_content(status_type::ok, "");
});
server.sync_start();
}

void metrics_example() {
auto get_req_counter = std::make_shared<counter_t>(
"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_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_req_qps->update(value - last);
last = value;
}
});
thd.detach();

coro_http_server server(1, 9001);
server.set_http_handler<GET>(
"/get", [&](coro_http_request &req, coro_http_response &resp) {
// get_req_counter->inc({"/get"});
resp.set_status_and_content(status_type::ok, "ok");
});
server.set_http_handler<GET>(
"/", [&](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "hello world");
});
server.use_metrics(true, "/metrics");
server.sync_start();
}

async_simple::coro::Lazy<void> use_channel() {
coro_http_server server(1, 9001);
server.set_http_handler<GET>(
"/", [&](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "hello world");
});
server.use_metrics();
server.async_start();
std::this_thread::sleep_for(100ms);

Expand All @@ -564,15 +451,8 @@ async_simple::coro::Lazy<void> use_pool() {
"/", [&](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "hello world");
});
server.use_metrics();
server.async_start();

auto map = default_static_metric_manager::instance().metric_map();
for (auto &[k, m] : map) {
std::cout << k << ", ";
std::cout << m->help() << "\n";
}

std::string url = "http://127.0.0.1:9001/";

auto pool = coro_io::client_pool<coro_http_client>::create(
Expand Down Expand Up @@ -600,8 +480,6 @@ async_simple::coro::Lazy<void> use_pool() {
}

int main() {
// use_metric();
// metrics_example();
async_simple::coro::syncAwait(use_channel());
async_simple::coro::syncAwait(use_pool());
async_simple::coro::syncAwait(basic_usage());
Expand Down
34 changes: 34 additions & 0 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,40 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
#endif
}

#ifdef INJECT_FOR_HTTP_CLIENT_TEST
async_simple::coro::Lazy<std::error_code> async_write_raw(
std::string_view data) {
auto [ec, _] = co_await async_write(asio::buffer(data));
co_return ec;
}

async_simple::coro::Lazy<resp_data> async_read_raw(
http_method method, bool clear_buffer = false) {
if (clear_buffer) {
body_.clear();
}

char buf[1024];
std::error_code ec{};
size_t size{};
#ifdef CINATRA_ENABLE_SSL
if (has_init_ssl_) {
std::tie(ec, size) = co_await coro_io::async_read_some(
*socket_->ssl_stream_, asio::buffer(buf, 1024));
}
else {
#endif
std::tie(ec, size) = co_await coro_io::async_read_some(
socket_->impl_, asio::buffer(buf, 1024));
#ifdef CINATRA_ENABLE_SSL
}
#endif
body_.append(buf, size);

co_return resp_data{ec, {}, {}, body_};
}
#endif

inline void set_proxy(const std::string &host, const std::string &port) {
proxy_host_ = host;
proxy_port_ = port;
Expand Down
Loading

0 comments on commit 2e27edb

Please sign in to comment.