Skip to content

Commit

Permalink
Merge pull request #357 from crypto-chassis/small_fix
Browse files Browse the repository at this point in the history
refactor: improve error handling for building initial order book
  • Loading branch information
cryptochassis authored Dec 29, 2022
2 parents 0cf8137 + 0269d39 commit a403597
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
3 changes: 3 additions & 0 deletions include/ccapi_cpp/ccapi_macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -1143,4 +1143,7 @@
#ifndef CCAPI_GATEIO_API_CHANNEL_ID
#define CCAPI_GATEIO_API_CHANNEL_ID "cryptochassis"
#endif
#ifndef CCAPI_MEXC_API_SOURCE
#define CCAPI_MEXC_API_SOURCE "CHAS"
#endif
#endif // INCLUDE_CCAPI_CPP_CCAPI_MACRO_H_
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class ExecutionManagementServiceMexc : public ExecutionManagementService {
ExecutionManagementService::convertRequestForRestGenericPrivateRequest(req, request, now, symbolId, credential);
} break;
case Request::Operation::CREATE_ORDER: {
req.set("source", CCAPI_MEXC_API_SOURCE);
req.method(http::verb::post);
std::string queryString;
const std::map<std::string, std::string> param = request.getFirstParamWithDefault();
Expand Down
63 changes: 34 additions & 29 deletions include/ccapi_cpp/service/ccapi_market_data_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,22 @@ class MarketDataService : public Service {
marketDataMessage.data;
}
}
void buildOrderBookInitialOnFail(const WsConnection& wsConnection, const std::string& exchangeSubscriptionId, long delayMilliSeconds) {
auto thisDelayMilliSeconds = delayMilliSeconds * 2;
if (thisDelayMilliSeconds > 0) {
this->fetchMarketDepthInitialSnapshotTimerByConnectionIdExchangeSubscriptionIdMap[wsConnection.id][exchangeSubscriptionId] =
this->serviceContextPtr->tlsClientPtr->set_timer(thisDelayMilliSeconds,
[wsConnection, exchangeSubscriptionId, thisDelayMilliSeconds, that = this](ErrorCode const& ec) {
if (ec) {
that->onError(Event::Type::SUBSCRIPTION_STATUS, Message::Type::GENERIC_ERROR, ec, "timer");
} else {
that->buildOrderBookInitial(wsConnection, exchangeSubscriptionId, thisDelayMilliSeconds);
}
});
} else {
this->buildOrderBookInitial(wsConnection, exchangeSubscriptionId, thisDelayMilliSeconds);
}
}
void buildOrderBookInitial(const WsConnection& wsConnection, const std::string& exchangeSubscriptionId, long delayMilliSeconds) {
auto now = UtilTime::now();
http::request<http::string_body> req;
Expand All @@ -1227,20 +1243,7 @@ class MarketDataService : public Service {
this->sendRequest(
req,
[wsConnection, exchangeSubscriptionId, delayMilliSeconds, that = shared_from_base<MarketDataService>()](const beast::error_code& ec) {
auto thisDelayMilliSeconds = delayMilliSeconds * 2;
if (thisDelayMilliSeconds > 0) {
that->fetchMarketDepthInitialSnapshotTimerByConnectionIdExchangeSubscriptionIdMap[wsConnection.id][exchangeSubscriptionId] =
that->serviceContextPtr->tlsClientPtr->set_timer(thisDelayMilliSeconds,
[wsConnection, exchangeSubscriptionId, thisDelayMilliSeconds, that](ErrorCode const& ec) {
if (ec) {
that->onError(Event::Type::SUBSCRIPTION_STATUS, Message::Type::GENERIC_ERROR, ec, "timer");
} else {
that->buildOrderBookInitial(wsConnection, exchangeSubscriptionId, thisDelayMilliSeconds);
}
});
} else {
that->buildOrderBookInitial(wsConnection, exchangeSubscriptionId, thisDelayMilliSeconds);
}
that->buildOrderBookInitialOnFail(wsConnection, exchangeSubscriptionId, delayMilliSeconds);
},
[wsConnection, exchangeSubscriptionId, delayMilliSeconds, that = shared_from_base<MarketDataService>()](const http::response<http::string_body>& res) {
auto timeReceived = UtilTime::now();
Expand Down Expand Up @@ -1365,27 +1368,29 @@ class MarketDataService : public Service {
that->eventHandler(event, nullptr);
that->processedInitialSnapshotByConnectionIdChannelIdSymbolIdMap[wsConnection.id][channelId][symbolId] = true;
} else {
if (delayMilliSeconds > 0) {
that->fetchMarketDepthInitialSnapshotTimerByConnectionIdExchangeSubscriptionIdMap[wsConnection.id][exchangeSubscriptionId] =
that->serviceContextPtr->tlsClientPtr->set_timer(
delayMilliSeconds, [wsConnection, exchangeSubscriptionId, delayMilliSeconds, that](ErrorCode const& ec) {
if (ec) {
that->onError(Event::Type::SUBSCRIPTION_STATUS, Message::Type::GENERIC_ERROR, ec, "timer");
} else {
that->buildOrderBookInitial(wsConnection, exchangeSubscriptionId, delayMilliSeconds);
}
});
} else {
that->buildOrderBookInitial(wsConnection, exchangeSubscriptionId, delayMilliSeconds);
}
that->buildOrderBookInitialOnFail(wsConnection, exchangeSubscriptionId, delayMilliSeconds);
// if (delayMilliSeconds > 0) {
// that->fetchMarketDepthInitialSnapshotTimerByConnectionIdExchangeSubscriptionIdMap[wsConnection.id][exchangeSubscriptionId] =
// that->serviceContextPtr->tlsClientPtr->set_timer(
// delayMilliSeconds, [wsConnection, exchangeSubscriptionId, delayMilliSeconds, that](ErrorCode const& ec) {
// if (ec) {
// that->onError(Event::Type::SUBSCRIPTION_STATUS, Message::Type::GENERIC_ERROR, ec, "timer");
// } else {
// that->buildOrderBookInitial(wsConnection, exchangeSubscriptionId, delayMilliSeconds);
// }
// });
// } else {
// that->buildOrderBookInitial(wsConnection, exchangeSubscriptionId, delayMilliSeconds);
// }
}
return;
} catch (const std::runtime_error& e) {
CCAPI_LOGGER_ERROR(std::string("e.what() = ") + e.what());
}
}
WsConnection thisWsConnection = wsConnection;
that->onFail_(thisWsConnection);
that->buildOrderBookInitialOnFail(wsConnection, exchangeSubscriptionId, delayMilliSeconds);
// WsConnection thisWsConnection = wsConnection;
// that->onFail_(thisWsConnection);
},
this->sessionOptions.httpRequestTimeoutMilliSeconds);
}
Expand Down

0 comments on commit a403597

Please sign in to comment.