diff --git a/include/ylt/coro_io/client_pool.hpp b/include/ylt/coro_io/client_pool.hpp index 96e20db60..9b4eb6d1d 100644 --- a/include/ylt/coro_io/client_pool.hpp +++ b/include/ylt/coro_io/client_pool.hpp @@ -298,8 +298,10 @@ class client_pool : public std::enable_shared_from_this< this->weak_from_this(), clients, (std::max)(collect_time, std::chrono::milliseconds{50}), pool_config_.idle_queue_per_max_clear_count) - .directlyStart([](auto&&) { - },coro_io::get_global_executor()); + .directlyStart( + [](auto&&) { + }, + coro_io::get_global_executor()); } } } @@ -372,6 +374,13 @@ class client_pool : public std::enable_shared_from_this< pool_config, io_context_pool); } + static std::unique_ptr create_unique( + std::string_view host_name, const pool_config& pool_config = {}, + io_context_pool_t& io_context_pool = coro_io::g_io_context_pool()) { + return std::make_unique(private_construct_token{}, host_name, + pool_config, io_context_pool); + } + client_pool(private_construct_token t, std::string_view host_name, const pool_config& pool_config, io_context_pool_t& io_context_pool) diff --git a/include/ylt/easylog.hpp b/include/ylt/easylog.hpp index aa7f1f76c..d429735f7 100644 --- a/include/ylt/easylog.hpp +++ b/include/ylt/easylog.hpp @@ -68,10 +68,11 @@ class logger { void init(Severity min_severity, bool async, bool enable_console, const std::string &filename, size_t max_file_size, size_t max_files, bool flush_every_time) { - static appender appender(filename, async, enable_console, max_file_size, - max_files, flush_every_time); + static auto app = + std::make_shared(filename, async, enable_console, + max_file_size, max_files, flush_every_time); async_ = async; - appender_ = &appender; + appender_ = app; min_severity_ = min_severity; enable_console_ = enable_console; } @@ -105,11 +106,11 @@ class logger { private: logger() { - static appender appender{}; - appender.start_thread(); - appender.enable_console(true); + static auto app = std::make_shared(); + app->start_thread(); + app->enable_console(true); async_ = true; - appender_ = &appender; + appender_ = app; } logger(const logger &) = default; @@ -135,7 +136,7 @@ class logger { #endif bool async_ = false; bool enable_console_ = true; - appender *appender_ = nullptr; + std::shared_ptr appender_ = nullptr; std::vector> appenders_; }; diff --git a/include/ylt/standalone/cinatra/coro_http_client.hpp b/include/ylt/standalone/cinatra/coro_http_client.hpp index c10e535c5..480ef7842 100644 --- a/include/ylt/standalone/cinatra/coro_http_client.hpp +++ b/include/ylt/standalone/cinatra/coro_http_client.hpp @@ -32,6 +32,7 @@ #include "brzip.hpp" #endif #include "cinatra_log_wrapper.hpp" +#include "error.hpp" #include "http_parser.hpp" #include "multipart.hpp" #include "picohttpparser.h" @@ -351,7 +352,7 @@ class coro_http_client : public std::enable_shared_from_this { data = co_await connect(u, eps); } if (socket_->is_timeout_) { - co_return resp_data{std::make_error_code(std::errc::timed_out), 404}; + co_return resp_data{make_error_code(http_errc::connect_timeout), 404}; } if (!data.net_err) { data.status = 200; @@ -1087,7 +1088,7 @@ class coro_http_client : public std::enable_shared_from_this { data = co_await connect(u); } if (socket_->is_timeout_) { - data = resp_data{std::make_error_code(std::errc::timed_out), 404}; + data = resp_data{make_error_code(http_errc::connect_timeout), 404}; } if (data.net_err) { co_return false; @@ -1103,7 +1104,7 @@ class coro_http_client : public std::enable_shared_from_this { } #endif if (socket_->is_timeout_) { - ec = std::make_error_code(std::errc::timed_out); + ec = make_error_code(http_errc::request_timeout); } } @@ -1374,7 +1375,7 @@ class coro_http_client : public std::enable_shared_from_this { co_await handle_read(ec, size, is_keep_alive, std::move(ctx), method); } while (0); if (ec && socket_->is_timeout_) { - ec = std::make_error_code(std::errc::timed_out); + ec = make_error_code(http_errc::request_timeout); } handle_result(data, ec, is_keep_alive); co_return data; @@ -2050,7 +2051,7 @@ class coro_http_client : public std::enable_shared_from_this { } #endif if (socket_->is_timeout_) { - ec = std::make_error_code(std::errc::timed_out); + ec = make_error_code(http_errc::connect_timeout); co_return resp_data{ec, 404}; } @@ -2194,7 +2195,7 @@ class coro_http_client : public std::enable_shared_from_this { sock, read_buf, ws.left_header_len(), has_init_ssl); ec) { if (socket_->is_timeout_) { - co_return resp_data{std::make_error_code(std::errc::timed_out), 404}; + co_return resp_data{make_error_code(http_errc::request_timeout), 404}; } data.net_err = ec; data.status = 404; diff --git a/include/ylt/standalone/cinatra/error.hpp b/include/ylt/standalone/cinatra/error.hpp new file mode 100644 index 000000000..3ebcb9bc3 --- /dev/null +++ b/include/ylt/standalone/cinatra/error.hpp @@ -0,0 +1,37 @@ +#pragma once +#include +#include + +namespace cinatra { +enum class http_errc { connect_timeout = 313, request_timeout }; + +class http_error_category : public std::error_category { + public: + const char* name() const noexcept override { return "coro_http_error"; } + + std::string message(int ev) const override { + switch (static_cast(ev)) { + case http_errc::connect_timeout: + return "connect timeout"; + case http_errc::request_timeout: + return "request timeout"; + default: + return "unknown error"; + } + } +}; + +inline cinatra::http_error_category& category() { + static cinatra::http_error_category instance; + return instance; +} + +inline std::error_code make_error_code(http_errc e) { + return {static_cast(e), category()}; +} + +inline bool operator==(const std::error_code& code, http_errc ec) { + return code.value() == (int)ec; +} + +} // namespace cinatra diff --git a/src/coro_http/tests/test_cinatra.cpp b/src/coro_http/tests/test_cinatra.cpp index aed5d59df..6c8988ba5 100644 --- a/src/coro_http/tests/test_cinatra.cpp +++ b/src/coro_http/tests/test_cinatra.cpp @@ -83,7 +83,7 @@ TEST_CASE("test for gzip") { auto result = async_simple::coro::syncAwait( client.connect(uri).via(&client.get_executor())); if (result.net_err) - CHECK(result.net_err == std::errc::timed_out); + CHECK(result.net_err == http_errc::connect_timeout); client.set_conn_timeout(-1ms); client.set_req_timeout(0ms); @@ -96,7 +96,7 @@ TEST_CASE("test for gzip") { result = async_simple::coro::syncAwait( client.async_get("/none").via(&client.get_executor())); if (result.net_err) - CHECK(result.net_err == std::errc::timed_out); + CHECK(result.net_err == http_errc::request_timeout); client.add_header("Content-Encoding", "none"); client.set_req_timeout(-1ms); @@ -113,7 +113,7 @@ TEST_CASE("test for gzip") { result = async_simple::coro::syncAwait( client.async_get(uri).via(&client.get_executor())); if (result.net_err) - CHECK(result.net_err == std::errc::timed_out); + CHECK(result.net_err == http_errc::request_timeout); } { @@ -2575,7 +2575,7 @@ TEST_CASE("test coro_http_client request timeout") { if (!r.net_err) { r = async_simple::coro::syncAwait(client.async_get("/")); if (r.net_err) { - CHECK(r.net_err == std::errc::timed_out); + CHECK(r.net_err == http_errc::request_timeout); } } } @@ -2770,7 +2770,7 @@ TEST_CASE("test coro http request timeout") { client.set_req_timeout(500ms); result = async_simple::coro::syncAwait(client.async_get(uri)); - CHECK(result.net_err == std::errc::timed_out); + CHECK(result.net_err == http_errc::request_timeout); // after timeout, the socket in client has been closed, so use a new client // to test. diff --git a/src/coro_http/tests/test_cinatra_websocket.cpp b/src/coro_http/tests/test_cinatra_websocket.cpp index ed15970f6..f10782eaf 100644 --- a/src/coro_http/tests/test_cinatra_websocket.cpp +++ b/src/coro_http/tests/test_cinatra_websocket.cpp @@ -147,7 +147,7 @@ TEST_CASE("test websocket") { co_await client.write_websocket("hello websocket"); auto data = co_await client.read_websocket(); CINATRA_LOG_DEBUG << data.net_err.message(); - CHECK(data.net_err == std::errc::timed_out); + CHECK(data.net_err == http_errc::request_timeout); }; async_simple::coro::syncAwait(client_timeout());