Skip to content

Commit 5bb1141

Browse files
committed
Replace custom exit waiter with UserExitOnSignalWaiter
- Removed manual exit signal implementation using promise/future - Added new `aos/exit_waiter/user_exit_on_signal/waiter.h` include - Replaced custom wait logic with `UserExitOnSignalWaiter` class - Cleaned up commented-out old implementation - Maintained same functionality with cleaner interface
1 parent 7de5a56 commit 5bb1141

File tree

5 files changed

+116
-10
lines changed

5 files changed

+116
-10
lines changed

aos/aos.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include "aos/executer_provider/i_executor_provider.h"
3232
#include "aos/executor_strategy/executor_strategy.h"
3333
#include "aos/executor_strategy/i_executor_strategy.h"
34+
#include "aos/exit_waiter/i_exit_waiter.h"
35+
#include "aos/exit_waiter/user_exit_on_signal/waiter.h"
36+
#include "aos/exit_waiter/user_exit_on_timeout/waiter.h"
3437
#include "aos/hash_utils/decompose_hash.h"
3538
#include "aos/histogram/histogram_calculator.h"
3639
#include "aos/histogram/i_histogram_calculator.h"

aos/exit_waiter/i_exit_waiter.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
namespace aos {
4+
struct IExitWaiterInterface {
5+
virtual void Wait() = 0;
6+
virtual ~IExitWaiterInterface() = default;
7+
};
8+
struct IAsyncExitWaiterInterface {
9+
virtual boost::asio::awaitable<void> WaitAsync() = 0;
10+
virtual ~IAsyncExitWaiterInterface() = default;
11+
};
12+
13+
} // namespace aos
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
#include <atomic>
3+
#include <csignal>
4+
#include <future>
5+
6+
#include "aos/exit_waiter/i_exit_waiter.h"
7+
#include "aos/logger/mylog.h"
8+
#include "boost/asio/io_context.hpp"
9+
#include "boost/asio/signal_set.hpp"
10+
#include "boost/asio/this_coro.hpp"
11+
12+
namespace aos {
13+
namespace impl {
14+
class UserExitOnSignalWaiter : public IExitWaiterInterface {
15+
public:
16+
explicit UserExitOnSignalWaiter(boost::asio::thread_pool& pool)
17+
: signal_set_(pool.get_executor(), SIGINT, SIGTERM) {}
18+
19+
void Wait() override {
20+
std::promise<void> promise;
21+
auto future = promise.get_future();
22+
23+
signal_set_.async_wait([&](const boost::system::error_code&,
24+
int signal_number) { promise.set_value(); });
25+
26+
future.wait(); // блокируемся здесь
27+
}
28+
29+
private:
30+
boost::asio::signal_set signal_set_;
31+
};
32+
33+
}; // namespace impl
34+
}; // namespace aos
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
#include <atomic>
3+
#include <future>
4+
#include <csignal>
5+
6+
#include "aos/exit_waiter/i_exit_waiter.h"
7+
#include "boost/asio/io_context.hpp"
8+
#include "boost/asio/signal_set.hpp"
9+
#include "boost/asio/this_coro.hpp"
10+
#include "aos/logger/my_logger.h"
11+
12+
namespace aos {
13+
namespace impl {
14+
class UserExitOnTimeoutWaiter : public IExitWaiterInterface {
15+
public:
16+
UserExitOnTimeoutWaiter(boost::asio::thread_pool& pool,
17+
std::chrono::steady_clock::duration timeout)
18+
: timer_(pool.get_executor(), timeout) {}
19+
20+
void Wait() override {
21+
std::promise<void> promise;
22+
auto future = promise.get_future();
23+
24+
timer_.async_wait([&](const boost::system::error_code& ec) {
25+
if (!ec) {
26+
logi("Timeout reached");
27+
} else {
28+
loge("Timer error: {}", ec.message());
29+
}
30+
promise.set_value();
31+
});
32+
33+
future.wait(); // блокируемся здесь
34+
}
35+
36+
private:
37+
boost::asio::steady_timer timer_;
38+
};
39+
40+
} // namespace impl
41+
} // namespace aos

examples/example47/main.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "aos/strategy_engine/i_strategy_engine.h"
1717
#include "aos/strategy_engine/strategy_engine.h"
1818
#include "aos/trading_pair/trading_pair.h"
19+
#include "aos/exit_waiter/user_exit_on_signal/waiter.h"
1920

2021
namespace str {
2122
struct Config {
@@ -128,26 +129,39 @@ class StrategyWrapper {
128129
};
129130
}; // namespace str
130131

131-
void wait_for_user_input(std::promise<void>& exit_signal) {
132-
std::cout << "Press ENTER to exit...\n";
133-
std::cin.get(); // ждём нажатия Enter
134-
exit_signal.set_value(); // снимаем блокировку
135-
}
132+
// class UserExitWaiter {
133+
// public:
134+
// explicit UserExitWaiter(boost::asio::thread_pool& pool)
135+
// : exit_future_(exit_promise_.get_future()) {
136+
// boost::asio::post(pool, [this] {
137+
// std::cout << "Press ENTER to exit...\n";
138+
// std::cin.get();
139+
// exit_promise_.set_value();
140+
// });
141+
// }
142+
143+
// void Wait() { exit_future_.wait(); }
144+
145+
// private:
146+
// std::promise<void> exit_promise_;
147+
// std::future<void> exit_future_;
148+
// };
136149

137150
int main(int, char**) {
138151
{
139152
try {
140153
boost::asio::thread_pool thread_pool;
154+
aos::impl::UserExitOnSignalWaiter user_waiter(thread_pool);
141155
using HashT = uint64_t;
142156
using Price = double;
143157
fmtlog::setLogLevel(fmtlog::LogLevel::DBG);
144158
// --- механизм блокировки через future ---
145-
std::promise<void> exit_promise;
146-
std::future<void> exit_future = exit_promise.get_future();
159+
// std::promise<void> exit_promise;
160+
// std::future<void> exit_future = exit_promise.get_future();
147161

148162
// Поток, ожидающий пользовательский ввод
149-
std::jthread input_thread(wait_for_user_input,
150-
std::ref(exit_promise));
163+
// std::jthread input_thread(wait_for_user_input,
164+
// std::ref(exit_promise));
151165
// boost::asio::thread_pool thread_pool;
152166
LogPolling log_polling(thread_pool, std::chrono::microseconds(1));
153167
using Price = double;
@@ -190,7 +204,8 @@ int main(int, char**) {
190204
aos::TradingPair::kETHUSDT);
191205

192206
binance_futures_main_net_infrastructure->StartAsync();
193-
exit_future.get(); // блокирующий вызов — будет ждать ENTER
207+
// exit_future.get(); // блокирующий вызов — будет ждать ENTER
208+
user_waiter.Wait();
194209
binance_futures_main_net_infrastructure->StopAsync();
195210
} catch (...) {
196211
loge("error occured");

0 commit comments

Comments
 (0)