This commit is contained in:
Kevin Darby committed 2019-09-24 14:37:30 -04:00
1 parent aa8a56787b
commit f66004b465
6 files changed
+150 -58

No files matched your search

+6 -53
View File
@@ -8,6 +8,8 @@
#include "MDClient.h"
#include "ExchangeOrderKey.h"
#include "MBOProcessor.h"
#include "MDClient.h"
#include "HandleIndex.h"
#include "lib/utils/LockBox.h"
@@ -21,55 +23,6 @@ using namespace bts::api::orderman;
namespace bts {
namespace md {
//
// Simple class to handle MD recovery and maintain an index of SecurityKey=>MDConsumerHandle
class HandleIndex {
MDClient& _client;
int _sub;
std::optional<MDConsumer> _consumer;
public:
utils::LockBox<std::unordered_map<SecurityKey,MDConsumerHandle>> index;
HandleIndex(md::MDClient& client):
_client(client),
_sub(0),
_consumer(std::nullopt),
index() {
_sub = _client.on_consumer().subscribe([=](const MDConsumer& c) { on_consumer(c); });
on_consumer(_client.current_consumer());
}
~HandleIndex() { _client.on_consumer().unsubscribe(_sub); }
private:
void on_consumer(const MDConsumer& c) {
MDConsumer temp(c);
if (temp.all_heartbeats_ok()) {
auto existing = _consumer ? _consumer->id() : 0;
LOG(md,Info, "Updateing MBOService MDConsumer. Old {} new {}", existing, temp.id());
// clear the index
index.lock([](auto& index) { index.clear(); });
// Swap to new consumer
if (_consumer) {
_consumer->swap(temp);
}
else { _consumer = temp; }
// Subscribe to on_security
auto id = _consumer->id();
_consumer->on_security([this,id](const SecurityPtr&, MDConsumerHandle handle) {
if (_consumer->id() == id) {
index.lock([handle](auto& index) {
index[SecurityKey(handle.key())] = handle;
});
}
});
}
}
};
class ActiveOrder {
SecurityKey _security;
SparkOrderKey _id;
@@ -111,7 +64,7 @@ struct MBOServiceImpl {
MBOServiceImpl(
api::ServiceContext& ctx,
utils::Dispatcher& d,
md::MDClient& c,
MDClient& c,
MBOServiceBase::PiqPublisher&& pub
):
dispatcher(d),
@@ -124,10 +77,10 @@ struct MBOServiceImpl {
{}
utils::Dispatcher& dispatcher;
md::MDClient& client;
MDClient& client;
OrderService orders;
HandleIndex handle_index;
KeyHandleIndex handle_index;
MBOServiceBase::PiqPublisher publish;
utils::DispatcherTimer timer;
@@ -162,7 +115,7 @@ std::optional<uint32_t> search_orders(const ExchangeOrderKey oid, const std::vec
return std::nullopt;
}
bool update_position(const HandleIndex& handles, ActiveOrder& order) {
bool update_position(const KeyHandleIndex& handles, ActiveOrder& order) {
const auto& index = handles.index;
auto handle = index.lock([id=order.security_id()](const auto& index) {
auto iter = index.find(id);