From fe8c34eb176b9a2fa0e3a02e1230aac0e39de43a Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Sat, 7 Mar 2020 06:54:52 +0100 Subject: [PATCH] improved timeouts for mobile wallet --- contrib/epee/include/net/http_client.h | 24 +++++++++++++++++++----- src/wallet/core_default_rpc_proxy.cpp | 19 +++++++++++++++++-- src/wallet/core_default_rpc_proxy.h | 20 ++++++++++++++++---- src/wallet/wallets_manager.cpp | 8 +++++--- 4 files changed, 57 insertions(+), 14 deletions(-) diff --git a/contrib/epee/include/net/http_client.h b/contrib/epee/include/net/http_client.h index 28e6a30b..28064e5a 100644 --- a/contrib/epee/include/net/http_client.h +++ b/contrib/epee/include/net/http_client.h @@ -230,7 +230,10 @@ using namespace std; blocked_mode_client m_net_client; std::string m_host_buff; std::string m_port; - unsigned int m_timeout; + //unsigned int m_timeout; + unsigned int m_connection_timeout; + unsigned int m_invoke_timeout; + unsigned int m_recv_timeout; std::string m_header_cache; http_response_info m_response_info; size_t m_len_in_summary; @@ -259,14 +262,25 @@ using namespace std; { return connect(host, std::to_string(port), timeout); } - bool connect(const std::string& host, const std::string& port, unsigned int timeout) + + bool set_timeouts(unsigned int connection_timeout, unsigned int invoke_timeout, unsigned int recv_timeout) + { + + } + + bool connect(const std::string& host, std::string port) { CRITICAL_REGION_LOCAL(m_lock); m_host_buff = host; m_port = port; - m_timeout = timeout; - return m_net_client.connect(host, port, timeout, timeout); + return m_net_client.connect(host, port, m_connection_timeout, m_recv_timeout); + } + + bool connect(const std::string& host, const std::string& port, unsigned int timeout) + { + m_connection_timeout = m_invoke_timeout = m_recv_timeout = timeout; + return connect(host, port); } //--------------------------------------------------------------------------- bool disconnect() @@ -303,7 +317,7 @@ using namespace std; if(!is_connected()) { LOG_PRINT("Reconnecting...", LOG_LEVEL_3); - if(!connect(m_host_buff, m_port, m_timeout)) + if(!connect(m_host_buff, m_port)) { LOG_PRINT("Failed to connect to " << m_host_buff << ":" << m_port, LOG_LEVEL_3); return false; diff --git a/src/wallet/core_default_rpc_proxy.cpp b/src/wallet/core_default_rpc_proxy.cpp index ae6123bd..320a6681 100644 --- a/src/wallet/core_default_rpc_proxy.cpp +++ b/src/wallet/core_default_rpc_proxy.cpp @@ -11,6 +11,11 @@ using namespace epee; #include "currency_core/currency_format_utils.h" #include "currency_core/alias_helper.h" +#undef LOG_DEFAULT_CHANNEL +#define LOG_DEFAULT_CHANNEL "rpc_proxy" +ENABLE_CHANNEL_BY_DEFAULT("rpc_proxy") + + namespace tools { bool default_http_core_proxy::set_connection_addr(const std::string& url) @@ -131,7 +136,7 @@ namespace tools epee::net_utils::parse_url(m_daemon_address, u); if (!u.port) u.port = 8081; - return m_http_client.connect(u.host, std::to_string(u.port), WALLET_RCP_CONNECTION_TIMEOUT); + return m_http_client.connect(u.host, std::to_string(u.port), m_connection_timeout); } //------------------------------------------------------------------------------------------------------------------------------ bool default_http_core_proxy::call_COMMAND_RPC_GET_ALL_ALIASES(currency::COMMAND_RPC_GET_ALL_ALIASES::response& res) @@ -166,7 +171,17 @@ namespace tools m_plast_daemon_is_disconnected = plast_daemon_is_disconnected ? plast_daemon_is_disconnected : &m_last_daemon_is_disconnected_stub; } //------------------------------------------------------------------------------------------------------------------------------ - default_http_core_proxy::default_http_core_proxy():m_plast_daemon_is_disconnected(&m_last_daemon_is_disconnected_stub) + bool default_http_core_proxy::set_connectivity(unsigned int connection_timeout, size_t repeats_count) + { + m_connection_timeout = connection_timeout; + m_attempts_count = repeats_count; + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + default_http_core_proxy::default_http_core_proxy() :m_plast_daemon_is_disconnected(&m_last_daemon_is_disconnected_stub), + m_last_success_interract_time(0), + m_connection_timeout(WALLET_RCP_CONNECTION_TIMEOUT), + m_attempts_count(WALLET_RCP_COUNT_ATTEMNTS) { } diff --git a/src/wallet/core_default_rpc_proxy.h b/src/wallet/core_default_rpc_proxy.h index c855b212..7180edb8 100644 --- a/src/wallet/core_default_rpc_proxy.h +++ b/src/wallet/core_default_rpc_proxy.h @@ -11,9 +11,11 @@ #include "core_rpc_proxy.h" #include "storages/http_abstract_invoke.h" -#define WALLET_RCP_CONNECTION_TIMEOUT 10000 +#define WALLET_RCP_CONNECTION_TIMEOUT 3000 #define WALLET_RCP_COUNT_ATTEMNTS 3 + + namespace tools { class default_http_core_proxy final : public i_core_proxy @@ -22,6 +24,7 @@ namespace tools bool set_connection_addr(const std::string& url) override; + bool set_connectivity(unsigned int connection_timeout, size_t repeats_count); bool call_COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES(const currency::COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::request& rqt, currency::COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::response& rsp) override; bool call_COMMAND_RPC_GET_BLOCKS_FAST(const currency::COMMAND_RPC_GET_BLOCKS_FAST::request& rqt, currency::COMMAND_RPC_GET_BLOCKS_FAST::response& rsp) override; bool call_COMMAND_RPC_GET_BLOCKS_DIRECT(const currency::COMMAND_RPC_GET_BLOCKS_DIRECT::request& rqt, currency::COMMAND_RPC_GET_BLOCKS_DIRECT::response& rsp) override; @@ -58,7 +61,7 @@ namespace tools CRITICAL_REGION_LOCAL(m_lock); bool ret = false; - for(size_t i = WALLET_RCP_COUNT_ATTEMNTS; i && !ret; --i) + for(size_t i = m_attempts_count; i && !ret; --i) { ret = request(); } @@ -82,7 +85,7 @@ namespace tools inline bool invoke_http_bin_remote_command2_update_is_disconnect(const std::string& url, const t_request& req, t_response& res) { return call_request([&](){ - return epee::net_utils::invoke_http_bin_remote_command2(m_daemon_address + url, req, res, m_http_client, WALLET_RCP_CONNECTION_TIMEOUT); + return epee::net_utils::invoke_http_bin_remote_command2(m_daemon_address + url, req, res, m_http_client, m_connection_timeout); }); } @@ -90,7 +93,14 @@ namespace tools inline bool invoke_http_json_remote_command2_update_is_disconnect(const std::string& url, const t_request& req, t_response& res) { return call_request([&](){ - return epee::net_utils::invoke_http_json_remote_command2(m_daemon_address + url, req, res, m_http_client, WALLET_RCP_CONNECTION_TIMEOUT); +#ifdef MOBILE_WALLET_BUILD + LOG_PRINT_L0("[INVOKE_JSON] --->" << typeid(t_request).name() ) +#endif + bool r = epee::net_utils::invoke_http_json_remote_command2(m_daemon_address + url, req, res, m_http_client, m_connection_timeout); +#ifdef MOBILE_WALLET_BUILD + LOG_PRINT_L0("[INVOKE_JSON] <---" << typeid(t_request).name()) +#endif + return r; }); } //------------------------------------------------------------------------------------------------------------------------------ @@ -105,6 +115,8 @@ namespace tools std::atomic m_last_success_interract_time; std::atomic *m_plast_daemon_is_disconnected; std::atomic m_last_daemon_is_disconnected_stub; + unsigned int m_connection_timeout; + size_t m_attempts_count; }; } diff --git a/src/wallet/wallets_manager.cpp b/src/wallet/wallets_manager.cpp index 06b7a3c6..2aedec2e 100644 --- a/src/wallet/wallets_manager.cpp +++ b/src/wallet/wallets_manager.cpp @@ -29,7 +29,9 @@ if (it == m_wallets.end()) \ return API_RETURN_CODE_WALLET_WRONG_ID; \ auto& name = it->second.w; -#define DAEMON_IDLE_UPDATE_TIME_MS 1000 +#define DAEMON_IDLE_UPDATE_TIME_MS 2000 +#define HTTP_PROXY_TIMEOUT 2000 +#define HTTP_PROXY_ATTEMPTS_COUNT 1 wallets_manager::wallets_manager():m_pview(&m_view_stub), m_stop_singal_sent(false), @@ -234,6 +236,7 @@ bool wallets_manager::init(int argc, char* argv[], view::i_view* pview_handler) m_remote_node_mode = true; auto proxy_ptr = new tools::default_http_core_proxy(); proxy_ptr->set_plast_daemon_is_disconnected(&m_last_daemon_is_disconnected); + proxy_ptr->set_connectivity(HTTP_PROXY_TIMEOUT, HTTP_PROXY_ATTEMPTS_COUNT); m_rpc_proxy.reset(proxy_ptr); m_rpc_proxy->set_connection_addr(command_line::get_arg(m_vm, arg_remote_node)); } @@ -505,10 +508,9 @@ bool wallets_manager::update_state_info() dsi.is_disconnected = true; m_last_daemon_network_state = dsi.daemon_network_state; m_pview->update_daemon_status(dsi); - LOG_ERROR("Failed to call get_info"); + LOG_PRINT_RED_L0("Failed to call get_info"); return false; } - m_is_pos_allowed = inf.pos_allowed; dsi.alias_count = inf.alias_count; dsi.pow_difficulty = std::to_string(inf.pow_difficulty);