Skip to content

Commit

Permalink
Automatically attempt to reconnect the web socket for read nodes to f…
Browse files Browse the repository at this point in the history
…orward TXs to a write node.
  • Loading branch information
Michael Vandeberg committed Feb 27, 2017
1 parent 0a095bd commit c659d73
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
32 changes: 28 additions & 4 deletions libraries/app/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,13 @@ namespace steemit { namespace app {

if( _app._read_only )
{
FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or non connected." );
// If we are not connected, attempt to connect once and then fail
if( !_app._remote_net_api )
{
_app.connect_to_write_node();
FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or not currently connected." );
}

(*_app._remote_net_api)->broadcast_transaction( trx );
}
else
Expand All @@ -225,7 +231,13 @@ namespace steemit { namespace app {
{
if( _app._read_only )
{
FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or non connected." );
// If we are not connected, attempt to connect once and then fail
if( !_app._remote_net_api )
{
_app.connect_to_write_node();
FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or not currently connected." );
}

return (*_app._remote_net_api)->broadcast_transaction_synchronous( trx );
}
else
Expand All @@ -242,7 +254,13 @@ namespace steemit { namespace app {
{
if( _app._read_only )
{
FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or non connected." );
// If we are not connected, attempt to connect once and then fail
if( !_app._remote_net_api )
{
_app.connect_to_write_node();
FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or not currently connected." );
}

(*_app._remote_net_api)->broadcast_block( b );
}
else
Expand All @@ -256,7 +274,13 @@ namespace steemit { namespace app {
{
if( _app._read_only )
{
FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or non connected." );
// If we are not connected, attempt to connect once and then fail
if( !_app._remote_net_api )
{
_app.connect_to_write_node();
FC_ASSERT( _app._remote_net_api, "Write node RPC not configured properly or not currently connected." );
}

(*_app._remote_net_api)->broadcast_transaction_with_callback( cb, trx );
}
else
Expand Down
19 changes: 14 additions & 5 deletions libraries/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,7 @@ namespace detail {
{
try
{
auto ws_ptr = _self->_client.connect( _options->at( "read-forward-rpc" ).as< string >() );
auto apic = std::make_shared< fc::rpc::websocket_api_connection >( *ws_ptr );
auto login = apic->get_remote_api< login_api >( 1 );
FC_ASSERT( login->login( "", "" ) );
_self->_remote_net_api = login->get_api_by_name( "network_broadcast_api" )->as< network_broadcast_api >();
_self->_remote_endpoint = _options->at( "read-forward-rpc" ).as< string >();
}
catch( fc::exception& e )
{
Expand Down Expand Up @@ -1036,6 +1032,19 @@ void application::get_max_block_age( int32_t& result )
my->get_max_block_age( result );
}

void application::connect_to_write_node()
{
if( _remote_endpoint )
{
_remote_net_api.reset();
auto ws_ptr = _client.connect( *_remote_endpoint );
auto apic = std::make_shared< fc::rpc::websocket_api_connection >( *ws_ptr );
auto login = apic->get_remote_api< login_api >( 1 );
FC_ASSERT( login->login( "", "" ) );
_remote_net_api = login->get_api_by_name( "network_broadcast_api" )->as< network_broadcast_api >();
}
}

void application::shutdown_plugins()
{
for( auto& entry : my->_plugins_enabled )
Expand Down
3 changes: 3 additions & 0 deletions libraries/app/include/steemit/app/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ namespace steemit { namespace app {

void get_max_block_age( int32_t& result );

void connect_to_write_node();

bool _read_only = true;
fc::optional< string > _remote_endpoint;
fc::optional< fc::api< network_broadcast_api > > _remote_net_api;
fc::optional< fc::api< login_api > > _remote_login;
fc::http::websocket_connection_ptr _ws_ptr;
Expand Down

0 comments on commit c659d73

Please sign in to comment.