forked from Cylix/cpp_redis
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathconsumer.hpp
More file actions
231 lines (200 loc) · 5.81 KB
/
consumer.hpp
File metadata and controls
231 lines (200 loc) · 5.81 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// The MIT License (MIT)
//
// Copyright (c) 11/27/18 nick. <nbatkins@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.#ifndef CPP_REDIS_CONSUMER_HPP
#ifndef CPP_REDIS_CONSUMER_HPP
#define CPP_REDIS_CONSUMER_HPP
#include <cpp_redis/core/client.hpp>
#include <cpp_redis/misc/dispatch_queue.hpp>
#include <string>
namespace cpp_redis {
using defer = shared_ptr<void>;
#define READ_NEW ">"
#define CPP_REDIS_WILD_CARD "*"
//!
//! reply callback called whenever a reply is received
//! takes as parameter the received reply
//!
using consumer_callback_t = dispatch_callback_t;
struct consumer_callback_container {
consumer_callback_t consumer_callback;
acknowledgement_callback_t acknowledgement_callback;
};
using consumer_callback_container_t = consumer_callback_container;
struct consumer_reply {
string_t group_id;
xstream_reply_t reply;
};
using consumer_reply_t = consumer_reply;
//!
//! background processing using redis streams
//!
class consumer {
public:
explicit consumer(
string_t stream, string_t consumer,
size_t max_concurrency = std::thread::hardware_concurrency());
consumer &subscribe(
const string_t &group, const consumer_callback_t &consumer_callback,
const acknowledgement_callback_t &acknowledgement_callback = nullptr);
//!
//! @brief Connect to redis server
//! @param host host to be connected to
//! @param port port to be connected to
//! @param connect_callback connect handler to be called on connect events
//! (may be null)
//! @param timeout_ms maximum time to connect
//! @param max_reconnects maximum attempts of reconnection if connection
//! dropped
//! @param reconnect_interval_ms time between two attempts of reconnection
//!
void connect(const string_t &host = "127.0.0.1", size_t port = 6379,
const connect_callback_t &connect_callback = nullptr,
int timeout_ms = 0, int max_reconnects = 0,
int reconnect_interval_ms = 0);
void auth(const string_t &password,
const reply_callback_t &reply_callback = nullptr);
//!
//! commit pipelined transaction
//! that is, send to the network all commands pipelined by calling send() /
//! subscribe() / ...
//!
//! @return current instance
//!
consumer &commit();
//!
//! check if it is necessary to read
//! entries from the backlog
//!
void check_for_pending() {
if (m_should_read_pending.load()) {
m_should_read_pending.store(false);
m_read_id = READ_NEW;
// Set to block infinitely
m_block_sec = 0;
// Set to read 1
m_read_count = 1;
}
}
//!
//! fires upon a change in the dispatcher.
//! changes occur when a task item is finished
//! or the queue is full.
//!
void dispatch_changed_handler(size_t size);
private:
//!
//! polls the stream for work items
//!
void poll();
void dispatch(const xmessage_t &message,
const pair<string_t, consumer_callback_container_t> &cb);
private:
class client_container {
public:
client_container();
client_t ack_client;
client_t poll_client;
};
public:
//!
//! internal typedef for mapping callbacks
//!
using client_container_t = client_container;
//!
//! internal typedef for mapping callbacks
//!
using client_container_ptr_t = unique_ptr<client_container_t>;
//!
//! internal typedef for mapping callbacks
//!
using consumer_callbacks_t =
multimap<string_t, consumer_callback_container_t>;
private:
//!
//! the redis client
//!
client_container_ptr_t m_client;
//!
//! callback container
//!
consumer_callbacks_t m_callbacks;
//!
//! mutex for the callback container
//!
mutex_t m_callbacks_mutex;
//!
//! dispatch queue:
//! fire and forget background processing
//!
dispatch_queue_ptr_t m_dispatch_queue;
//!
//! whether to add additional work items
//!
atomic_bool dispatch_queue_full{false};
//!
//! signals from the dispatcher
//!
condition_variable_t dispatch_queue_changed;
//!
//! lock for the condi variable
//!
mutex_t dispatch_queue_changed_mutex;
//!
//! the name of the stream
//!
bool is_ready = false;
//!
//! whether or not the client should read
//! from new or stale
//!
atomic_bool m_should_read_pending{true};
private:
//!
//! the name of the stream
//!
string_t m_stream;
//!
//! the name of this consumer group
//!
string_t m_name;
//!
//! the topic ID
//!
string_t m_read_id;
//!
//! number of milliseconds to block when polling
//!
int m_block_sec;
//!
//! maximum number of worker threads
//!
size_t m_max_concurrency;
//!
//! number of messages read
//!
int m_read_count;
};
//!
//! exported typedef
//!
using consumer_t = consumer;
} // namespace cpp_redis
#endif // CPP_REDIS_CONSUMER_HPP