1+ #include < boost/asio/thread_pool.hpp>
2+ #include < boost/asio/post.hpp>
3+ #include < boost/thread.hpp>
4+ #include < boost/asio.hpp>
5+ #include < boost/bind/bind.hpp>
6+
17#include " session.hpp"
2- #include < memory>
3- #include < set>
48#include " log.hpp"
59#include " db.hpp"
10+
11+ #include < memory>
12+ #include < set>
13+
614using boost::asio::ip::tcp;
15+ static boost::asio::thread_pool thread_pool_ (4 ); // 4 threads in the pool
716
817std::unordered_map<std::string, uint32_t > duckdb_to_pg_type = {
918 {" BOOLEAN" , 16 }, // PG: bool
@@ -23,6 +32,13 @@ std::unordered_map<std::string, uint32_t> duckdb_to_pg_type = {
2332 // 添加更多类型映射...
2433};
2534
35+ boost::asio::io_context &
36+ PGSession::get_io_context ()
37+ {
38+ static boost::asio::io_context io_context_;
39+ return io_context_;
40+ }
41+
2642// 解析启动参数
2743void PGSession::parse_startup_params (const char *data, size_t length)
2844{
@@ -189,7 +205,13 @@ void PGSession::handle_simple_query()
189205 if (!ec)
190206 {
191207 // 处理查询并返回结果
192- self->process_query ();
208+ boost::asio::post (thread_pool_,
209+ [self]()
210+ {
211+ self->process_query ();
212+ });
213+ // 继续处理下一个查询
214+ self->handle_query ();
193215 }
194216 });
195217 }
@@ -366,9 +388,6 @@ void PGSession::process_query()
366388 }
367389 // 3. 发送ReadyForQuery
368390 send_ready_for_query ();
369-
370- // 继续处理下一个查询
371- handle_query ();
372391}
373392
374393// 发送行描述
@@ -439,7 +458,9 @@ void PGSession::send_row_description(const std::vector<ColumnDesc> &columns)
439458 std::copy (reinterpret_cast <uint8_t *>(&net_len),
440459 reinterpret_cast <uint8_t *>(&net_len) + 4 ,
441460 msg.begin () + len_pos);
442- asio::write (socket_, asio::buffer (msg));
461+
462+ boost::asio::post (get_io_context (), [self = shared_from_this (), msg]()
463+ { asio::write (self->socket_ , asio::buffer (msg)); });
443464}
444465
445466// 发送数据行
@@ -475,7 +496,11 @@ void PGSession::send_data_row(const std::vector<std::string> &values)
475496 msg.insert (msg.end (), val.begin (), val.end ());
476497 }
477498
478- asio::write (socket_, asio::buffer (msg));
499+ boost::asio::post (get_io_context (),
500+ [self = shared_from_this (), msg]()
501+ {
502+ asio::write (self->socket_ , asio::buffer (msg));
503+ });
479504}
480505
481506// 发送命令完成
@@ -493,12 +518,20 @@ void PGSession::send_command_complete(const std::string &tag)
493518 msg.insert (msg.end (), tag.begin (), tag.end ());
494519 msg.push_back (' \0 ' );
495520
496- asio::write (socket_, asio::buffer (msg));
521+ boost::asio::post (get_io_context (),
522+ [self = shared_from_this (), msg]()
523+ {
524+ asio::write (self->socket_ , asio::buffer (msg));
525+ });
497526}
498527
499528// 发送ReadyForQuery
500529void PGSession::send_ready_for_query ()
501530{
502- std::vector<char > ready = {' Z' , 0 , 0 , 0 , 5 , ' I' };
503- asio::write (socket_, asio::buffer (ready));
531+ boost::asio::post (get_io_context (),
532+ [self = shared_from_this ()]()
533+ {
534+ std::vector<char > ready = {' Z' , 0 , 0 , 0 , 5 , ' I' };
535+ asio::write (self->socket_ , asio::buffer (ready));
536+ });
504537}
0 commit comments