Skip to content

Commit

Permalink
Handling rpc requests in a work thread instead of an io thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
clemahieu committed May 29, 2017
1 parent 20e0558 commit 3fdeb45
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions rai/node/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2025,40 +2025,43 @@ void rai::rpc_connection::parse_connection ()
{
if (!ec)
{
auto start (std::chrono::system_clock::now ());
auto version (this_l->request.version);
auto response_handler ([this_l, version, start] (boost::property_tree::ptree const & tree_a)
this_l->node->background ([this_l] ()
{
std::stringstream ostream;
boost::property_tree::write_json (ostream, tree_a);
ostream.flush ();
auto body (ostream.str ());
this_l->res.fields.insert ("content-type", "application/json");
this_l->res.fields.insert ("Access-Control-Allow-Origin", "*");
this_l->res.status = 200;
this_l->res.body = body;
this_l->res.version = version;
beast::http::async_write (this_l->socket, this_l->res, [this_l] (boost::system::error_code const & ec)
auto start (std::chrono::system_clock::now ());
auto version (this_l->request.version);
auto response_handler ([this_l, version, start] (boost::property_tree::ptree const & tree_a)
{
std::stringstream ostream;
boost::property_tree::write_json (ostream, tree_a);
ostream.flush ();
auto body (ostream.str ());
this_l->res.fields.insert ("content-type", "application/json");
this_l->res.fields.insert ("Access-Control-Allow-Origin", "*");
this_l->res.status = 200;
this_l->res.body = body;
this_l->res.version = version;
beast::http::async_write (this_l->socket, this_l->res, [this_l] (boost::system::error_code const & ec)
{
});
if (this_l->node->config.logging.log_rpc ())
{
BOOST_LOG (this_l->node->log) << boost::str (boost::format ("RPC request %2% completed in: %1% microseconds") % std::chrono::duration_cast <std::chrono::microseconds> (std::chrono::system_clock::now () - start).count () % boost::io::group (std::hex, std::showbase, reinterpret_cast <uintptr_t> (this_l.get ())));
}
});
if (this_l->node->config.logging.log_rpc ())
if (this_l->request.method () == "POST")
{
BOOST_LOG (this_l->node->log) << boost::str (boost::format ("RPC request %2% completed in: %1% microseconds") % std::chrono::duration_cast <std::chrono::microseconds> (std::chrono::system_clock::now () - start).count () % boost::io::group (std::hex, std::showbase, reinterpret_cast <uintptr_t> (this_l.get ())));
auto handler (std::make_shared <rai::rpc_handler> (*this_l->node, this_l->rpc, this_l->request.body, response_handler));
handler->process_request ();
if (this_l->node->config.logging.log_rpc ())
{
BOOST_LOG (this_l->node->log) << boost::str (boost::format ("RPC request %2% serviced in: %1% microseconds") % std::chrono::duration_cast <std::chrono::microseconds> (std::chrono::system_clock::now () - start).count () % boost::io::group (std::hex, std::showbase, reinterpret_cast <uintptr_t> (this_l.get ())));
}
}
});
if (this_l->request.method () == "POST")
{
auto handler (std::make_shared <rai::rpc_handler> (*this_l->node, this_l->rpc, this_l->request.body, response_handler));
handler->process_request ();
if (this_l->node->config.logging.log_rpc ())
else
{
BOOST_LOG (this_l->node->log) << boost::str (boost::format ("RPC request %2% serviced in: %1% microseconds") % std::chrono::duration_cast <std::chrono::microseconds> (std::chrono::system_clock::now () - start).count () % boost::io::group (std::hex, std::showbase, reinterpret_cast <uintptr_t> (this_l.get ())));
error_response (response_handler, "Can only POST requests");
}
}
else
{
error_response (response_handler, "Can only POST requests");
}
});
}
});
}
Expand Down

0 comments on commit 3fdeb45

Please sign in to comment.