![]() |
cpp_redis
4.0.0
cpp_redis is a C++11 Asynchronous Multi-Platform Lightweight Redis Client, with support for synchronous operations and pipelining.
|
#include <sentinel.hpp>
Classes | |
class | sentinel_def |
Public Types | |
typedef std::function< void(reply &)> | reply_callback_t |
typedef std::function< void(sentinel &)> | sentinel_disconnect_handler_t |
Public Member Functions | |
sentinel (void) | |
ctor & dtor More... | |
sentinel (const std::shared_ptr< network::tcp_client_iface > &tcp_client) | |
~sentinel (void) | |
dtor More... | |
sentinel (const sentinel &)=delete | |
copy ctor More... | |
sentinel & | operator= (const sentinel &)=delete |
assignment operator More... | |
sentinel & | send (const std::vector< std::string > &sentinel_cmd, const reply_callback_t &callback=nullptr) |
sentinel & | commit (void) |
sentinel & | sync_commit (void) |
template<class Rep , class Period > | |
sentinel & | sync_commit (const std::chrono::duration< Rep, Period > &timeout) |
sentinel & | add_sentinel (const std::string &host, std::size_t port) |
void | clear_sentinels (void) |
void | disconnect (bool wait_for_removal=false) |
bool | is_connected (void) |
void | connect_sentinel (std::uint32_t timeout_msecs=0, const sentinel_disconnect_handler_t &disconnect_handler=nullptr) |
void | connect (const std::string &host, std::size_t port, const sentinel_disconnect_handler_t &disconnect_handler=nullptr, std::uint32_t timeout_msecs=0) |
bool | get_master_addr_by_name (const std::string &name, std::string &host, std::size_t &port, bool autoconnect=true) |
sentinel & | ckquorum (const std::string &name, const reply_callback_t &reply_callback=nullptr) |
sentinel & | failover (const std::string &name, const reply_callback_t &reply_callback=nullptr) |
sentinel & | flushconfig (const reply_callback_t &reply_callback=nullptr) |
sentinel & | master (const std::string &name, const reply_callback_t &reply_callback=nullptr) |
sentinel & | masters (const reply_callback_t &reply_callback=nullptr) |
sentinel & | monitor (const std::string &name, const std::string &ip, std::size_t port, std::size_t quorum, const reply_callback_t &reply_callback=nullptr) |
sentinel & | ping (const reply_callback_t &reply_callback=nullptr) |
sentinel & | remove (const std::string &name, const reply_callback_t &reply_callback=nullptr) |
sentinel & | reset (const std::string &pattern, const reply_callback_t &reply_callback=nullptr) |
sentinel & | sentinels (const std::string &name, const reply_callback_t &reply_callback=nullptr) |
sentinel & | set (const std::string &name, const std::string &option, const std::string &value, const reply_callback_t &reply_callback=nullptr) |
sentinel & | slaves (const std::string &name, const reply_callback_t &reply_callback=nullptr) |
Private Member Functions | |
void | connection_receive_handler (network::redis_connection &connection, reply &reply) |
void | connection_disconnect_handler (network::redis_connection &connection) |
void | call_disconnect_handler (void) |
void | clear_callbacks (void) |
void | try_commit (void) |
Private Attributes | |
std::vector< sentinel_def > | m_sentinels |
network::redis_connection | m_client |
std::queue< reply_callback_t > | m_callbacks |
sentinel_disconnect_handler_t | m_disconnect_handler |
std::mutex | m_callbacks_mutex |
std::condition_variable | m_sync_condvar |
std::atomic< unsigned int > | m_callbacks_running = ATOMIC_VAR_INIT(0) |
typedef std::function<void(reply&)> cpp_redis::sentinel::reply_callback_t |
callback to be called whenever a reply has been received
typedef std::function<void(sentinel&)> cpp_redis::sentinel::sentinel_disconnect_handler_t |
handlers called whenever disconnection occurred function takes the sentinel current instance as parameter
cpp_redis::sentinel::sentinel | ( | void | ) |
ctor & dtor
default ctor
|
explicit |
custom ctor to specify custom tcp_client
tcp_client | tcp client to be used for network communications |
cpp_redis::sentinel::~sentinel | ( | void | ) |
dtor
|
delete |
copy ctor
sentinel& cpp_redis::sentinel::add_sentinel | ( | const std::string & | host, |
std::size_t | port | ||
) |
add a sentinel definition. Required for connect() or get_master_addr_by_name() when autoconnect is enabled.
host | sentinel host |
port | sentinel port |
|
private |
Call the user-defined disconnection handler
sentinel& cpp_redis::sentinel::ckquorum | ( | const std::string & | name, |
const reply_callback_t & | reply_callback = nullptr |
||
) |
|
private |
reset the queue of pending callbacks
void cpp_redis::sentinel::clear_sentinels | ( | void | ) |
clear all existing sentinels.
sentinel& cpp_redis::sentinel::commit | ( | void | ) |
commit pipelined transaction that is, send to the network all commands pipelined by calling send()
void cpp_redis::sentinel::connect | ( | const std::string & | host, |
std::size_t | port, | ||
const sentinel_disconnect_handler_t & | disconnect_handler = nullptr , |
||
std::uint32_t | timeout_msecs = 0 |
||
) |
Connect to named sentinel
host | host to be connected to |
port | port to be connected to |
timeout_msecs | maximum time to connect |
disconnect_handler | handler to be called whenever disconnection occurs |
void cpp_redis::sentinel::connect_sentinel | ( | std::uint32_t | timeout_msecs = 0 , |
const sentinel_disconnect_handler_t & | disconnect_handler = nullptr |
||
) |
Connect to 1st active sentinel we find. Requires add_sentinel() to be called first
timeout_msecs | maximum time to connect |
disconnect_handler | handler to be called whenever disconnection occurs |
|
private |
redis_connection disconnection handler, triggered whenever a disconnection occured
connection | redis_connection instance |
|
private |
redis connection receive handler, triggered whenever a reply has been read by the redis connection
connection | redis_connection instance |
reply | parsed reply |
void cpp_redis::sentinel::disconnect | ( | bool | wait_for_removal = false | ) |
disconnect from redis server
wait_for_removal | when sets to true, disconnect blocks until the underlying TCP client has been effectively removed from the io_service and that all the underlying callbacks have completed. |
sentinel& cpp_redis::sentinel::failover | ( | const std::string & | name, |
const reply_callback_t & | reply_callback = nullptr |
||
) |
sentinel& cpp_redis::sentinel::flushconfig | ( | const reply_callback_t & | reply_callback = nullptr | ) |
bool cpp_redis::sentinel::get_master_addr_by_name | ( | const std::string & | name, |
std::string & | host, | ||
std::size_t & | port, | ||
bool | autoconnect = true |
||
) |
Used to find the current redis master by asking one or more sentinels. Use high availablity. Handles connect() and disconnect() automatically when autoconnect=true This method is synchronous. No need to call sync_commit() or process a reply callback. Call add_sentinel() before using when autoconnect==true
name | sentinel name |
host | sentinel host |
port | sentinel port |
autoconnect | autoconnect we loop through and connect/disconnect as necessary to sentinels that were added using add_sentinel(). Otherwise we rely on the call to connect to a sentinel before calling this method. |
bool cpp_redis::sentinel::is_connected | ( | void | ) |
sentinel& cpp_redis::sentinel::master | ( | const std::string & | name, |
const reply_callback_t & | reply_callback = nullptr |
||
) |
sentinel& cpp_redis::sentinel::masters | ( | const reply_callback_t & | reply_callback = nullptr | ) |
sentinel& cpp_redis::sentinel::monitor | ( | const std::string & | name, |
const std::string & | ip, | ||
std::size_t | port, | ||
std::size_t | quorum, | ||
const reply_callback_t & | reply_callback = nullptr |
||
) |
sentinel& cpp_redis::sentinel::ping | ( | const reply_callback_t & | reply_callback = nullptr | ) |
sentinel& cpp_redis::sentinel::remove | ( | const std::string & | name, |
const reply_callback_t & | reply_callback = nullptr |
||
) |
sentinel& cpp_redis::sentinel::reset | ( | const std::string & | pattern, |
const reply_callback_t & | reply_callback = nullptr |
||
) |
sentinel& cpp_redis::sentinel::send | ( | const std::vector< std::string > & | sentinel_cmd, |
const reply_callback_t & | callback = nullptr |
||
) |
send the given command the command is actually pipelined and only buffered, so nothing is sent to the network please call commit() to flush the buffer
sentinel_cmd | command to be sent |
callback | callback to be called when reply is received for this command |
sentinel& cpp_redis::sentinel::sentinels | ( | const std::string & | name, |
const reply_callback_t & | reply_callback = nullptr |
||
) |
sentinel& cpp_redis::sentinel::set | ( | const std::string & | name, |
const std::string & | option, | ||
const std::string & | value, | ||
const reply_callback_t & | reply_callback = nullptr |
||
) |
sentinel& cpp_redis::sentinel::slaves | ( | const std::string & | name, |
const reply_callback_t & | reply_callback = nullptr |
||
) |
sentinel& cpp_redis::sentinel::sync_commit | ( | void | ) |
same as commit(), but synchronous will block until all pending commands have been sent and that a reply has been received for each of them and all underlying callbacks completed
|
inline |
same as sync_commit, but with a timeout will simply block until it completes or timeout expires
|
private |
try to commit the pending pipelined if client is disconnected, will throw an exception and clear all pending callbacks (call clear_callbacks())
|
private |
queue of callback to process
|
private |
callbacks thread safety
|
private |
number of callbacks currently being running
|
private |
tcp client for redis sentinel connection
|
private |
user defined disconnection handler to be called on disconnection
|
private |
A pool of 1 or more sentinels we ask to determine which redis server is the master.
|
private |
condvar for callbacks updates