perf: Send Binance Usds Futs md rest reqs on background thread

This commit is contained in:
John Colvin committed 2025-04-29 19:32:21 +00:00
1 parent 8e93856767
commit d72ebdf421
1 file changed
+30 -26
@@ -200,6 +200,7 @@ struct BinanceUsdsFuturesSubscription::PImpl {
unordered_map<string, DepthHandler> depth_handlers;
unordered_map<string, TickerHandler> ticker_handlers;
unordered_map<string, StatsHandler> stats_handlers;
std::thread start_thread;
explicit PImpl(
ccapi::EventDispatcher *dispatcher,
@@ -219,32 +220,35 @@ struct BinanceUsdsFuturesSubscription::PImpl {
// Request the list of all spots at Binance. Match the results against our SecurityCriteria,
// and subscribe to md for each matching spot.
std::string exch_info_url = this->cfg.url_rest_base();
exch_info_url.append("/fapi/v1/exchangeInfo");
bts::http::HttpResponse exch_info_res;
bts::http::HttpClient::get(exch_info_url, {}, &exch_info_res);
std::vector<domain::Security> secs;
BinanceUsdsFuture::parse(exch_info_res.body, secs);
// Exclude secs that don't satisfy the SecurityCriteria
auto is_sec_excluded = [this](const Security &sec) {
return sec.type() != api::domain::SecurityType::SECURITY_TYPE_FUTURE ||
!criteria.includes_or_undefined(sec) ||
CONTAINS(this->cfg.excluded_securities(), sec.desc()) ||
known_sec_names.find(sec.desc()) != known_sec_names.end();
};
REMOVE_IF(secs, is_sec_excluded);
// Assign security ids
for (auto &sec : secs) {
// Since the ids haven't been set yet, we should expect them to be 0
bts_assert(sec.id().value() == 0);
SecurityID sec_id{
sec_id_store->get_or_add(VENUE, sec.type(), sec.desc())};
sec.security_common.id = sec_id;
}
auto sec_descs = get_sec_descs(secs);
LOG(md, Info, "Received BinanceUsdsFuture securities:\n{}",
fmt::to_string(fmt::join(sec_descs, "\n")));
on_securities(secs);
start_thread = std::thread{[this]() {
std::string exch_info_url = this->cfg.url_rest_base();
exch_info_url.append("/fapi/v1/exchangeInfo");
bts::http::HttpResponse exch_info_res;
bts::http::HttpClient::get(exch_info_url, {}, &exch_info_res);
std::vector<domain::Security> secs;
BinanceUsdsFuture::parse(exch_info_res.body, secs);
// Exclude secs that don't satisfy the SecurityCriteria
auto is_sec_excluded = [this](const Security &sec) {
return sec.type() !=
api::domain::SecurityType::SECURITY_TYPE_FUTURE ||
!criteria.includes_or_undefined(sec) ||
CONTAINS(this->cfg.excluded_securities(), sec.desc()) ||
known_sec_names.find(sec.desc()) != known_sec_names.end();
};
REMOVE_IF(secs, is_sec_excluded);
// Assign security ids
for (auto &sec : secs) {
// Since the ids haven't been set yet, we should expect them to be 0
bts_assert(sec.id().value() == 0);
SecurityID sec_id{
this->sec_id_store->get_or_add(VENUE, sec.type(), sec.desc())};
sec.security_common.id = sec_id;
}
auto sec_descs = get_sec_descs(secs);
LOG(md, Info, "Received BinanceUsdsFuture securities:\n{}",
fmt::to_string(fmt::join(sec_descs, "\n")));
on_securities(secs);
}};
}
SecurityID get_sec_id(SecurityType sec_type, const string &sec_desc) {