8 #include <redis3m/connection.h>
9 #include <boost/shared_ptr.hpp>
10 #include <boost/noncopyable.hpp>
11 #include <boost/thread/mutex.hpp>
12 #include <redis3m/utils/exception.h>
13 #include <boost/function.hpp>
16 REDIS3M_EXCEPTION(cannot_find_sentinel)
17 REDIS3M_EXCEPTION(cannot_find_master)
18 REDIS3M_EXCEPTION(cannot_find_slave)
19 REDIS3M_EXCEPTION(too_much_retries)
20 REDIS3M_EXCEPTION(wrong_database)
28 typedef boost::shared_ptr<connection_pool> ptr_t;
38 static inline ptr_t
create(
const std::string& sentinel_host,
39 const std::string& master_name,
40 unsigned int sentinel_port=26379)
42 return ptr_t(
new connection_pool(sentinel_host, master_name, sentinel_port));
50 connection::ptr_t
get(connection::role_t type=connection::MASTER);
58 void put(connection::ptr_t conn );
60 template<
typename Ret>
71 connection::role_t conn_type = connection::MASTER,
72 unsigned int retries=5)
78 connection::ptr_t c =
get(conn_type);
82 }
catch (
const transport_failure& ex)
87 throw too_much_retries();
95 inline void set_database(
unsigned int value) { _database = value; }
99 const std::string& master_name,
100 unsigned int sentinel_port);
101 connection::ptr_t create_slave_connection();
102 connection::ptr_t create_master_connection();
103 connection::ptr_t sentinel_connection();
105 boost::mutex access_mutex;
106 std::set<connection::ptr_t> connections;
108 std::vector<std::string> sentinel_hosts;
109 unsigned int sentinel_port;
110 std::string master_name;
111 unsigned int _database;
116 connection::role_t conn_type,
117 unsigned int retries);
void set_database(unsigned int value)
Set a database to use on every new connection object created by the pool.
Definition: connection_pool.h:95
Manages a connection pool, using a Redis Sentinel to get instaces ip, managing also failover...
Definition: connection_pool.h:25
static ptr_t create(const std::string &sentinel_host, const std::string &master_name, unsigned int sentinel_port=26379)
Create a new connection_pool.
Definition: connection_pool.h:38
Ret run_with_connection(boost::function< Ret(connection::ptr_t)> f, connection::role_t conn_type=connection::MASTER, unsigned int retries=5)
Execute a block of code passing a connection::ptr_t if something fails, like broken connection...
Definition: connection_pool.h:70