diff --git a/src/lib_binance_execution/binance_execution/cross_margin/rest_api_client.cpp b/src/lib_binance_execution/binance_execution/cross_margin/rest_api_client.cpp index 7e7164c67..c8cd8ecb2 100644 --- a/src/lib_binance_execution/binance_execution/cross_margin/rest_api_client.cpp +++ b/src/lib_binance_execution/binance_execution/cross_margin/rest_api_client.cpp @@ -2,6 +2,7 @@ #include "lib/io/http/http_client.h" #include "lib/io/http/http_response.h" #include "lib/utils/crypto/hmac.h" +#include "lib/utils/type_name.h" namespace binance_execution::cross_margin { @@ -18,16 +19,24 @@ using std::string; #define ERROR_FN(fn, ...) _LOG_FN(bts::Error, fn, __VA_ARGS__) template -static void shrink_string(std::string &str) { +static void shrink_str(std::string &str, const char *name) { if (str.size() > MAX_SIZE) { + WARN_FN( + "shrink_str", + "Shrinking {} buffer size from {} to {} (max resting size is {})", + std::string{name}, str.size(), DEFAULT_SIZE, MAX_SIZE); str.resize(DEFAULT_SIZE); str.shrink_to_fit(); } } template -static void shrink_vec(std::vector &vec) { +static void shrink_vec(std::vector &vec, const char *name) { if (vec.size() > MAX_SIZE) { + WARN_FN( + "shrink_vec", + "Shrinking {} buffer size from {} to {} (max resting size is {})", + std::string{name}, vec.size(), DEFAULT_SIZE, MAX_SIZE); vec.resize(DEFAULT_SIZE); vec.shrink_to_fit(); } @@ -57,7 +66,8 @@ void RestApiClient::request_( // Use thread_local vars to reduce memory allocations. Shrink strings // to reduce memory footprint from previous requests. thread_local bts::http::HttpRequest http_req; - shrink_string<4096, 128>(http_req.body); + shrink_str<4096, 128>( + http_req.body, bts::utils::cached_type_name()); http_req.reset(); // Prepare request http_req.url = url_; @@ -66,7 +76,9 @@ void RestApiClient::request_( http_req.method = method; http_req.headers["X-MBX-APIKEY"] = api_key_; thread_local std::string qps; - shrink_string<256, 16>(qps); + static const std::string query_params_name = + fmt::format("{}_params", bts::utils::cached_type_name()); + shrink_str<256, 16>(qps, query_params_name.c_str()); qps.clear(); to_query_params(qps, req); if (!qps.empty()) { @@ -78,7 +90,8 @@ void RestApiClient::request_( } // Send it thread_local HttpResponse http_res; - shrink_string<4096, 128>(http_res.body); + shrink_str<4096, 128>( + http_res.body, bts::utils::cached_type_name()); http_res.reset(); HttpClient::send(http_req, &http_res); res.http_code = http_res.code; diff --git a/src/lib_binance_execution/binance_execution/spots/rest_api_client.cpp b/src/lib_binance_execution/binance_execution/spots/rest_api_client.cpp index 58affcb28..d993b4ded 100644 --- a/src/lib_binance_execution/binance_execution/spots/rest_api_client.cpp +++ b/src/lib_binance_execution/binance_execution/spots/rest_api_client.cpp @@ -4,6 +4,7 @@ #include "lib/io/http/http_response.h" #include "lib/io/json_formatter.h" #include "lib/utils/crypto/hmac.h" +#include "lib/utils/type_name.h" namespace binance_execution::spots { @@ -20,16 +21,24 @@ using std::string; #define ERROR_FN(fn, ...) _LOG_FN(bts::Error, fn, __VA_ARGS__) template -static void shrink_string(std::string &str) { +static void shrink_str(std::string &str, const char *name) { if (str.size() > MAX_SIZE) { + WARN_FN( + "shrink_str", + "Shrinking {} buffer size from {} to {} (max resting size is {})", + std::string{name}, str.size(), DEFAULT_SIZE, MAX_SIZE); str.resize(DEFAULT_SIZE); str.shrink_to_fit(); } } template -static void shrink_vec(std::vector &vec) { +static void shrink_vec(std::vector &vec, const char *name) { if (vec.size() > MAX_SIZE) { + WARN_FN( + "shrink_vec", + "Shrinking {} buffer size from {} to {} (max resting size is {})", + std::string{name}, vec.size(), DEFAULT_SIZE, MAX_SIZE); vec.resize(DEFAULT_SIZE); vec.shrink_to_fit(); } @@ -59,7 +68,8 @@ void RestApiClient::request_( // Use thread_local vars to reduce memory allocations. Shrink strings // to reduce memory footprint from previous requests. thread_local bts::http::HttpRequest http_req; - shrink_string<4096, 128>(http_req.body); + shrink_str<4096, 128>( + http_req.body, bts::utils::cached_type_name()); http_req.reset(); // Prepare request http_req.url = url_; @@ -68,7 +78,9 @@ void RestApiClient::request_( http_req.method = method; http_req.headers["X-MBX-APIKEY"] = api_key_; thread_local std::string qps; - shrink_string<256, 16>(qps); + static const std::string query_params_name = + fmt::format("{}_params", bts::utils::cached_type_name()); + shrink_str<256, 16>(qps, query_params_name.c_str()); qps.clear(); to_query_params(qps, req); if (!qps.empty()) { @@ -80,7 +92,8 @@ void RestApiClient::request_( } // Send it thread_local HttpResponse http_res; - shrink_string<4096, 128>(http_res.body); + shrink_str<4096, 128>( + http_res.body, bts::utils::cached_type_name()); http_res.reset(); HttpClient::send(http_req, &http_res); res.http_code = http_res.code; diff --git a/src/lib_binance_execution/binance_execution/usds_futures/rest_api_client.cpp b/src/lib_binance_execution/binance_execution/usds_futures/rest_api_client.cpp index e061fcc77..2b71471a3 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/rest_api_client.cpp +++ b/src/lib_binance_execution/binance_execution/usds_futures/rest_api_client.cpp @@ -2,6 +2,7 @@ #include "lib/io/http/http_client.h" #include "lib/io/http/http_response.h" #include "lib/utils/crypto/hmac.h" +#include "lib/utils/type_name.h" namespace binance_execution::usds_futures { @@ -18,16 +19,24 @@ using std::string; #define ERROR_FN(fn, ...) _LOG_FN(bts::Error, fn, __VA_ARGS__) template -static void shrink_string(std::string &str) { +static void shrink_str(std::string &str, const char *name) { if (str.size() > MAX_SIZE) { + WARN_FN( + "shrink_str", + "Shrinking {} buffer size from {} to {} (max resting size is {})", + std::string{name}, str.size(), DEFAULT_SIZE, MAX_SIZE); str.resize(DEFAULT_SIZE); str.shrink_to_fit(); } } template -static void shrink_vec(std::vector &vec) { +static void shrink_vec(std::vector &vec, const char *name) { if (vec.size() > MAX_SIZE) { + WARN_FN( + "shrink_vec", + "Shrinking {} buffer size from {} to {} (max resting size is {})", + std::string{name}, vec.size(), DEFAULT_SIZE, MAX_SIZE); vec.resize(DEFAULT_SIZE); vec.shrink_to_fit(); } @@ -57,7 +66,8 @@ void RestApiClient::request_( // Use thread_local vars to reduce memory allocations. Shrink strings // to reduce memory footprint from previous requests. thread_local bts::http::HttpRequest http_req; - shrink_string<4096, 128>(http_req.body); + shrink_str<4096, 128>( + http_req.body, bts::utils::cached_type_name()); http_req.reset(); // Prepare request http_req.url = url_; @@ -66,7 +76,9 @@ void RestApiClient::request_( http_req.method = method; http_req.headers["X-MBX-APIKEY"] = api_key_; thread_local std::string qps; - shrink_string<256, 16>(qps); + static const std::string query_params_name = + fmt::format("{}_params", bts::utils::cached_type_name()); + shrink_str<256, 16>(qps, query_params_name.c_str()); qps.clear(); to_query_params(qps, req); if (!qps.empty()) { @@ -78,7 +90,8 @@ void RestApiClient::request_( } // Send it thread_local HttpResponse http_res; - shrink_string<4096, 128>(http_res.body); + shrink_str<4096, 128>( + http_res.body, bts::utils::cached_type_name()); http_res.reset(); HttpClient::send(http_req, &http_res); res.http_code = http_res.code;