lintel 0.2.0
Loading...
Searching...
No Matches
ConnectionPool.h
Go to the documentation of this file.
1#ifndef LINTEL_CONNECTIONPOOL_H
2#define LINTEL_CONNECTIONPOOL_H
3
4#include <chrono>
5#include <condition_variable>
6#include <cstddef>
7#include <functional>
8#include <map>
9#include <memory>
10#include <mutex>
11#include <source_location>
12#include <string>
13#include <thread>
14#include <vector>
15
18
19namespace lintel {
20namespace db {
32 {
34 std::size_t maxConnections = 0;
36 std::chrono::milliseconds acquireTimeout{ 5000 };
37 };
38
65 {
66 public:
68 using Connector = std::function<std::unique_ptr<Connection>()>;
70 using HealthCheck = std::function<bool(const Connection &)>;
71
81 class Lease
82 {
83 public:
85 Lease() = default;
86 Lease(const Lease &) = delete;
87 Lease &operator=(const Lease &) = delete;
89 Lease(Lease &&other) noexcept;
92 Lease &operator=(Lease &&other) noexcept;
95
99
101 [[nodiscard]] bool valid() const { return m_connection != nullptr; }
102
103 private:
104 friend class ConnectionPool;
105
112 Lease(ConnectionPool *pool, std::unique_ptr<Connection> connection, std::thread::id owner);
113
115 void reset();
116
117 ConnectionPool *m_pool = nullptr;
118 std::unique_ptr<Connection> m_connection;
119 std::thread::id m_owner;
120 };
121
131 explicit ConnectionPool(std::shared_ptr<DatabaseConnectionEntry> connectionEntry,
134 Connector connector = {});
135
138
146 Lease acquire(std::source_location location = std::source_location::current());
147
149 [[nodiscard]] const std::string &name() const;
150
152 [[nodiscard]] std::size_t maxConnections() const { return m_settings.maxConnections; }
153
155 [[nodiscard]] std::size_t outstanding() const;
156
158 [[nodiscard]] std::size_t available() const;
159
160 private:
161 friend class Lease;
162
168 void release(std::unique_ptr<Connection> connection, std::thread::id owner);
169
172 [[nodiscard]] bool isHealthy(const Connection &connection) const;
173
176 [[nodiscard]] std::string describeOutstanding(std::thread::id waiting) const;
177
180 [[nodiscard]] static std::string describeLocation(const std::source_location &location);
181
183 struct OutstandingLease
184 {
186 std::thread::id m_owner;
188 std::source_location m_location;
190 std::chrono::steady_clock::time_point m_acquired;
191 };
192
193 std::shared_ptr<DatabaseConnectionEntry> m_connectionEntry;
194 PoolSettings m_settings;
195 HealthCheck m_healthCheck;
196 Connector m_connector;
197 mutable std::mutex m_mutex;
198 std::condition_variable m_condition;
199 std::vector<std::unique_ptr<Connection>> m_idle;
200 std::map<std::thread::id, OutstandingLease> m_outstanding;
201 std::size_t m_created = 0;
202 };
203}// namespace db
204
205}// namespace lintel
206#endif// LINTEL_CONNECTIONPOOL_H
Definition ConnectionPool.h:82
Lease(Lease &&other) noexcept
bool valid() const
Definition ConnectionPool.h:101
Lease(const Lease &)=delete
Connection & connection() const
Lease & operator=(Lease &&other) noexcept
Lease & operator=(const Lease &)=delete
Definition ConnectionPool.h:65
const std::string & name() const
ConnectionPool(const ConnectionPool &)=delete
std::size_t maxConnections() const
Definition ConnectionPool.h:152
ConnectionPool(std::shared_ptr< DatabaseConnectionEntry > connectionEntry, PoolSettings settings, HealthCheck healthCheck={}, Connector connector={})
ConnectionPool & operator=(const ConnectionPool &)=delete
std::function< std::unique_ptr< Connection >()> Connector
Definition ConnectionPool.h:68
std::function< bool(const Connection &)> HealthCheck
Definition ConnectionPool.h:70
std::size_t available() const
std::size_t outstanding() const
Lease acquire(std::source_location location=std::source_location::current())
Definition Connection.h:25
Definition BaseFeature.h:6
constexpr std::string_view type_name()
Definition TypeName.h:9
Definition ConnectionPool.h:32
std::size_t maxConnections
Definition ConnectionPool.h:34
std::chrono::milliseconds acquireTimeout
Definition ConnectionPool.h:36