add an async subscriber impl with thread pool
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "core/subscribable.hpp"
|
||||
#include "core/thread_pool.hpp"
|
||||
|
||||
using ProductCount = int;
|
||||
using ProductId = std::string;
|
||||
|
||||
// inventory manager is not generic ( works with the specific types ProductId
|
||||
// and ProductCount ) but the subscribable is generic
|
||||
class InventoryManager : public Subscribable<ProductId, ProductCount> {
|
||||
private:
|
||||
std::unordered_map<ProductId, int> inventory_;
|
||||
std::shared_ptr<ThreadPool> thread_pool_;
|
||||
|
||||
public:
|
||||
explicit InventoryManager(std::shared_ptr<ThreadPool> thread_pool);
|
||||
|
||||
void setInventory(const ProductId& productId, ProductCount quantity);
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "app/inventory_manager.hpp"
|
||||
#include "core/subscriber.hpp"
|
||||
|
||||
class SmsService : public Subscriber<ProductId, ProductCount> {
|
||||
public:
|
||||
void onNotification(const ProductId& productId,
|
||||
const ProductCount& quantity) override;
|
||||
};
|
||||
|
||||
class EmailService : public Subscriber<ProductId, ProductCount> {
|
||||
public:
|
||||
void onNotification(const ProductId& productId,
|
||||
const ProductCount& quantity) override;
|
||||
};
|
||||
|
||||
class LogService : public Subscriber<ProductId, ProductCount> {
|
||||
public:
|
||||
void onNotification(const ProductId& productId,
|
||||
const ProductCount& quantity) override;
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "subscriber.hpp"
|
||||
#include "thread_pool.hpp"
|
||||
|
||||
template <typename TEventId, typename TEventInfo>
|
||||
class Subscribable {
|
||||
using TSubscriber = Subscriber<TEventId, TEventInfo>;
|
||||
|
||||
private:
|
||||
std::unordered_map<TEventId, std::vector<std::shared_ptr<TSubscriber>>>
|
||||
subscribersMap_;
|
||||
|
||||
public:
|
||||
void subscribe(const TEventId& eventId,
|
||||
std::shared_ptr<TSubscriber> subscriber) {
|
||||
subscribersMap_[eventId].push_back(subscriber);
|
||||
}
|
||||
|
||||
void unsubscribe(const TEventId& eventId,
|
||||
std::shared_ptr<TSubscriber> subscriber) {
|
||||
auto entry = subscribersMap_.find(eventId);
|
||||
if (entry == subscribersMap_.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::erase(entry->second, subscriber);
|
||||
if (entry->second.empty()) {
|
||||
subscribersMap_.erase(entry);
|
||||
}
|
||||
}
|
||||
|
||||
void notify(const TEventId& eventId, const TEventInfo& eventInfo,
|
||||
ThreadPool& threadPool) const {
|
||||
auto entry = subscribersMap_.find(eventId);
|
||||
if (entry == subscribersMap_.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// edge case: if a subscriber unsubscribes itself during notification
|
||||
// this modifies the vector and makes my iterators invalid
|
||||
auto subscribersCopy = entry->second;
|
||||
for (auto& subscriber : subscribersCopy) {
|
||||
// Enqueue each notification as a separate task in the thread pool
|
||||
// capture by value next to copy the shared_ptr
|
||||
threadPool.enqueue([subscriber, eventId, eventInfo]() {
|
||||
subscriber->onNotification(eventId, eventInfo);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
template <typename TEventId, typename TEventInfo>
|
||||
class Subscriber {
|
||||
public:
|
||||
virtual void onNotification(const TEventId& eventId,
|
||||
const TEventInfo& eventInfo) = 0;
|
||||
|
||||
virtual ~Subscriber() = default;
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
class ThreadPool {
|
||||
public:
|
||||
explicit ThreadPool(
|
||||
size_t num_threads = std::thread::hardware_concurrency());
|
||||
|
||||
~ThreadPool() noexcept;
|
||||
|
||||
void enqueue(std::function<void()> task);
|
||||
|
||||
// Get the number of worker threads
|
||||
[[nodiscard]] size_t thread_count() const noexcept {
|
||||
return workers_.size();
|
||||
}
|
||||
|
||||
// Wait for all queued tasks to complete
|
||||
void wait_for_completion() noexcept;
|
||||
|
||||
// Disable copy and move
|
||||
// sync primitives are not movable or copyable
|
||||
ThreadPool(const ThreadPool&) = delete;
|
||||
ThreadPool& operator=(const ThreadPool&) = delete;
|
||||
ThreadPool(ThreadPool&&) = delete;
|
||||
ThreadPool& operator=(ThreadPool&&) = delete;
|
||||
|
||||
private:
|
||||
// Worker thread function
|
||||
void worker();
|
||||
|
||||
// Thread management
|
||||
std::vector<std::thread> workers_;
|
||||
|
||||
// Task queue
|
||||
std::queue<std::function<void()>> tasks_;
|
||||
|
||||
// Synchronization primitives
|
||||
std::mutex queue_mutex_;
|
||||
std::condition_variable condition_;
|
||||
|
||||
// Shutdown flag
|
||||
std::atomic<bool> stop_;
|
||||
|
||||
// Track active tasks for wait_for_completion
|
||||
std::atomic<size_t> active_tasks_;
|
||||
std::condition_variable completion_condition_;
|
||||
};
|
||||
Reference in New Issue
Block a user