cpp_redis  4.0.0
cpp_redis is a C++11 Asynchronous Multi-Platform Lightweight Redis Client, with support for synchronous operations and pipelining.
subscriber.hpp
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com>
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #pragma once
24 
25 #include <atomic>
26 #include <functional>
27 #include <map>
28 #include <mutex>
29 #include <string>
30 
31 #include <cpp_redis/core/sentinel.hpp>
32 #include <cpp_redis/network/redis_connection.hpp>
33 #include <cpp_redis/network/tcp_client_iface.hpp>
34 
35 namespace cpp_redis {
36 
43  class subscriber {
44  public:
45 #ifndef __CPP_REDIS_USE_CUSTOM_TCP_CLIENT
46 
48  subscriber();
49 
50 #endif /* __CPP_REDIS_USE_CUSTOM_TCP_CLIENT */
51 
57  explicit subscriber(const std::shared_ptr<network::tcp_client_iface> &tcp_client);
58 
60  ~subscriber();
61 
63  subscriber(const subscriber &) = delete;
64 
66  subscriber &operator=(const subscriber &) = delete;
67 
68  public:
79  enum class connect_state {
80  dropped,
81  start,
82  sleeping,
83  ok,
84  failed,
85  lookup_failed,
86  stopped
87  };
88 
89  public:
91  typedef std::function<void(const std::string &host, std::size_t port, connect_state status)> connect_callback_t;
92 
100  void connect(
101  const std::string &host = "127.0.0.1",
102  std::size_t port = 6379,
103  const connect_callback_t &connect_callback = nullptr,
104  std::uint32_t timeout_ms = 0,
105  std::int32_t max_reconnects = 0,
106  std::uint32_t reconnect_interval_ms = 0);
107 
114  void connect(
115  const std::string &m_name,
116  const connect_callback_t &connect_callback = nullptr,
117  std::uint32_t timeout_ms = 0,
118  std::int32_t max_reconnects = 0,
119  std::uint32_t reconnect_interval_ms = 0);
120 
123  bool is_connected() const;
124 
127  void disconnect(bool wait_for_removal = false);
128 
131  bool is_reconnecting() const;
132 
134  void cancel_reconnect();
135 
136  public:
138  typedef std::function<void(reply &)> reply_callback_t;
139 
146  subscriber &auth(const std::string &password, const reply_callback_t &reply_callback = nullptr);
147 
152  typedef std::function<void(const std::string &, const std::string &)> read_callback_t;
153 
158  typedef std::function<void(int64_t)> acknowledgement_callback_t;
159 
171  subscriber &subscribe(const std::string &channel, const read_callback_t &callback,
172  const acknowledgement_callback_t &acknowledgement_callback = nullptr);
173 
185  subscriber &psubscribe(const std::string &pattern, const read_callback_t &callback,
186  const acknowledgement_callback_t &acknowledgement_callback = nullptr);
187 
195  subscriber &unsubscribe(const std::string &channel);
196 
204  subscriber &punsubscribe(const std::string &pattern);
205 
212  subscriber &commit();
213 
214  public:
222  void add_sentinel(const std::string &host, std::size_t port, std::uint32_t timeout_ms = 0);
223 
229  const sentinel &get_sentinel() const;
230 
238 
242  void clear_sentinels();
243 
244  private:
248  struct callback_holder {
249  read_callback_t read_callback;
250  acknowledgement_callback_t acknowledgement_callback;
251  };
252 
253  private:
260  void connection_receive_handler(network::redis_connection &connection, reply &reply);
261 
267  void connection_disconnection_handler(network::redis_connection &connection);
268 
275  void handle_acknowledgement_reply(const std::vector<reply> &reply);
276 
283  void read_reply_handler(const std::vector<reply> &reply);
284 
291  void handle_psubscribe_reply(const std::vector<reply> &reply);
292 
301  void
302  call_acknowledgement_callback(const std::string &channel, const std::map<std::string, callback_holder> &channels,
303  std::mutex &channels_mtx, int64_t nb_chans);
304 
305  private:
310  void reconnect();
311 
315  void re_auth();
316 
320  void re_subscribe();
321 
325  bool should_reconnect() const;
326 
330  void sleep_before_next_reconnect_attempt();
331 
335  void clear_subscriptions();
336 
337  private:
346  void unprotected_read(const std::string &channel, const read_callback_t &callback,
347  const acknowledgement_callback_t &acknowledgement_callback);
348 
357  void unprotected_psubscribe(const std::string &pattern, const read_callback_t &callback,
358  const acknowledgement_callback_t &acknowledgement_callback);
359 
360  private:
364  std::string m_redis_server;
368  std::size_t m_redis_port = 0;
372  std::string m_master_name;
376  std::string m_password;
377 
381  network::redis_connection m_client;
382 
386  cpp_redis::sentinel m_sentinel;
387 
391  std::uint32_t m_connect_timeout_ms = 0;
395  std::int32_t m_max_reconnects = 0;
399  std::int32_t m_current_reconnect_attempts = 0;
403  std::uint32_t m_reconnect_interval_ms = 0;
404 
408  std::atomic_bool m_reconnecting;
412  std::atomic_bool m_cancel;
413 
417  std::map<std::string, callback_holder> m_subscribed_sessions;
421  std::map<std::string, callback_holder> m_psubscribed_channels;
422 
426  connect_callback_t m_connect_callback;
427 
431  std::mutex m_psubscribed_channels_mutex;
435  std::mutex m_subscribed_sessions_mutex;
436 
440  reply_callback_t m_auth_reply_callback;
441  };
442 
443 } // namespace cpp_redis
Definition: redis_connection.hpp:45
Definition: subscriber.hpp:43
const sentinel & get_sentinel() const
std::function< void(const std::string &host, std::size_t port, connect_state status)> connect_callback_t
connect handler, called whenever a new connection even occurred
Definition: subscriber.hpp:91
subscriber & psubscribe(const std::string &pattern, const read_callback_t &callback, const acknowledgement_callback_t &acknowledgement_callback=nullptr)
void cancel_reconnect()
stop any reconnect in progress
Definition: reply.hpp:37
subscriber & operator=(const subscriber &)=delete
assignment operator
subscriber & commit()
bool is_connected() const
determines client connectivity
bool is_reconnecting() const
determines if reconnect is in progress
std::function< void(int64_t)> acknowledgement_callback_t
Definition: subscriber.hpp:158
connect_state
Definition: subscriber.hpp:79
std::function< void(const std::string &, const std::string &)> read_callback_t
Definition: subscriber.hpp:152
subscriber & auth(const std::string &password, const reply_callback_t &reply_callback=nullptr)
ability to authenticate on the redis server if necessary this method should not be called repeatedly ...
Definition: sentinel.hpp:40
void add_sentinel(const std::string &host, std::size_t port, std::uint32_t timeout_ms=0)
void disconnect(bool wait_for_removal=false)
disconnect from redis server
subscriber & punsubscribe(const std::string &pattern)
subscriber & unsubscribe(const std::string &channel)
void connect(const std::string &host="127.0.0.1", std::size_t port=6379, const connect_callback_t &connect_callback=nullptr, std::uint32_t timeout_ms=0, std::int32_t max_reconnects=0, std::uint32_t reconnect_interval_ms=0)
Connect to redis server.
std::function< void(reply &)> reply_callback_t
reply callback called whenever a reply is received, takes as parameter the received reply ...
Definition: subscriber.hpp:138
subscriber & subscribe(const std::string &channel, const read_callback_t &callback, const acknowledgement_callback_t &acknowledgement_callback=nullptr)
Definition: array_builder.hpp:29