diag: Log when http buffers are resized in binance exe gws

This commit is contained in:
John Colvin committed 2025-03-10 03:05:59 +00:00
1 parent b9fe39f6e3
commit 4effdc4160
3 files changed
+54 -15

No files matched your search

@@ -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 <std::size_t MAX_SIZE, std::size_t DEFAULT_SIZE>
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 <class T, std::size_t MAX_SIZE, std::size_t DEFAULT_SIZE>
static void shrink_vec(std::vector<T> &vec) {
static void shrink_vec(std::vector<T> &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<RequestT>());
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<RequestT>());
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<ResponseT>());
http_res.reset();
HttpClient::send(http_req, &http_res);
res.http_code = http_res.code;
@@ -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 <std::size_t MAX_SIZE, std::size_t DEFAULT_SIZE>
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 <class T, std::size_t MAX_SIZE, std::size_t DEFAULT_SIZE>
static void shrink_vec(std::vector<T> &vec) {
static void shrink_vec(std::vector<T> &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<RequestT>());
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<RequestT>());
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<ResponseT>());
http_res.reset();
HttpClient::send(http_req, &http_res);
res.http_code = http_res.code;
@@ -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 <std::size_t MAX_SIZE, std::size_t DEFAULT_SIZE>
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 <class T, std::size_t MAX_SIZE, std::size_t DEFAULT_SIZE>
static void shrink_vec(std::vector<T> &vec) {
static void shrink_vec(std::vector<T> &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<RequestT>());
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<RequestT>());
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<ResponseT>());
http_res.reset();
HttpClient::send(http_req, &http_res);
res.http_code = http_res.code;