Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Move json::to_string response processing to http thread pool #6893

Merged
merged 2 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions plugins/chain_api_plugin/chain_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ chain_api_plugin::~chain_api_plugin(){}
void chain_api_plugin::set_program_options(options_description&, options_description&) {}
void chain_api_plugin::plugin_initialize(const variables_map&) {}

struct async_result_visitor : public fc::visitor<std::string> {
struct async_result_visitor : public fc::visitor<fc::variant> {
template<typename T>
std::string operator()(const T& v) const {
return fc::json::to_string(v);
fc::variant operator()(const T& v) const {
return fc::variant(v);
}
};

Expand All @@ -41,8 +41,8 @@ struct async_result_visitor : public fc::visitor<std::string> {
api_handle.validate(); \
try { \
if (body.empty()) body = "{}"; \
auto result = api_handle.call_name(fc::json::from_string(body).as<api_namespace::call_name ## _params>()); \
cb(http_response_code, fc::json::to_string(result)); \
fc::variant result( api_handle.call_name(fc::json::from_string(body).as<api_namespace::call_name ## _params>()) ); \
cb(http_response_code, std::move(result)); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
Expand Down
2 changes: 1 addition & 1 deletion plugins/db_size_api_plugin/db_size_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using namespace eosio;
try { \
if (body.empty()) body = "{}"; \
INVOKE \
cb(http_response_code, fc::json::to_string(result)); \
cb(http_response_code, fc::variant(result)); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
Expand Down
2 changes: 1 addition & 1 deletion plugins/faucet_testnet_plugin/faucet_testnet_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ using results_pair = std::pair<uint32_t,fc::variant>;
try { \
if (body.empty()) body = "{}"; \
const auto result = api_handle->invoke_cb(body); \
response_cb(result.first, fc::json::to_string(result.second)); \
response_cb(result.first, fc::variant(result.second)); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, response_cb); \
} \
Expand Down
4 changes: 2 additions & 2 deletions plugins/history_api_plugin/history_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ void history_api_plugin::plugin_initialize(const variables_map&) {}
[api_handle](string, string body, url_response_callback cb) mutable { \
try { \
if (body.empty()) body = "{}"; \
auto result = api_handle.call_name(fc::json::from_string(body).as<api_namespace::call_name ## _params>()); \
cb(200, fc::json::to_string(result)); \
fc::variant result( api_handle.call_name(fc::json::from_string(body).as<api_namespace::call_name ## _params>()) ); \
cb(200, std::move(result)); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
Expand Down
32 changes: 17 additions & 15 deletions plugins/http_plugin/http_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,18 +304,20 @@ namespace eosio {
[ioc = this->server_ioc, &bytes_in_flight = this->bytes_in_flight, handler_itr,
resource{std::move( resource )}, body{std::move( body )}, con]() {
try {
bytes_in_flight -= body.size();
handler_itr->second( resource, body,
[ioc{std::move(ioc)}, &bytes_in_flight, con]( int code, std::string response_body ) {
bytes_in_flight += response_body.size();
boost::asio::post( *ioc, [ioc, response_body{std::move( response_body )}, &bytes_in_flight, con, code]() {
size_t body_size = response_body.size();
con->set_body( std::move( response_body ) );
[ioc{std::move(ioc)}, &bytes_in_flight, con]( int code, fc::variant response_body ) {
boost::asio::post( *ioc, [ioc, response_body{std::move( response_body )}, &bytes_in_flight, con, code]() mutable {
brianjohnson5972 marked this conversation as resolved.
Show resolved Hide resolved
std::string json = fc::json::to_string( response_body );
brianjohnson5972 marked this conversation as resolved.
Show resolved Hide resolved
response_body.clear();
const size_t json_size = json.size();
bytes_in_flight += json_size;
con->set_body( std::move( json ) );
con->set_status( websocketpp::http::status_code::value( code ) );
con->send_http_response();
bytes_in_flight -= body_size;
bytes_in_flight -= json_size;
} );
});
bytes_in_flight -= body.size();
} catch( ... ) {
handle_exception<T>( con );
con->send_http_response();
Expand Down Expand Up @@ -592,7 +594,7 @@ namespace eosio {
try {
if (body.empty()) body = "{}";
auto result = (*this).get_supported_apis();
cb(200, fc::json::to_string(result));
cb(200, fc::variant(result));
} catch (...) {
handle_exception("node", "get_supported_apis", body, cb);
}
Expand Down Expand Up @@ -629,36 +631,36 @@ namespace eosio {
throw;
} catch (chain::unknown_block_exception& e) {
error_results results{400, "Unknown Block", error_results::error_info(e, verbose_http_errors)};
cb( 400, fc::json::to_string( results ));
cb( 400, fc::variant( results ));
} catch (chain::unsatisfied_authorization& e) {
error_results results{401, "UnAuthorized", error_results::error_info(e, verbose_http_errors)};
cb( 401, fc::json::to_string( results ));
cb( 401, fc::variant( results ));
} catch (chain::tx_duplicate& e) {
error_results results{409, "Conflict", error_results::error_info(e, verbose_http_errors)};
cb( 409, fc::json::to_string( results ));
cb( 409, fc::variant( results ));
} catch (fc::eof_exception& e) {
error_results results{422, "Unprocessable Entity", error_results::error_info(e, verbose_http_errors)};
cb( 422, fc::json::to_string( results ));
cb( 422, fc::variant( results ));
elog( "Unable to parse arguments to ${api}.${call}", ("api", api_name)( "call", call_name ));
dlog("Bad arguments: ${args}", ("args", body));
} catch (fc::exception& e) {
error_results results{500, "Internal Service Error", error_results::error_info(e, verbose_http_errors)};
cb( 500, fc::json::to_string( results ));
cb( 500, fc::variant( results ));
if (e.code() != chain::greylist_net_usage_exceeded::code_value && e.code() != chain::greylist_cpu_usage_exceeded::code_value) {
elog( "FC Exception encountered while processing ${api}.${call}",
("api", api_name)( "call", call_name ));
dlog( "Exception Details: ${e}", ("e", e.to_detail_string()));
}
} catch (std::exception& e) {
error_results results{500, "Internal Service Error", error_results::error_info(fc::exception( FC_LOG_MESSAGE( error, e.what())), verbose_http_errors)};
cb( 500, fc::json::to_string( results ));
cb( 500, fc::variant( results ));
elog( "STD Exception encountered while processing ${api}.${call}",
("api", api_name)( "call", call_name ));
dlog( "Exception Details: ${e}", ("e", e.what()));
} catch (...) {
error_results results{500, "Internal Service Error",
error_results::error_info(fc::exception( FC_LOG_MESSAGE( error, "Unknown Exception" )), verbose_http_errors)};
cb( 500, fc::json::to_string( results ));
cb( 500, fc::variant( results ));
elog( "Unknown Exception encountered while processing ${api}.${call}",
("api", api_name)( "call", call_name ));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace eosio {
*
* Arguments: response_code, response_body
*/
using url_response_callback = std::function<void(int,string)>;
using url_response_callback = std::function<void(int,fc::variant)>;

/**
* @brief Callback type for a URL handler
Expand Down
4 changes: 2 additions & 2 deletions plugins/login_plugin/login_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ void login_plugin::plugin_initialize(const variables_map& options) {
try { \
if (body.empty()) \
body = "{}"; \
auto result = call_name(fc::json::from_string(body).as<login_plugin::call_name##_params>()); \
cb(http_response_code, fc::json::to_string(result)); \
fc::variant result( call_name(fc::json::from_string(body).as<login_plugin::call_name##_params>()) ); \
cb(http_response_code, std::move(result)); \
} catch (...) { \
http_plugin::handle_exception("login", #call_name, body, cb); \
} \
Expand Down
2 changes: 1 addition & 1 deletion plugins/net_api_plugin/net_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using namespace eosio;
try { \
if (body.empty()) body = "{}"; \
INVOKE \
cb(http_response_code, fc::json::to_string(result)); \
cb(http_response_code, fc::variant(result)); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
Expand Down
2 changes: 1 addition & 1 deletion plugins/producer_api_plugin/producer_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using namespace eosio;
try { \
if (body.empty()) body = "{}"; \
INVOKE \
cb(http_response_code, fc::json::to_string(result)); \
cb(http_response_code, fc::variant(result)); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
Expand Down
4 changes: 2 additions & 2 deletions plugins/test_control_api_plugin/test_control_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ struct async_result_visitor : public fc::visitor<std::string> {
[api_handle](string, string body, url_response_callback cb) mutable { \
try { \
if (body.empty()) body = "{}"; \
auto result = api_handle.call_name(fc::json::from_string(body).as<api_namespace::call_name ## _params>()); \
cb(http_response_code, fc::json::to_string(result)); \
fc::variant result( api_handle.call_name(fc::json::from_string(body).as<api_namespace::call_name ## _params>()) ); \
cb(http_response_code, std::move(result)); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
Expand Down
4 changes: 2 additions & 2 deletions plugins/txn_test_gen_plugin/txn_test_gen_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ using io_work_t = boost::asio::executor_work_guard<boost::asio::io_context::exec
try { \
if (body.empty()) body = "{}"; \
INVOKE \
cb(http_response_code, fc::json::to_string(result)); \
cb(http_response_code, fc::variant(result)); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
Expand Down Expand Up @@ -77,7 +77,7 @@ using io_work_t = boost::asio::executor_work_guard<boost::asio::io_context::exec
http_plugin::handle_exception(#api_name, #call_name, body, cb);\
}\
} else {\
cb(http_response_code, fc::json::to_string(eosio::detail::txn_test_gen_empty())); \
cb(http_response_code, fc::variant(eosio::detail::txn_test_gen_empty())); \
}\
};\
INVOKE \
Expand Down
2 changes: 1 addition & 1 deletion plugins/wallet_api_plugin/wallet_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using namespace eosio;
try { \
if (body.empty()) body = "{}"; \
INVOKE \
cb(http_response_code, fc::json::to_string(result)); \
cb(http_response_code, fc::variant(result)); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
Expand Down
2 changes: 1 addition & 1 deletion programs/keosd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int main(int argc, char** argv)
if(!app().initialize<wallet_plugin, wallet_api_plugin, http_plugin>(argc, argv))
return -1;
auto& http = app().get_plugin<http_plugin>();
http.add_handler("/v1/keosd/stop", [](string, string, url_response_callback cb) { cb(200, "{}"); std::raise(SIGTERM); } );
http.add_handler("/v1/keosd/stop", [](string, string, url_response_callback cb) { cb(200, fc::variant(fc::variant_object())); std::raise(SIGTERM); } );
app().startup();
app().exec();
} catch (const fc::exception& e) {
Expand Down