Mempool data processing moved to async thread

This commit is contained in:
moneroexamples 2017-05-25 13:21:17 +08:00
parent eb50233146
commit 4500e640c3
5 changed files with 57 additions and 11 deletions

View file

@ -49,6 +49,7 @@ main(int ac, const char* av[])
auto no_blocks_on_index_opt = opts.get_option<string>("no-blocks-on-index");
auto testnet_url = opts.get_option<string>("testnet-url");
auto mainnet_url = opts.get_option<string>("mainnet-url");
auto network_info_timeout_opt = opts.get_option<string>("network-info-timeout");
auto testnet_opt = opts.get_option<bool>("testnet");
auto enable_key_image_checker_opt = opts.get_option<bool>("enable-key-image-checker");
auto enable_output_key_checker_opt = opts.get_option<bool>("enable-output-key-checker");
@ -63,6 +64,7 @@ main(int ac, const char* av[])
auto enable_emission_monitor_opt = opts.get_option<bool>("enable-emission-monitor");
bool testnet {*testnet_opt};
bool enable_pusher {*enable_pusher_opt};
bool enable_key_image_checker {*enable_key_image_checker_opt};
@ -152,6 +154,21 @@ main(int ac, const char* av[])
deamon_url = "http:://127.0.0.1:28081";
}
uint64_t network_info_timeout {3000};
try
{
network_info_timeout = boost::lexical_cast<uint64_t>(*network_info_timeout_opt);
}
catch (boost::bad_lexical_cast &e)
{
cout << "Cant cast " << (*network_info_timeout_opt) << " into number."
<< "Using default value of " << network_info_timeout << " milliseconds."
<< endl;
}
if (enable_emission_monitor == true)
{
@ -200,6 +217,7 @@ main(int ac, const char* av[])
enable_block_cache,
show_cache_times,
no_blocks_on_index,
network_info_timeout,
*testnet_url,
*mainnet_url);

View file

@ -55,6 +55,8 @@ namespace xmreg
"you can specify mainnet url, if you run it on testnet. link will show on front page to mainnet explorer")
("no-blocks-on-index", value<string>()->default_value("10"),
"number of last blocks to be shown on index page")
("network-info-timeout", value<string>()->default_value("3000"),
"maximum time, in milliseconds, to wait for network and mempool data availability")
("bc-path,b", value<string>(),
"path to lmdb folder of the blockchain, e.g., ~/.bitmonero/lmdb")
("ssl-crt-file", value<string>(),

View file

@ -35,6 +35,7 @@
#define TMPL_INDEX TMPL_DIR "/index.html"
#define TMPL_INDEX2 TMPL_DIR "/index2.html"
#define TMPL_MEMPOOL TMPL_DIR "/mempool.html"
#define TMPL_MEMPOOL_ERROR TMPL_DIR "/mempool_error.html"
#define TMPL_HEADER TMPL_DIR "/header.html"
#define TMPL_FOOTER TMPL_DIR "/footer.html"
#define TMPL_BLOCK TMPL_DIR "/block.html"
@ -261,6 +262,7 @@ namespace xmreg
uint64_t no_of_mempool_tx_of_frontpage;
uint64_t no_blocks_on_index;
uint64_t network_info_timeout;
string testnet_url;
string mainnet_url;
@ -335,6 +337,7 @@ namespace xmreg
bool _enable_block_cache,
bool _show_cache_times,
uint64_t _no_blocks_on_index,
uint64_t _network_info_timeout,
string _testnet_url,
string _mainnet_url)
: mcore {_mcore},
@ -352,6 +355,7 @@ namespace xmreg
enable_block_cache {_enable_block_cache},
show_cache_times {_show_cache_times},
no_blocks_on_index {_no_blocks_on_index},
network_info_timeout {_network_info_timeout},
testnet_url {_testnet_url},
mainnet_url {_mainnet_url},
mempool_tx_json_cache(1000),
@ -369,6 +373,7 @@ namespace xmreg
template_file["footer"] = get_footer();
template_file["index2"] = get_full_page(xmreg::read(TMPL_INDEX2));
template_file["mempool"] = xmreg::read(TMPL_MEMPOOL);
template_file["mempool_error"] = xmreg::read(TMPL_MEMPOOL_ERROR);
template_file["mempool_full"] = get_full_page(template_file["mempool"]);
template_file["block"] = get_full_page(xmreg::read(TMPL_BLOCK));
template_file["tx"] = get_full_page(xmreg::read(TMPL_TX));
@ -397,6 +402,8 @@ namespace xmreg
index2(uint64_t page_no = 0, bool refresh_page = false)
{
// we get network info, such as current hash rate
// but since this makes a rpc call to deamon, we make it as an async
// call. this way we dont have to wait with execution of the rest of the
@ -420,6 +427,9 @@ namespace xmreg
j_info["fee_per_kb"] = fee_estimated;
// get memory pool rendered template
j_info["mempool_html"] = mempool(false, no_of_mempool_tx_of_frontpage);
return j_info;
});
@ -734,11 +744,13 @@ namespace xmreg
context["cache_misses"] = cache_misses;
// now time to check if we have our networkinfo from network_info future
// wait a bit (200 millisecond max) if not, just in case, but we dont wait more.
// wait a bit (network_info_timeout millisecond max) if not, just in case, but we dont wait more.
// if its not ready by now, forget about it.
string mempool_html {"Cant get mempool_pool"};
std::future_status ftr_status = network_info_ftr.wait_for(
std::chrono::milliseconds(200));
std::chrono::milliseconds(network_info_timeout));
if (ftr_status == std::future_status::ready)
{
@ -758,16 +770,20 @@ namespace xmreg
}
context["network_info"] = mstch::map {
{"difficulty" , j_network_info["difficulty"].get<uint64_t>()},
{"hash_rate" , difficulty},
{"fee_per_kb" , print_money(j_network_info["fee_per_kb"])},
{"alt_blocks_no" , j_network_info["alt_blocks_count"].get<uint64_t>()}
{"difficulty" , j_network_info["difficulty"].get<uint64_t>()},
{"hash_rate" , difficulty},
{"fee_per_kb" , print_money(j_network_info["fee_per_kb"])},
{"alt_blocks_no" , j_network_info["alt_blocks_count"].get<uint64_t>()}
};
mempool_html = j_network_info["mempool_html"];
}
}
else
{
cerr << "network_info future not ready yet, skipping." << endl;
mempool_html = template_file["mempool_error"];
cerr << "network_info and mempool future not ready yet, skipping." << endl;
}
if (CurrentBlockchainStatus::is_thread_running())
@ -792,7 +808,7 @@ namespace xmreg
// get memory pool rendered template
string mempool_html = mempool(false, no_of_mempool_tx_of_frontpage);
//string mempool_html = mempool(false, no_of_mempool_tx_of_frontpage);
// append mempool_html to the index context map
context["mempool_info"] = mempool_html;
@ -804,8 +820,8 @@ namespace xmreg
}
/**
* Render mempool data
*/
* Render mempool data
*/
string
mempool(bool add_header_and_footer = false, uint64_t no_of_mempool_tx = 25)
{

View file

@ -16,7 +16,7 @@ h1, h2, h3, h4, h5, h6 {
padding: 10px;*/
}
tr, li, #pages {
tr, li, #pages, .info {
font-family: "Lucida Console", Monaco, monospace;
font-size : 12px;
height: 22px;

View file

@ -0,0 +1,10 @@
<h2 style="margin-bottom: 0px">
Memory pool
</h2>
<h4 style="font-size: 14px; margin-top: 0px"></h4>
<div class="center info" style="text-align: center;width:80%;color:#949490">
Newtork info and mempool data preparation failed.
Its processing took longer than expected and it timed out. Sorry.
</div>