-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPublisherService.hpp
144 lines (124 loc) · 4.87 KB
/
PublisherService.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef GAZELLEMQ_SERVER_PUBLISHERSERVICE_HPP
#define GAZELLEMQ_SERVER_PUBLISHERSERVICE_HPP
#include <vector>
#include <cstring>
#include <csignal>
#include <linux/time_types.h>
#include <liburing/io_uring.h>
#include <liburing.h>
#include "../lib/MPMCQueue/MPMCQueue.hpp"
#include "MessagePublisher.hpp"
#include "Message.hpp"
#include "MessageQueue.hpp"
namespace gazellemq::server {
class PublisherService {
private:
std::vector<MessagePublisher> publishers;
MessageQueue& messageQueue;
std::atomic_flag hasNewPublishers{false};
std::atomic_flag isRunning{true};
std::jthread bgThread;
public:
explicit PublisherService():messageQueue(getMessageQueue()) {}
private:
/**
* Error handler
* @param msg
*/
static void printError(char const *msg, int err) {
printf("%s\n%s\n", msg, strerror(-err));
}
/**
* Pushes the message to the queue. Messages are sent to subscribers in a background thread.
* @param messageType
* @param batch
*/
void pushToQueue(MessageBatch&& batch) {
messageQueue.push_back(std::move(batch));
}
/**
* Removes inactive subscribers/publishers, and runs handleEvent(...) on new publishers/subscribers
*/
void onBeforeHandleMessages(io_uring* ring) {
std::for_each(publishers.begin(), publishers.end(), [ring](MessagePublisher& o) {
if (o.getMustDisconnect()) {
printf("Disconnecting publisher [%s]\n", o.clientName.c_str());
o.disconnect(ring);
}
});
publishers.erase(
std::remove_if(publishers.begin(), publishers.end(), [](MessagePublisher const& o) {
if (o.getIsZombie()) {
printf("Remove zombie publisher [%s]\n", o.clientName.c_str());
}
return o.getIsZombie();
}), publishers.end());
if (hasNewPublishers.test()) {
hasNewPublishers.clear();
std::for_each(publishers.begin(), publishers.end(), [ring](MessagePublisher &o) {
return o.handleEvent(ring, 0);
});
}
}
public:
/**
* Registers a publisher so it can sendMessage messages
* @param name
* @param fd
*/
void newPublisher(std::string &&name, int fd) {
MessagePublisher publisher{fd, std::move(name), [this](MessageBatch&& batch) {
this->pushToQueue(std::move(batch));
}};
// other publishers with the same name will be removed
for (MessagePublisher& o: publishers) {
if (o.clientName == publisher.clientName) {
o.forceDisconnect();
}
}
publisher.printHello();
publishers.push_back(std::move(publisher));
hasNewPublishers.test_and_set();
}
void go() {
using namespace std::chrono_literals;
bgThread = std::jthread{[this]() {
printf("PublisherService running in background thread.\n");
constexpr static size_t NB_EVENTS = 32;
io_uring ring{};
io_uring_queue_init(NB_EVENTS, &ring, 0);
std::vector<io_uring_cqe*> cqes{};
cqes.reserve(NB_EVENTS);
cqes.insert(cqes.begin(), NB_EVENTS, nullptr);
__kernel_timespec ts{.tv_sec = 2, .tv_nsec = 0};
outer:
while (isRunning.test()) {
int ret = io_uring_wait_cqe_timeout(&ring, cqes.data(), &ts);
if (ret == -SIGILL || ret == TIMEOUT) {
onBeforeHandleMessages(&ring);
continue;
}
if (ret < 0) {
printError("io_uring_wait_cqe_timeout(...)", ret);
goto outer;
}
std::for_each(cqes.begin(), cqes.end(), [&ring](io_uring_cqe* cqe) {
if (cqe != nullptr) {
int res = cqe->res;
if (res != -EAGAIN) {
auto *pSubscriber = static_cast<MessagePublisher *>(io_uring_cqe_get_data(cqe));
pSubscriber->handleEvent(&ring, res);
}
io_uring_cqe_seen(&ring, cqe);
}
});
}
}};
}
};
inline PublisherService _mpq{};
static PublisherService& getPublisherService() {
return gazellemq::server::_mpq;
}
}
#endif //GAZELLEMQ_SERVER_PUBLISHERSERVICE_HPP