From 7132b467e1d718a052b301189b6a502f557d1d81 Mon Sep 17 00:00:00 2001 From: Michiro Hibari Date: Wed, 26 Nov 2014 17:27:04 +0900 Subject: [PATCH] Modify ssl shutdown.(Add async shutdown) Ticket #34416 --- l7vsd/include/tcp_session.h | 1 + l7vsd/include/tcp_ssl_socket.h | 22 +++++++++++++++++++++- l7vsd/src/tcp_session.cpp | 38 +++++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/l7vsd/include/tcp_session.h b/l7vsd/include/tcp_session.h index 9784024c..4947def4 100644 --- a/l7vsd/include/tcp_session.h +++ b/l7vsd/include/tcp_session.h @@ -546,6 +546,7 @@ protected: virtual void down_thread_sorryserver_async_read_some_handler(const boost::system::error_code &error_code, std::size_t len); virtual void down_thread_sorryserver_handle_async_read_some(const TCP_PROCESS_TYPE_TAG); virtual void up_thread_client_ssl_socket_clear_socket_handler(); + virtual void up_thread_client_disconnect_handler(const boost::system::error_code &error_code); //! down thread receive from realserver and raise module event of handle_realserver_recv //! @param[in] process_type is process type diff --git a/l7vsd/include/tcp_ssl_socket.h b/l7vsd/include/tcp_ssl_socket.h index be91894f..64de5a4d 100644 --- a/l7vsd/include/tcp_ssl_socket.h +++ b/l7vsd/include/tcp_ssl_socket.h @@ -38,6 +38,7 @@ class tcp_ssl_socket : public basic_tcp_socket< ssl_socket > public: // typedef typedef boost::function< void (const boost::system::error_code &) > async_handshake_handler_t; + typedef boost::function< void (const boost::system::error_code &) > async_shutdown_handler_t; // constructor tcp_ssl_socket( @@ -124,6 +125,14 @@ public: return error_code ? false : true; } + virtual void async_shutdown(async_handshake_handler_t handler) { + boost::mutex::scoped_lock lock(ssl_mutex); + boost::system::error_code error_code; + shutdown_con++; + my_socket->lowest_layer().cancel(error_code); + my_socket->async_shutdown(ssl_strand.wrap(handler)); + } + virtual std::size_t read_some(const boost::asio::mutable_buffers_1 &buffers, boost::system::error_code &error_code) { boost::mutex::scoped_lock lock(ssl_mutex); if (write_con > 0 || handshake_con > 0) { @@ -203,9 +212,19 @@ public: return write_con; } + void decrement_shutdown_con() { + boost::mutex::scoped_lock lock(ssl_mutex); + shutdown_con--; + ssl_cond.notify_one(); + } + + int get_shutdown_con() { + return shutdown_con; + } + void wait_async_event_all_end() { boost::mutex::scoped_lock lock(ssl_mutex); - while (handshake_con > 0 || read_con > 0 || write_con > 0) { + while (handshake_con > 0 || read_con > 0 || write_con > 0 || shutdown_con > 0) { boost::format fmt("handshake_con : %d read_con = %d write_con = %d "); fmt % handshake_con % read_con % write_con ; Logger::putLogInfo(LOG_CAT_L7VSD_SESSION, 999, fmt.str(), __FILE__, __LINE__); @@ -225,6 +244,7 @@ protected: int handshake_con; int read_con; int write_con; + int shutdown_con; virtual void set_quickack(boost::system::error_code &error_code) { int err = ::setsockopt(my_socket->lowest_layer().native(), IPPROTO_TCP, TCP_QUICKACK, &opt_info.quickack_val, sizeof(opt_info.quickack_val)); diff --git a/l7vsd/src/tcp_session.cpp b/l7vsd/src/tcp_session.cpp index 1333c88d..f7d4bbc2 100644 --- a/l7vsd/src/tcp_session.cpp +++ b/l7vsd/src/tcp_session.cpp @@ -1494,7 +1494,12 @@ void tcp_session::up_thread_client_disconnect(const TCP_PROCESS_TYPE_TAG process fmt % boost::this_thread::get_id() % ec.message(); Logger::putLogInfo(LOG_CAT_L7VSD_SESSION, 999, fmt.str(), __FILE__, __LINE__); #endif - func_tag = UP_FUNC_CLIENT_DISCONNECT; + upthread_status = UPTHREAD_LOCK; + tcp_ssl_socket::async_shutdown_handler_t handler; + handler = boost::bind(&tcp_session::up_thread_client_disconnect_handler, this, boost::asio::placeholders::error); + client_ssl_socket.async_shutdown(handler); + up_thread_next_call_function = up_thread_function_array[UP_FUNC_CLIENT_DISCONNECT_EVENT]; + return; } else if (ec == boost::asio::error::eof) { #ifdef DEBUG boost::format fmt("Thread ID[%d] ssl_shutdown fail: %s"); @@ -4121,6 +4126,37 @@ void tcp_session::down_thread_sorryserver_async_read_some_handler(const boost::s downthread_status_cond.notify_one(); } +void tcp_session::up_thread_client_disconnect_handler(const boost::system::error_code &error_code) +{ + if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel(LOG_CAT_L7VSD_SESSION))) { + boost::format fmt("Thread ID[%d] FUNC IN up_thread_client_disconnect_handler"); + fmt % boost::this_thread::get_id(); + Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 999, fmt.str(), __FILE__, __LINE__); + } + + client_ssl_socket.decrement_shutdown_con(); + + tcp_thread_message *up_msg = new tcp_thread_message(); + if (error_code == boost::asio::error::try_again) { + tcp_ssl_socket::async_shutdown_handler_t handler; + handler = boost::bind(&tcp_session::up_thread_client_disconnect_handler, this, boost::asio::placeholders::error); + client_ssl_socket.async_shutdown(handler); + up_thread_next_call_function = up_thread_function_array[UP_FUNC_CLIENT_DISCONNECT_EVENT]; + return; + } else { + up_msg->message = up_que_function_map[UP_FUNC_CLIENT_DISCONNECT_EVENT]; + while (!up_thread_message_que.push(up_msg)) {} + } + + upthread_status_cond.notify_one(); + + if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel(LOG_CAT_L7VSD_SESSION))) { + boost::format fmt("Thread ID[%d] FUNC OUT up_thread_client_disconnect_handler"); + fmt % boost::this_thread::get_id() ; + Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 999, fmt.str(), __FILE__, __LINE__); + } +} + void tcp_session::up_thread_client_ssl_socket_clear_socket_handler() { if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel(LOG_CAT_L7VSD_SESSION))) { -- 2.11.0