mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-16 21:04:15 +08:00
Untab everything and improve formatting
This commit is contained in:
parent
1987614bfe
commit
628b0bbaff
@ -42,196 +42,196 @@
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
namespace connection_validator
|
||||
{
|
||||
struct automatic
|
||||
{
|
||||
template<typename Connection>
|
||||
void validate(Connection* connection)
|
||||
{
|
||||
if (!connection->is_valid())
|
||||
{
|
||||
try
|
||||
{
|
||||
connection->reconnect();
|
||||
}
|
||||
catch (const sqlpp::exception&)
|
||||
{
|
||||
throw sqlpp::exception("Failed to reconnect to database.");
|
||||
}
|
||||
}
|
||||
}
|
||||
namespace connection_validator
|
||||
{
|
||||
struct automatic
|
||||
{
|
||||
template<typename Connection>
|
||||
void validate(Connection* connection)
|
||||
{
|
||||
if (!connection->is_valid())
|
||||
{
|
||||
try
|
||||
{
|
||||
connection->reconnect();
|
||||
}
|
||||
catch (const sqlpp::exception&)
|
||||
{
|
||||
throw sqlpp::exception("Failed to reconnect to database.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Connection>
|
||||
void deregister(Connection* connection) {}
|
||||
};
|
||||
template<typename Connection>
|
||||
void deregister(Connection* connection) {}
|
||||
};
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
class periodic
|
||||
{
|
||||
private:
|
||||
std::chrono::seconds revalidate_interval;
|
||||
std::unordered_map<void*,std::chrono::time_point<std::chrono::system_clock>> last_checked;
|
||||
using namespace std::chrono_literals;
|
||||
class periodic
|
||||
{
|
||||
private:
|
||||
std::chrono::seconds revalidate_interval;
|
||||
std::unordered_map<void*, std::chrono::time_point<std::chrono::system_clock>> last_checked;
|
||||
|
||||
public:
|
||||
periodic(const std::chrono::seconds r = 28800s) //default wait_timeout in MySQL
|
||||
: revalidate_interval(r), last_checked() {}
|
||||
public:
|
||||
periodic(const std::chrono::seconds r = 28800s) //default wait_timeout in MySQL
|
||||
: revalidate_interval(r), last_checked() {}
|
||||
|
||||
template<typename Connection>
|
||||
void validate(Connection* connection)
|
||||
{
|
||||
auto last = last_checked.find(connection);
|
||||
auto now = std::chrono::system_clock::now();
|
||||
if (last == last_checked.end())
|
||||
{
|
||||
last_checked.emplace_hint(last, connection, now);
|
||||
}
|
||||
template<typename Connection>
|
||||
void validate(Connection* connection)
|
||||
{
|
||||
auto last = last_checked.find(connection);
|
||||
auto now = std::chrono::system_clock::now();
|
||||
if (last == last_checked.end())
|
||||
{
|
||||
last_checked.emplace_hint(last, connection, now);
|
||||
}
|
||||
|
||||
if (now - last->second < revalidate_interval)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (now - last->second < revalidate_interval)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!connection->is_valid())
|
||||
{
|
||||
try
|
||||
{
|
||||
connection->reconnect();
|
||||
}
|
||||
catch (const sqlpp::exception& e)
|
||||
{
|
||||
throw sqlpp::exception("Failed to reconnect to database.");
|
||||
}
|
||||
}
|
||||
if (!connection->is_valid())
|
||||
{
|
||||
try
|
||||
{
|
||||
connection->reconnect();
|
||||
}
|
||||
catch (const sqlpp::exception& e)
|
||||
{
|
||||
throw sqlpp::exception("Failed to reconnect to database.");
|
||||
}
|
||||
}
|
||||
|
||||
last = now;
|
||||
}
|
||||
last = now;
|
||||
}
|
||||
|
||||
template<typename Connection>
|
||||
void deregister(Connection* con)
|
||||
{
|
||||
auto itr = last_checked.find(con);
|
||||
if(itr != last_checked.end())
|
||||
{
|
||||
last_checked.erase(itr);
|
||||
}
|
||||
}
|
||||
};
|
||||
template<typename Connection>
|
||||
void deregister(Connection* con)
|
||||
{
|
||||
auto itr = last_checked.find(con);
|
||||
if (itr != last_checked.end())
|
||||
{
|
||||
last_checked.erase(itr);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct none
|
||||
{
|
||||
template<typename Connection>
|
||||
void validate(Connection*) {}
|
||||
struct none
|
||||
{
|
||||
template<typename Connection>
|
||||
void validate(Connection*) {}
|
||||
|
||||
template<typename Connection>
|
||||
void deregister(Connection*) {}
|
||||
};
|
||||
}
|
||||
template<typename Connection>
|
||||
void deregister(Connection*) {}
|
||||
};
|
||||
}
|
||||
|
||||
template <typename Connection_config,
|
||||
typename Connection_validator = connection_validator::automatic,
|
||||
typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value, Connection_config::connection>::type>
|
||||
class connection_pool
|
||||
{
|
||||
friend pool_connection<Connection_config, Connection_validator, Connection>;
|
||||
template <typename Connection_config,
|
||||
typename Connection_validator = connection_validator::automatic,
|
||||
typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value, Connection_config::connection>::type>
|
||||
class connection_pool
|
||||
{
|
||||
friend pool_connection<Connection_config, Connection_validator, Connection>;
|
||||
|
||||
private:
|
||||
std::mutex connection_pool_mutex;
|
||||
const std::shared_ptr<Connection_config> config;
|
||||
size_t maximum_pool_size = 0;
|
||||
std::stack<std::unique_ptr<Connection>> free_connections;
|
||||
Connection_validator connection_validator;
|
||||
private:
|
||||
std::mutex connection_pool_mutex;
|
||||
const std::shared_ptr<Connection_config> config;
|
||||
size_t maximum_pool_size = 0;
|
||||
std::stack<std::unique_ptr<Connection>> free_connections;
|
||||
Connection_validator connection_validator;
|
||||
|
||||
void free_connection(std::unique_ptr<Connection>& connection)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(connection_pool_mutex);
|
||||
if (free_connections.size() >= maximum_pool_size)
|
||||
{
|
||||
// Exceeds default size, deregister left over info in the connection_validator and let connection self destroy.
|
||||
connection_validator.deregister(connection.get());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (connection.get())
|
||||
{
|
||||
free_connections.push(std::move(connection));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw sqlpp::exception("Trying to free an empty connection.");
|
||||
}
|
||||
}
|
||||
}
|
||||
void free_connection(std::unique_ptr<Connection>& connection)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(connection_pool_mutex);
|
||||
if (free_connections.size() >= maximum_pool_size)
|
||||
{
|
||||
// Exceeds default size, deregister left over info in the connection_validator and let connection self destroy.
|
||||
connection_validator.deregister(connection.get());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (connection.get())
|
||||
{
|
||||
free_connections.push(std::move(connection));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw sqlpp::exception("Trying to free an empty connection.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
connection_pool(const std::shared_ptr<Connection_config>& config, size_t pool_size)
|
||||
: config(config), maximum_pool_size(pool_size), connection_validator(Connection_validator()) {}
|
||||
~connection_pool() = default;
|
||||
connection_pool(const connection_pool&) = delete;
|
||||
connection_pool(connection_pool&& other)
|
||||
: config(std::move(other.config)), maximum_pool_size(std::move(other.maximum_pool_size)),
|
||||
connection_validator(std::move(other.connection_validator)) {}
|
||||
connection_pool& operator=(const connection_pool&) = delete;
|
||||
connection_pool& operator=(connection_pool&&) = delete;
|
||||
public:
|
||||
connection_pool(const std::shared_ptr<Connection_config>& config, size_t pool_size)
|
||||
: config(config), maximum_pool_size(pool_size), connection_validator(Connection_validator()) {}
|
||||
~connection_pool() = default;
|
||||
connection_pool(const connection_pool&) = delete;
|
||||
connection_pool(connection_pool&& other)
|
||||
: config(std::move(other.config)), maximum_pool_size(std::move(other.maximum_pool_size)),
|
||||
connection_validator(std::move(other.connection_validator)) {}
|
||||
connection_pool& operator=(const connection_pool&) = delete;
|
||||
connection_pool& operator=(connection_pool&&) = delete;
|
||||
|
||||
pool_connection<Connection_config, Connection_validator, Connection> get_connection()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(connection_pool_mutex);
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!free_connections.empty())
|
||||
{
|
||||
auto connection = std::move(free_connections.top());
|
||||
free_connections.pop();
|
||||
connection_validator.validate(connection.get());
|
||||
auto get_connection()
|
||||
-> pool_connection<Connection_config, Connection_validator, Connection>
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(connection_pool_mutex);
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!free_connections.empty())
|
||||
{
|
||||
auto connection = std::move(free_connections.top());
|
||||
free_connections.pop();
|
||||
connection_validator.validate(connection.get());
|
||||
|
||||
return pool_connection<Connection_config, Connection_validator, Connection>(std::move(connection), this);
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (const sqlpp::exception&)
|
||||
{
|
||||
throw sqlpp::exception("Failed to retrieve a valid connection.");
|
||||
}
|
||||
}
|
||||
return pool_connection<Connection_config, Connection_validator, Connection>(std::move(connection), this);
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (const sqlpp::exception&)
|
||||
{
|
||||
throw sqlpp::exception("Failed to retrieve a valid connection.");
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return pool_connection<Connection_config, Connection_validator, Connection>(std::move(std::make_unique<Connection>(config)), this);
|
||||
}
|
||||
catch (const sqlpp::exception&)
|
||||
{
|
||||
throw sqlpp::exception("Failed to spawn a new connection.");
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
return pool_connection<Connection_config, Connection_validator, Connection>(std::move(std::make_unique<Connection>(config)), this);
|
||||
}
|
||||
catch (const sqlpp::exception&)
|
||||
{
|
||||
throw sqlpp::exception("Failed to spawn a new connection.");
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Query, typename Lambda>
|
||||
void operator()(Query query, Lambda callback)
|
||||
{
|
||||
query_task<connection_pool, Query, Lambda>(*this, query, callback)();
|
||||
}
|
||||
template<typename Query, typename Lambda>
|
||||
void operator()(Query query, Lambda callback)
|
||||
{
|
||||
query_task<connection_pool, Query, Lambda>(*this, query, callback)();
|
||||
}
|
||||
|
||||
template<typename Query>
|
||||
void operator()(Query query)
|
||||
{
|
||||
operator()(query, [](){});
|
||||
}
|
||||
};
|
||||
template<typename Query>
|
||||
void operator()(Query query)
|
||||
{
|
||||
operator()(query, []() {});
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Connection_config,
|
||||
typename Connection_validator = connection_validator::automatic,
|
||||
typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value,Connection_config::connection>::type>
|
||||
connection_pool<Connection_config, Connection_validator, Connection> make_connection_pool(
|
||||
const std::shared_ptr<Connection_config>& config,
|
||||
size_t max_pool_size)
|
||||
{
|
||||
return connection_pool<Connection_config, Connection_validator, Connection>(config, max_pool_size);
|
||||
}
|
||||
template<typename Connection_config,
|
||||
typename Connection_validator = connection_validator::automatic,
|
||||
typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value, Connection_config::connection>::type>
|
||||
auto make_connection_pool(const std::shared_ptr<Connection_config>& config, size_t max_pool_size)
|
||||
-> connection_pool<Connection_config, Connection_validator, Connection>
|
||||
{
|
||||
return connection_pool<Connection_config, Connection_validator, Connection>(config, max_pool_size);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -32,63 +32,67 @@
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
template <typename Connection_config, typename Connection_validator, typename Connection,
|
||||
typename Connection_pool = connection_pool<Connection_config, Connection_validator, Connection>>
|
||||
struct pool_connection : public sqlpp::connection
|
||||
{
|
||||
private:
|
||||
std::unique_ptr<Connection> _impl;
|
||||
Connection_pool* origin;
|
||||
template <typename Connection_config, typename Connection_validator, typename Connection,
|
||||
typename Connection_pool = connection_pool<Connection_config, Connection_validator, Connection>>
|
||||
struct pool_connection : public sqlpp::connection
|
||||
{
|
||||
private:
|
||||
std::unique_ptr<Connection> _impl;
|
||||
Connection_pool* origin;
|
||||
|
||||
public:
|
||||
pool_connection() : _impl(nullptr), origin(nullptr) {}
|
||||
public:
|
||||
pool_connection() : _impl(nullptr), origin(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
pool_connection(std::unique_ptr<Connection>& connection, Connection_pool* origin)
|
||||
: _impl(std::move(connection)), origin(origin) {}
|
||||
pool_connection(std::unique_ptr<Connection>& connection, Connection_pool* origin) : _impl(std::move(connection)), origin(origin)
|
||||
{
|
||||
}
|
||||
|
||||
~pool_connection()
|
||||
{
|
||||
if (_impl.get())
|
||||
{
|
||||
origin->free_connection(_impl);
|
||||
}
|
||||
}
|
||||
~pool_connection()
|
||||
{
|
||||
if (_impl.get())
|
||||
{
|
||||
origin->free_connection(_impl);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
auto operator()(Args&&... args) -> decltype(_impl->args(std::forward<Args>(args)...))
|
||||
{
|
||||
return _impl->args(std::forward<Args>(args)...);
|
||||
}
|
||||
template<typename... Args>
|
||||
auto operator()(Args&&... args) -> decltype(_impl->args(std::forward<Args>(args)...))
|
||||
{
|
||||
return _impl->args(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
auto operator()(const T& t) -> decltype(_impl->run(t))
|
||||
{
|
||||
return _impl->run(t);
|
||||
}
|
||||
template <typename T>
|
||||
auto operator()(const T& t) -> decltype(_impl->run(t))
|
||||
{
|
||||
return _impl->run(t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
auto execute(const T& t) -> decltype(_impl->execute(t))
|
||||
{
|
||||
return _impl->execute(t);
|
||||
}
|
||||
template <typename T>
|
||||
auto execute(const T& t) -> decltype(_impl->execute(t))
|
||||
{
|
||||
return _impl->execute(t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
auto prepare(const T& t) -> decltype(_impl->prepare(t))
|
||||
{
|
||||
return _impl->prepare(t);
|
||||
}
|
||||
template <typename T>
|
||||
auto prepare(const T& t) -> decltype(_impl->prepare(t))
|
||||
{
|
||||
return _impl->prepare(t);
|
||||
}
|
||||
|
||||
pool_connection(const pool_connection&) = delete;
|
||||
pool_connection(pool_connection&& other)
|
||||
: _impl(std::move(other._impl)), origin(other.origin) {}
|
||||
pool_connection& operator=(const pool_connection&) = delete;
|
||||
pool_connection& operator=(pool_connection&& other)
|
||||
{
|
||||
_impl = std::move(other._impl);
|
||||
origin = other.origin;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
pool_connection(const pool_connection&) = delete;
|
||||
pool_connection(pool_connection&& other) : _impl(std::move(other._impl)), origin(other.origin)
|
||||
{
|
||||
}
|
||||
pool_connection& operator=(const pool_connection&) = delete;
|
||||
pool_connection& operator=(pool_connection&& other)
|
||||
{
|
||||
_impl = std::move(other._impl);
|
||||
origin = other.origin;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user