Skip to content

[easylog]fix tsan #920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions include/ylt/coro_io/client_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down Expand Up @@ -372,6 +374,13 @@ class client_pool : public std::enable_shared_from_this<
pool_config, io_context_pool);
}

static std::unique_ptr<client_pool> 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<client_pool>(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)
Expand Down
17 changes: 9 additions & 8 deletions include/ylt/easylog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<appender>(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;
}
Expand Down Expand Up @@ -105,11 +106,11 @@ class logger {

private:
logger() {
static appender appender{};
appender.start_thread();
appender.enable_console(true);
static auto app = std::make_shared<appender>();
app->start_thread();
app->enable_console(true);
async_ = true;
appender_ = &appender;
appender_ = app;
}

logger(const logger &) = default;
Expand All @@ -135,7 +136,7 @@ class logger {
#endif
bool async_ = false;
bool enable_console_ = true;
appender *appender_ = nullptr;
std::shared_ptr<appender> appender_ = nullptr;
std::vector<std::function<void(std::string_view)>> appenders_;
};

Expand Down
13 changes: 7 additions & 6 deletions include/ylt/standalone/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -351,7 +352,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
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;
Expand Down Expand Up @@ -1087,7 +1088,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
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;
Expand All @@ -1103,7 +1104,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
}
#endif
if (socket_->is_timeout_) {
ec = std::make_error_code(std::errc::timed_out);
ec = make_error_code(http_errc::request_timeout);
}
}

Expand Down Expand Up @@ -1374,7 +1375,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
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;
Expand Down Expand Up @@ -2050,7 +2051,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
}
#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};
}

Expand Down Expand Up @@ -2194,7 +2195,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
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;
Expand Down
37 changes: 37 additions & 0 deletions include/ylt/standalone/cinatra/error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once
#include <string>
#include <system_error>

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<http_errc>(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<int>(e), category()};
}

inline bool operator==(const std::error_code& code, http_errc ec) {
return code.value() == (int)ec;
}

} // namespace cinatra
10 changes: 5 additions & 5 deletions src/coro_http/tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}

{
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/coro_http/tests/test_cinatra_websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading