Skip to content

Commit 11a8743

Browse files
committed
fixed some
1 parent 5734951 commit 11a8743

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

main.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,19 @@ int main(int argc, char **argv)
5252
}
5353
auto c = hcpp::config::get_config(cfg_path);
5454
c->config_to(hcpp::slow_dns::get_slow_dns());
55-
// c->save_callback([sd = hcpp::slow_dns::get_slow_dns()](auto &&cs)
56-
// { sd->save_hm(cs.hm_); });
5755

5856
asio::io_context io_context;
5957

60-
hcpp::nc=std::make_shared<hcpp::notify_channel>(io_context,1);
58+
hcpp::nc = std::make_shared<hcpp::notify_channel>(io_context, 1);
6159

6260
asio::signal_set signals(io_context, SIGINT, SIGTERM);
6361
signals.async_wait([&](auto, auto)
6462
{ io_context.stop(); });
6563

6664
hcpp::httpserver hs;
6765
hcpp::mimt_https_server mhs;
68-
if(!c->config_to(mhs)){
66+
if (!c->config_to(mhs))
67+
{
6968
return -1;
7069
}
7170

@@ -76,15 +75,28 @@ int main(int argc, char **argv)
7675
}
7776
return c; });
7877

79-
co_spawn(io_context, hs.wait_http(c->get_port()), detached);
80-
co_spawn(io_context, mhs.wait_c(10,c->get_proxy_service()), detached);
81-
// co_spawn(io_context, []()->asio::awaitable<void>{co_await hcpp::nc->async_send(asio::error_code{}, "ok");}, detached);
78+
auto exit_handler = [&io_context](auto &&eptr)
79+
{
80+
try
81+
{
82+
if (eptr)
83+
std::rethrow_exception(eptr);
84+
}
85+
catch (const std::exception &e)
86+
{
87+
log::error("exit_handler: {}", e.what());
88+
io_context.stop();
89+
}
90+
};
91+
92+
co_spawn(io_context, hs.wait_http(c->get_port()), exit_handler);
93+
co_spawn(io_context, mhs.wait_c(10, c->get_proxy_service()), exit_handler);
8294

8395
auto create_thread = [&](auto self, int i) -> void
8496
{
8597
if (i > 0)
8698
{
87-
spdlog::debug("线程{}创建成功",i);
99+
spdlog::debug("线程{}创建成功", i);
88100
std::jthread j(self, self, i - 1);
89101
while (!io_context.stopped())
90102
{
@@ -98,7 +110,7 @@ int main(int argc, char **argv)
98110
}
99111
}
100112
j.join();
101-
spdlog::debug("线程{}退出成功",i);
113+
spdlog::debug("线程{}退出成功", i);
102114
}
103115
};
104116
// 为了防止对象在多线程情况下销毁出问题

src/dns.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ namespace hcpp
4646

4747
std::shared_mutex smutex_;
4848
std::map<host_edp, edp_lists> edp_cache_;
49-
std::unique_ptr<tcp_resolver> resolver_;
5049
bool has_init_;
5150
bool has_save_;
5251

@@ -62,8 +61,18 @@ namespace hcpp
6261

6362
void remove(host_edp hedp);
6463

65-
//BUG 析构时使用锁
66-
~slow_dns_imp()=default;
64+
~slow_dns_imp()
65+
{
66+
{
67+
std::unique_lock<std::mutex> lk(rmutex_);
68+
resolve_running_.clear();
69+
}
70+
71+
{
72+
std::unique_lock<std::shared_mutex> lk(smutex_);
73+
edp_cache_.clear();
74+
}
75+
}
6776
};
6877
namespace
6978
{
@@ -90,7 +99,7 @@ namespace hcpp
9099
tcp_socket s(c);
91100
co_await asio::async_connect(s, endpoints);
92101

93-
auto ssl_m = std::make_shared<ssl_sock_mem>(provider.host_,"https");
102+
auto ssl_m = std::make_shared<ssl_sock_mem>(provider.host_, "https");
94103
ssl_m->init_client(std::move(s));
95104
co_await ssl_m->async_handshake();
96105
co_await ssl_m->wait();

src/http/http_svc_keeper.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ namespace hcpp
5151
awaitable<void> get_endpoint(const std::string &host, const std::string &service, std::shared_ptr<hcpp::slow_dns> dns, std::shared_ptr<channel> c);
5252
void return_back(const std::string &host, const std::string &service, std::shared_ptr<memory> m);
5353
bool remove_endpoint(std::string host, std::string service);
54+
55+
~imp()
56+
{
57+
{
58+
std::unique_lock<std::shared_mutex> lk(shm_rq_);
59+
request_queue_.clear();
60+
}
61+
{
62+
std::unique_lock<std::shared_mutex> lk(shm_c_);
63+
cache_.clear();
64+
}
65+
}
5466
};
5567

5668
class failed_mem : public simple_mem
@@ -337,8 +349,8 @@ namespace hcpp
337349
auto sock = co_await make_socket(host, service);
338350
if (sock)
339351
{
340-
auto ss= std::make_shared<socket_memory>(std::move(*sock));
341-
ss->host_=host;
352+
auto ss = std::make_shared<socket_memory>(std::move(*sock));
353+
ss->host_ = host;
342354
co_return ss;
343355
}
344356
co_return std::shared_ptr<memory>{};

0 commit comments

Comments
 (0)