fix: dedicate a separate WS connection for private channels (fills + balance)

Book connections no longer subscribe to tradeOrdersV2 or /account/balance.
A dedicated private WS connection is created as the last connection
(index = connection_count - 1) with zero book subscriptions, only private
channels. Decouples fill delivery from book connection health.
This commit is contained in:
nicolas 2026-06-02 23:42:18 -03:00
parent b0056f4b6b
commit ccf6ff8670
2 changed files with 9 additions and 8 deletions

View File

@ -156,17 +156,18 @@ int main(int argc, char *argv[]) {
uint32_t conns_needed = (n + 399) / 400;
if (conns_needed < 1) conns_needed = 1;
if (conns_needed > WS_MAX_CONNECTIONS) conns_needed = WS_MAX_CONNECTIONS;
ws_client.connection_count = conns_needed;
if (conns_needed >= WS_MAX_CONNECTIONS) conns_needed = WS_MAX_CONNECTIONS - 1;
ws_client.connection_count = conns_needed + 1;
log_write("[MAIN] Connecting %u WS connections...\n", conns_needed);
for (uint32_t i = 0; i < conns_needed; i++) {
log_write("[MAIN] Connecting %u WS connections (%u book + 1 private)...\n",
ws_client.connection_count, conns_needed);
for (uint32_t i = 0; i < ws_client.connection_count; i++) {
if (ws_client_connect(&ws_client, i) != 0) {
log_write("[MAIN] WS connection %u failed\n", i);
}
}
// Batch-subscribe up to 400 symbols per WS connection
// Batch-subscribe up to 400 symbols per WS connection (book connections only)
uint32_t batch_start = 0;
for (uint32_t conn_idx = 0; conn_idx < conns_needed; conn_idx++) {
uint32_t batch_end = batch_start + 400;

View File

@ -403,9 +403,9 @@ int ws_client_connect(ws_client_t *client, uint32_t conn_idx) {
ws_client_subscribe(client, conn_idx, conn->symbol_indices, conn->symbol_count);
}
/* Subscribe to private channels — only on connection 0 to avoid
duplicate delivery of order/balance events across connections */
if (conn_idx == 0) {
/* Subscribe to private channels — only on the dedicated private connection
(the last one, which has no book subscriptions). */
if (conn_idx == client->connection_count - 1) {
char msg[256];
snprintf(msg, sizeof(msg),
"{\"type\":\"subscribe\",\"topic\":\"/spotMarket/tradeOrdersV2\","