cpp_redis  4.0.0
cpp_redis is a C++11 Asynchronous Multi-Platform Lightweight Redis Client, with support for synchronous operations and pipelining.
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
cpp_redis::sentinel Class Reference

#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...
 
sentineloperator= (const sentinel &)=delete
 assignment operator More...
 
sentinelsend (const std::vector< std::string > &sentinel_cmd, const reply_callback_t &callback=nullptr)
 
sentinelcommit (void)
 
sentinelsync_commit (void)
 
template<class Rep , class Period >
sentinelsync_commit (const std::chrono::duration< Rep, Period > &timeout)
 
sentineladd_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)
 
sentinelckquorum (const std::string &name, const reply_callback_t &reply_callback=nullptr)
 
sentinelfailover (const std::string &name, const reply_callback_t &reply_callback=nullptr)
 
sentinelflushconfig (const reply_callback_t &reply_callback=nullptr)
 
sentinelmaster (const std::string &name, const reply_callback_t &reply_callback=nullptr)
 
sentinelmasters (const reply_callback_t &reply_callback=nullptr)
 
sentinelmonitor (const std::string &name, const std::string &ip, std::size_t port, std::size_t quorum, const reply_callback_t &reply_callback=nullptr)
 
sentinelping (const reply_callback_t &reply_callback=nullptr)
 
sentinelremove (const std::string &name, const reply_callback_t &reply_callback=nullptr)
 
sentinelreset (const std::string &pattern, const reply_callback_t &reply_callback=nullptr)
 
sentinelsentinels (const std::string &name, const reply_callback_t &reply_callback=nullptr)
 
sentinelset (const std::string &name, const std::string &option, const std::string &value, const reply_callback_t &reply_callback=nullptr)
 
sentinelslaves (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_defm_sentinels
 
network::redis_connection m_client
 
std::queue< reply_callback_tm_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)
 

Member Typedef Documentation

◆ reply_callback_t

typedef std::function<void(reply&)> cpp_redis::sentinel::reply_callback_t

callback to be called whenever a reply has been received

◆ sentinel_disconnect_handler_t

handlers called whenever disconnection occurred function takes the sentinel current instance as parameter

Constructor & Destructor Documentation

◆ sentinel() [1/3]

cpp_redis::sentinel::sentinel ( void  )

ctor & dtor

default ctor

◆ sentinel() [2/3]

cpp_redis::sentinel::sentinel ( const std::shared_ptr< network::tcp_client_iface > &  tcp_client)
explicit

custom ctor to specify custom tcp_client

Parameters
tcp_clienttcp client to be used for network communications

◆ ~sentinel()

cpp_redis::sentinel::~sentinel ( void  )

dtor

◆ sentinel() [3/3]

cpp_redis::sentinel::sentinel ( const sentinel )
delete

copy ctor

Member Function Documentation

◆ add_sentinel()

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.

Parameters
hostsentinel host
portsentinel port
Returns
current instance

◆ call_disconnect_handler()

void cpp_redis::sentinel::call_disconnect_handler ( void  )
private

Call the user-defined disconnection handler

◆ ckquorum()

sentinel& cpp_redis::sentinel::ckquorum ( const std::string &  name,
const reply_callback_t reply_callback = nullptr 
)

◆ clear_callbacks()

void cpp_redis::sentinel::clear_callbacks ( void  )
private

reset the queue of pending callbacks

◆ clear_sentinels()

void cpp_redis::sentinel::clear_sentinels ( void  )

clear all existing sentinels.

◆ commit()

sentinel& cpp_redis::sentinel::commit ( void  )

commit pipelined transaction that is, send to the network all commands pipelined by calling send()

Returns
current instance

◆ connect()

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

Parameters
hosthost to be connected to
portport to be connected to
timeout_msecsmaximum time to connect
disconnect_handlerhandler to be called whenever disconnection occurs

◆ connect_sentinel()

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

Parameters
timeout_msecsmaximum time to connect
disconnect_handlerhandler to be called whenever disconnection occurs

◆ connection_disconnect_handler()

void cpp_redis::sentinel::connection_disconnect_handler ( network::redis_connection connection)
private

redis_connection disconnection handler, triggered whenever a disconnection occured

Parameters
connectionredis_connection instance

◆ connection_receive_handler()

void cpp_redis::sentinel::connection_receive_handler ( network::redis_connection connection,
reply reply 
)
private

redis connection receive handler, triggered whenever a reply has been read by the redis connection

Parameters
connectionredis_connection instance
replyparsed reply

◆ disconnect()

void cpp_redis::sentinel::disconnect ( bool  wait_for_removal = false)

disconnect from redis server

Parameters
wait_for_removalwhen 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.

◆ failover()

sentinel& cpp_redis::sentinel::failover ( const std::string &  name,
const reply_callback_t reply_callback = nullptr 
)

◆ flushconfig()

sentinel& cpp_redis::sentinel::flushconfig ( const reply_callback_t reply_callback = nullptr)

◆ get_master_addr_by_name()

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

Parameters
namesentinel name
hostsentinel host
portsentinel port
autoconnectautoconnect 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.
Returns
true if a master was found and fills in host and port output parameters, false otherwise

◆ is_connected()

bool cpp_redis::sentinel::is_connected ( void  )
Returns
whether we are connected to the redis server or not

◆ master()

sentinel& cpp_redis::sentinel::master ( const std::string &  name,
const reply_callback_t reply_callback = nullptr 
)

◆ masters()

sentinel& cpp_redis::sentinel::masters ( const reply_callback_t reply_callback = nullptr)

◆ monitor()

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 
)

◆ operator=()

sentinel& cpp_redis::sentinel::operator= ( const sentinel )
delete

assignment operator

◆ ping()

sentinel& cpp_redis::sentinel::ping ( const reply_callback_t reply_callback = nullptr)

◆ remove()

sentinel& cpp_redis::sentinel::remove ( const std::string &  name,
const reply_callback_t reply_callback = nullptr 
)

◆ reset()

sentinel& cpp_redis::sentinel::reset ( const std::string &  pattern,
const reply_callback_t reply_callback = nullptr 
)

◆ send()

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

Parameters
sentinel_cmdcommand to be sent
callbackcallback to be called when reply is received for this command
Returns
current instance

◆ sentinels()

sentinel& cpp_redis::sentinel::sentinels ( const std::string &  name,
const reply_callback_t reply_callback = nullptr 
)

◆ set()

sentinel& cpp_redis::sentinel::set ( const std::string &  name,
const std::string &  option,
const std::string &  value,
const reply_callback_t reply_callback = nullptr 
)

◆ slaves()

sentinel& cpp_redis::sentinel::slaves ( const std::string &  name,
const reply_callback_t reply_callback = nullptr 
)

◆ sync_commit() [1/2]

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

Returns
current instance

◆ sync_commit() [2/2]

template<class Rep , class Period >
sentinel& cpp_redis::sentinel::sync_commit ( const std::chrono::duration< Rep, Period > &  timeout)
inline

same as sync_commit, but with a timeout will simply block until it completes or timeout expires

Returns
current instance

◆ try_commit()

void cpp_redis::sentinel::try_commit ( void  )
private

try to commit the pending pipelined if client is disconnected, will throw an exception and clear all pending callbacks (call clear_callbacks())

Member Data Documentation

◆ m_callbacks

std::queue<reply_callback_t> cpp_redis::sentinel::m_callbacks
private

queue of callback to process

◆ m_callbacks_mutex

std::mutex cpp_redis::sentinel::m_callbacks_mutex
private

callbacks thread safety

◆ m_callbacks_running

std::atomic<unsigned int> cpp_redis::sentinel::m_callbacks_running = ATOMIC_VAR_INIT(0)
private

number of callbacks currently being running

◆ m_client

network::redis_connection cpp_redis::sentinel::m_client
private

tcp client for redis sentinel connection

◆ m_disconnect_handler

sentinel_disconnect_handler_t cpp_redis::sentinel::m_disconnect_handler
private

user defined disconnection handler to be called on disconnection

◆ m_sentinels

std::vector<sentinel_def> cpp_redis::sentinel::m_sentinels
private

A pool of 1 or more sentinels we ask to determine which redis server is the master.

◆ m_sync_condvar

std::condition_variable cpp_redis::sentinel::m_sync_condvar
private

condvar for callbacks updates


The documentation for this class was generated from the following file: