Subscribe handler (#591)

Fixes #593
This commit is contained in:
cyan317
2023-04-13 14:14:11 +01:00
committed by GitHub
parent 36bb20806e
commit 0bc84fefbf
17 changed files with 1365 additions and 46 deletions

View File

@@ -66,7 +66,7 @@ getLedgerPubMessage(
boost::json::object
SubscriptionManager::subLedger(boost::asio::yield_context& yield, std::shared_ptr<WsBase> session)
{
subscribeHelper(session, ledgerSubscribers_, [this](session_ptr session) { unsubLedger(session); });
subscribeHelper(session, ledgerSubscribers_, [this](SessionPtrType session) { unsubLedger(session); });
auto ledgerRange = backend_->fetchLedgerRange();
assert(ledgerRange);
@@ -94,7 +94,7 @@ SubscriptionManager::unsubLedger(std::shared_ptr<WsBase> session)
void
SubscriptionManager::subTransactions(std::shared_ptr<WsBase> session)
{
subscribeHelper(session, txSubscribers_, [this](session_ptr session) { unsubTransactions(session); });
subscribeHelper(session, txSubscribers_, [this](SessionPtrType session) { unsubTransactions(session); });
}
void
@@ -104,15 +104,15 @@ SubscriptionManager::unsubTransactions(std::shared_ptr<WsBase> session)
}
void
SubscriptionManager::subAccount(ripple::AccountID const& account, std::shared_ptr<WsBase>& session)
SubscriptionManager::subAccount(ripple::AccountID const& account, std::shared_ptr<WsBase> const& session)
{
subscribeHelper(session, account, accountSubscribers_, [this, account](session_ptr session) {
subscribeHelper(session, account, accountSubscribers_, [this, account](SessionPtrType session) {
unsubAccount(account, session);
});
}
void
SubscriptionManager::unsubAccount(ripple::AccountID const& account, std::shared_ptr<WsBase>& session)
SubscriptionManager::unsubAccount(ripple::AccountID const& account, std::shared_ptr<WsBase> const& session)
{
accountSubscribers_.unsubscribe(session, account);
}
@@ -120,7 +120,8 @@ SubscriptionManager::unsubAccount(ripple::AccountID const& account, std::shared_
void
SubscriptionManager::subBook(ripple::Book const& book, std::shared_ptr<WsBase> session)
{
subscribeHelper(session, book, bookSubscribers_, [this, book](session_ptr session) { unsubBook(book, session); });
subscribeHelper(
session, book, bookSubscribers_, [this, book](SessionPtrType session) { unsubBook(book, session); });
}
void
@@ -132,7 +133,7 @@ SubscriptionManager::unsubBook(ripple::Book const& book, std::shared_ptr<WsBase>
void
SubscriptionManager::subBookChanges(std::shared_ptr<WsBase> session)
{
subscribeHelper(session, bookChangesSubscribers_, [this](session_ptr session) { unsubBookChanges(session); });
subscribeHelper(session, bookChangesSubscribers_, [this](SessionPtrType session) { unsubBookChanges(session); });
}
void
@@ -281,7 +282,7 @@ SubscriptionManager::forwardValidation(boost::json::object const& response)
void
SubscriptionManager::subProposedAccount(ripple::AccountID const& account, std::shared_ptr<WsBase> session)
{
subscribeHelper(session, account, accountProposedSubscribers_, [this, account](session_ptr session) {
subscribeHelper(session, account, accountProposedSubscribers_, [this, account](SessionPtrType session) {
unsubProposedAccount(account, session);
});
}
@@ -289,7 +290,7 @@ SubscriptionManager::subProposedAccount(ripple::AccountID const& account, std::s
void
SubscriptionManager::subManifest(std::shared_ptr<WsBase> session)
{
subscribeHelper(session, manifestSubscribers_, [this](session_ptr session) { unsubManifest(session); });
subscribeHelper(session, manifestSubscribers_, [this](SessionPtrType session) { unsubManifest(session); });
}
void
@@ -301,7 +302,7 @@ SubscriptionManager::unsubManifest(std::shared_ptr<WsBase> session)
void
SubscriptionManager::subValidation(std::shared_ptr<WsBase> session)
{
subscribeHelper(session, validationsSubscribers_, [this](session_ptr session) { unsubValidation(session); });
subscribeHelper(session, validationsSubscribers_, [this](SessionPtrType session) { unsubValidation(session); });
}
void
@@ -320,7 +321,7 @@ void
SubscriptionManager::subProposedTransactions(std::shared_ptr<WsBase> session)
{
subscribeHelper(
session, txProposedSubscribers_, [this](session_ptr session) { unsubProposedTransactions(session); });
session, txProposedSubscribers_, [this](SessionPtrType session) { unsubProposedTransactions(session); });
}
void
@@ -328,17 +329,19 @@ SubscriptionManager::unsubProposedTransactions(std::shared_ptr<WsBase> session)
{
txProposedSubscribers_.unsubscribe(session);
}
void
SubscriptionManager::subscribeHelper(std::shared_ptr<WsBase>& session, Subscription& subs, CleanupFunction&& func)
SubscriptionManager::subscribeHelper(std::shared_ptr<WsBase> const& session, Subscription& subs, CleanupFunction&& func)
{
subs.subscribe(session);
std::scoped_lock lk(cleanupMtx_);
cleanupFuncs_[session].push_back(std::move(func));
}
template <typename Key>
void
SubscriptionManager::subscribeHelper(
std::shared_ptr<WsBase>& session,
std::shared_ptr<WsBase> const& session,
Key const& k,
SubscriptionMap<Key>& subs,
CleanupFunction&& func)

View File

@@ -189,7 +189,7 @@ SubscriptionMap<Key>::publish(std::shared_ptr<Message> const& message, Key const
class SubscriptionManager
{
using session_ptr = std::shared_ptr<WsBase>;
using SessionPtrType = std::shared_ptr<WsBase>;
clio::Logger log_{"Subscriptions"};
std::vector<std::thread> workers_;
@@ -251,7 +251,7 @@ public:
}
boost::json::object
subLedger(boost::asio::yield_context& yield, session_ptr session);
subLedger(boost::asio::yield_context& yield, SessionPtrType session);
void
pubLedger(
@@ -264,28 +264,28 @@ public:
pubBookChanges(ripple::LedgerInfo const& lgrInfo, std::vector<Backend::TransactionAndMetadata> const& transactions);
void
unsubLedger(session_ptr session);
unsubLedger(SessionPtrType session);
void
subTransactions(session_ptr session);
subTransactions(SessionPtrType session);
void
unsubTransactions(session_ptr session);
unsubTransactions(SessionPtrType session);
void
pubTransaction(Backend::TransactionAndMetadata const& blobs, ripple::LedgerInfo const& lgrInfo);
void
subAccount(ripple::AccountID const& account, session_ptr& session);
subAccount(ripple::AccountID const& account, SessionPtrType const& session);
void
unsubAccount(ripple::AccountID const& account, session_ptr& session);
unsubAccount(ripple::AccountID const& account, SessionPtrType const& session);
void
subBook(ripple::Book const& book, session_ptr session);
subBook(ripple::Book const& book, SessionPtrType session);
void
unsubBook(ripple::Book const& book, session_ptr session);
unsubBook(ripple::Book const& book, SessionPtrType session);
void
subBookChanges(std::shared_ptr<WsBase> session);
@@ -294,16 +294,16 @@ public:
unsubBookChanges(std::shared_ptr<WsBase> session);
void
subManifest(session_ptr session);
subManifest(SessionPtrType session);
void
unsubManifest(session_ptr session);
unsubManifest(SessionPtrType session);
void
subValidation(session_ptr session);
subValidation(SessionPtrType session);
void
unsubValidation(session_ptr session);
unsubValidation(SessionPtrType session);
void
forwardProposedTransaction(boost::json::object const& response);
@@ -315,19 +315,19 @@ public:
forwardValidation(boost::json::object const& response);
void
subProposedAccount(ripple::AccountID const& account, session_ptr session);
subProposedAccount(ripple::AccountID const& account, SessionPtrType session);
void
unsubProposedAccount(ripple::AccountID const& account, session_ptr session);
unsubProposedAccount(ripple::AccountID const& account, SessionPtrType session);
void
subProposedTransactions(session_ptr session);
subProposedTransactions(SessionPtrType session);
void
unsubProposedTransactions(session_ptr session);
unsubProposedTransactions(SessionPtrType session);
void
cleanup(session_ptr session);
cleanup(SessionPtrType session);
boost::json::object
report() const
@@ -349,16 +349,20 @@ public:
private:
void
sendAll(std::string const& pubMsg, std::unordered_set<session_ptr>& subs);
sendAll(std::string const& pubMsg, std::unordered_set<SessionPtrType>& subs);
using CleanupFunction = std::function<void(session_ptr)>;
using CleanupFunction = std::function<void(SessionPtrType const)>;
void
subscribeHelper(std::shared_ptr<WsBase>& session, Subscription& subs, CleanupFunction&& func);
subscribeHelper(std::shared_ptr<WsBase> const& session, Subscription& subs, CleanupFunction&& func);
template <typename Key>
void
subscribeHelper(std::shared_ptr<WsBase>& session, Key const& k, SubscriptionMap<Key>& subs, CleanupFunction&& func);
subscribeHelper(
std::shared_ptr<WsBase> const& session,
Key const& k,
SubscriptionMap<Key>& subs,
CleanupFunction&& func);
/**
* This is how we chose to cleanup subscriptions that have been closed.
@@ -367,5 +371,5 @@ private:
* closed.
*/
std::mutex cleanupMtx_;
std::unordered_map<session_ptr, std::vector<CleanupFunction>> cleanupFuncs_ = {};
std::unordered_map<SessionPtrType, std::vector<CleanupFunction>> cleanupFuncs_ = {};
};