redis3m  1.0.0
 All Classes Functions Variables Enumerations Pages
connection_pool.h
1 // Copyright (c) 2014 Luca Marturana. All rights reserved.
2 // Licensed under Apache 2.0, see LICENSE for details
3 
4 #pragma once
5 
6 #include <string>
7 #include <set>
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>
14 
15 namespace redis3m {
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)
25  class connection_pool: boost::noncopyable
26  {
27  public:
28  typedef boost::shared_ptr<connection_pool> ptr_t;
29 
38  static inline ptr_t create(const std::string& sentinel_host,
39  const std::string& master_name,
40  unsigned int sentinel_port=26379)
41  {
42  return ptr_t(new connection_pool(sentinel_host, master_name, sentinel_port));
43  }
44 
50  connection::ptr_t get(connection::role_t type=connection::MASTER);
51 
58  void put(connection::ptr_t conn );
59 
60  template<typename Ret>
70  Ret run_with_connection(boost::function<Ret(connection::ptr_t)> f,
71  connection::role_t conn_type = connection::MASTER,
72  unsigned int retries=5)
73  {
74  while (retries > 0)
75  {
76  try
77  {
78  connection::ptr_t c = get(conn_type);
79  Ret r = f(c);
80  put(c);
81  return r;
82  } catch (const transport_failure& ex)
83  {
84  --retries;
85  }
86  }
87  throw too_much_retries();
88  }
89 
95  inline void set_database(unsigned int value) { _database = value; }
96 
97  private:
98  connection_pool(const std::string& sentinel_host,
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();
104 
105  boost::mutex access_mutex;
106  std::set<connection::ptr_t> connections;
107 
108  std::vector<std::string> sentinel_hosts;
109  unsigned int sentinel_port;
110  std::string master_name;
111  unsigned int _database;
112  };
113 
114  template<>
115  void connection_pool::run_with_connection(boost::function<void(connection::ptr_t)> f,
116  connection::role_t conn_type,
117  unsigned int retries);
118 }
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