irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

connection_pool.hpp
Go to the documentation of this file.
1 #ifndef IRODS_CONNECTION_POOL_HPP
2 #define IRODS_CONNECTION_POOL_HPP
3 
4 #include "rcConnect.h"
5 
6 #include <memory>
7 #include <vector>
8 #include <mutex>
9 #include <atomic>
10 #include <string>
11 #include <functional>
12 
13 namespace irods
14 {
16  {
17  public:
18  // A wrapper around a connection in the pool.
19  // On destruction, the underlying connection is immediately returned
20  // to the pool.
22  {
23  public:
24  friend class connection_pool;
25 
28 
30 
31  operator bool() const noexcept;
32 
33  operator rcComm_t&() const;
34  explicit operator rcComm_t*() const noexcept;
35 
36  rcComm_t* release();
37 
38  private:
39  connection_proxy(connection_pool& _pool, rcComm_t& _conn, int _index) noexcept;
40 
41  static constexpr int uninitialized_index = -1;
42 
45  int index_;
46  };
47 
48  connection_pool(int _size,
49  const std::string& _host,
50  const int _port,
51  const std::string& _username,
52  const std::string& _zone,
53  const int _refresh_time);
54 
55  connection_pool(const connection_pool&) = delete;
56  connection_pool& operator=(const connection_pool&) = delete;
57 
59 
60  private:
61  using connection_pointer = std::unique_ptr<rcComm_t, int(*)(rcComm_t*)>;
62 
64  {
65  std::mutex mutex{};
66  std::atomic<bool> in_use{};
67  bool refresh{};
70  std::time_t creation_time{};
71  };
72 
73  void create_connection(int _index,
74  std::function<void()> _on_connect_error,
75  std::function<void()> _on_login_error);
76 
77  rcComm_t* refresh_connection(int _index);
78 
79  bool verify_connection(int _index);
80 
81  void return_connection(int _index);
82 
83  void release_connection(int _index);
84 
85  const std::string host_;
86  const int port_;
87  const std::string username_;
88  const std::string zone_;
89  const int refresh_time_;
90  std::vector<connection_context> conn_ctxs_;
91  };
92 
93  std::shared_ptr<connection_pool> make_connection_pool(int size = 1);
94 } // namespace irods
95 
96 #endif // IRODS_CONNECTION_POOL_HPP
97 
irods::connection_pool::connection_pointer
std::unique_ptr< rcComm_t, int(*)(rcComm_t *)> connection_pointer
Definition: connection_pool.hpp:61
rcComm_t
Definition: rcConnect.h:95
rErrMsg_t
Definition: rodsError.h:19
irods::connection_pool::refresh_time_
const int refresh_time_
Definition: connection_pool.hpp:89
irods::connection_pool::refresh_connection
rcComm_t * refresh_connection(int _index)
Definition: connection_pool.cpp:180
irods::connection_pool::conn_ctxs_
std::vector< connection_context > conn_ctxs_
Definition: connection_pool.hpp:90
rcConnect.h
irods::connection_pool::host_
const std::string host_
Definition: connection_pool.hpp:85
irods::connection_pool::connection_context
Definition: connection_pool.hpp:64
irods::connection_pool::connection_proxy::conn_
rcComm_t * conn_
Definition: connection_pool.hpp:44
irods::connection_pool::connection_proxy::uninitialized_index
static constexpr int uninitialized_index
Definition: connection_pool.hpp:41
irods::connection_pool::username_
const std::string username_
Definition: connection_pool.hpp:87
irods::connection_pool::connection_proxy::index_
int index_
Definition: connection_pool.hpp:45
irods::connection_pool::get_connection
connection_proxy get_connection()
Definition: connection_pool.cpp:199
irods::connection_pool::connection_proxy::connection_proxy
connection_proxy(connection_proxy &&)
Definition: connection_pool.cpp:18
irods
Definition: apiHandler.hpp:35
irods::connection_pool::zone_
const std::string zone_
Definition: connection_pool.hpp:88
irods::connection_pool::connection_proxy::release
rcComm_t * release()
Definition: connection_pool.cpp:60
irods::connection_pool::return_connection
void return_connection(int _index)
Definition: connection_pool.cpp:213
irods::connection_pool::create_connection
void create_connection(int _index, std::function< void()> _on_connect_error, std::function< void()> _on_login_error)
Definition: connection_pool.cpp:136
irods::connection_pool::release_connection
void release_connection(int _index)
Definition: connection_pool.cpp:218
irods::connection_pool::connection_proxy::operator=
connection_proxy & operator=(connection_proxy &&)
Definition: connection_pool.cpp:28
irods::error
Definition: irods_error.hpp:23
irods::connection_pool::connection_proxy::pool_
connection_pool * pool_
Definition: connection_pool.hpp:43
irods::make_connection_pool
std::shared_ptr< connection_pool > make_connection_pool(int size=1)
Definition: connection_pool.cpp:223
irods::connection_pool::verify_connection
bool verify_connection(int _index)
Definition: connection_pool.cpp:159
irods::connection_pool::connection_proxy
Definition: connection_pool.hpp:22
rcDisconnect
int rcDisconnect(rcComm_t *conn)
Definition: rcConnect.cpp:246
size
long long size
Definition: filesystem.cpp:102
irods::connection_pool::connection_proxy::~connection_proxy
~connection_proxy()
Definition: connection_pool.cpp:11
irods::connection_pool::port_
const int port_
Definition: connection_pool.hpp:86
irods::connection_pool
Definition: connection_pool.hpp:16