IDGenerator is a thread-safe singleton now

This commit is contained in:
Taha Afzal committed 2022-10-19 15:29:40 -04:00
1 parent 32d1714cf6
commit bf6a232b5b
4 files changed
+27 -3

No files matched your search

+2 -1
View File
@@ -61,7 +61,8 @@ struct CCMarketDataListener::PImpl {
const bool use_cc_id_as_bts_id = true;
const std::string cc_name_prefix = "cc_security";
size_t n_bk_updates = 0;
bts::persistence::IDGenerator id_gen = {};
bts::persistence::IDGenerator &id_gen =
bts::persistence::IDGenerator::insance();
CCSecurityIndex &index;
MDProducer &md_producer;
// map of logical key (venue + exchange key) to id
+12
View File
@@ -8,6 +8,9 @@
namespace bts::persistence {
IDGenerator *IDGenerator::_instance = nullptr;
std::mutex IDGenerator::_instance_mx;
struct IDGenerator::PImpl {
std::unique_ptr<persistence::IStore> kv_store;
PImpl() : kv_store(std::make_unique<persistence::SqliteStore>("key_ids")) {}
@@ -15,6 +18,14 @@ struct IDGenerator::PImpl {
IDGenerator::IDGenerator() : me(new PImpl()) {}
IDGenerator &IDGenerator::insance() {
std::lock_guard<std::mutex> myLock(_instance_mx);
if (!_instance) {
_instance = new IDGenerator();
}
return *_instance;
}
inline uint64_t str_to_id(const std::string id_str) {
return std::stoull(id_str);
}
@@ -35,6 +46,7 @@ uint64_t get_next_id(
std::optional<uint64_t> IDGenerator::get_id(
const std::string &ns, const std::string &key, bool add_if_new) {
std::lock_guard<std::mutex> myLock(_instance_mx);
auto [exists, val] = me->kv_store->retrieve_one(ns, key);
if (exists) {
uint64_t rv = str_to_id(val);
+11 -1
View File
@@ -3,20 +3,30 @@
#include <OnixS/CME/MDH/Integral.h>
#include <cstdint>
#include <memory>
#include <mutex>
#include <optional>
namespace bts::persistence {
class IDGenerator {
public:
private:
IDGenerator();
~IDGenerator();
IDGenerator(const IDGenerator &) = delete;
IDGenerator &operator=(const IDGenerator &) = delete;
public:
std::optional<uint64_t> get_id(
const std::string &ns, const std::string &key, bool add_if_new);
public:
static IDGenerator &insance();
private:
struct PImpl;
std::unique_ptr<PImpl> me;
static IDGenerator *_instance;
static std::mutex _instance_mx;
};
} // namespace bts::persistence
@@ -341,7 +341,8 @@ struct EmMDListener::PImpl {
uint32_t n_updates = 0;
// to generate exchange_id's (the rest of the system demands it, whereas the
// exchange does not give us a numeric id in this case)
bts::persistence::IDGenerator id_gen = {};
bts::persistence::IDGenerator &id_gen =
bts::persistence::IDGenerator::insance();
emumba::deribit_connector::deribit_handler *handler = nullptr;
std::vector<EMTradingSession> trading_sessions;
// shared memory