From b38f7c4a7d79d7389c00a1fd7eb8137670514709 Mon Sep 17 00:00:00 2001 From: Qing Zhang Date: Mon, 24 Aug 2020 09:30:19 -0700 Subject: [PATCH] code optimization for new http_plugin option http-max-in-flight-requests --- plugins/http_plugin/http_plugin.cpp | 32 ++++++++--------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/plugins/http_plugin/http_plugin.cpp b/plugins/http_plugin/http_plugin.cpp index 87899536110..53c16ca11d8 100644 --- a/plugins/http_plugin/http_plugin.cpp +++ b/plugins/http_plugin/http_plugin.cpp @@ -136,8 +136,7 @@ namespace eosio { virtual bool verify_max_requests_in_flight() = 0; virtual void handle_exception() = 0; - virtual const void* operator* () const = 0; - virtual void* operator* () = 0; + virtual void update_connection(const std::string & body, int code) = 0; }; using abstract_conn_ptr = std::shared_ptr; @@ -329,7 +328,7 @@ namespace eosio { } template - void report_429_error( const T& con, string what) { + void report_429_error( const T& con, const std::string & what) { error_results::error_info ei; ei.code = websocketpp::http::status_code::too_many_requests; ei.name = "Busy"; @@ -405,20 +404,10 @@ namespace eosio { http_plugin_impl::handle_exception(_conn); } - /** - * const accessor - * @return const reference to the contained _conn - */ - const void* operator* () const override { - return (const void *) &_conn; - } - - /** - * mutable accessor (can be moved frmo) - * @return mutable reference to the contained _conn - */ - void* operator* () override { - return (void *) &_conn; + void update_connection(const std::string & body, int code) override { + _conn->set_body(std::move(body)); + _conn->set_status( websocketpp::http::status_code::value( code ) ); + _conn->send_http_response(); } detail::connection_ptr _conn; @@ -520,7 +509,7 @@ namespace eosio { auto next_ptr = std::make_shared(std::move(next)); return [this, priority, next_ptr=std::move(next_ptr)]( detail::abstract_conn_ptr conn, string r, string b, url_response_callback then ) mutable { auto tracked_b = make_in_flight(std::move(b), *this); - if (!conn->verify_max_bytes_in_flight() || !conn->verify_max_requests_in_flight()) { + if (!conn->verify_max_bytes_in_flight()) { return; } @@ -570,7 +559,7 @@ namespace eosio { auto make_http_response_handler( detail::abstract_conn_ptr abstract_conn_ptr ) { return [this, abstract_conn_ptr]( int code, fc::variant response ) { auto tracked_response = make_in_flight(std::move(response), *this); - if (!abstract_conn_ptr->verify_max_bytes_in_flight() || !abstract_conn_ptr->verify_max_requests_in_flight()) { + if (!abstract_conn_ptr->verify_max_bytes_in_flight()) { return; } @@ -579,10 +568,7 @@ namespace eosio { try { std::string json = fc::json::to_string( *(*tracked_response), fc::time_point::now() + max_response_time ); auto tracked_json = make_in_flight(std::move(json), *this); - auto con = *(static_cast*>(*(*abstract_conn_ptr))); - con->set_body( std::move( *(*tracked_json) ) ); - con->set_status( websocketpp::http::status_code::value( code ) ); - con->send_http_response(); + abstract_conn_ptr->update_connection(*(*tracked_json) , code); } catch( ... ) { abstract_conn_ptr->handle_exception(); }