forked from mtusi2026/mtusi2026lab6
62 lines
1.4 KiB
C++
62 lines
1.4 KiB
C++
#pragma once
|
|
#include <condition_variable>
|
|
|
|
#include <pqxx/pqxx>
|
|
|
|
#include <cstddef>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
|
|
std::string read_secret_file(const char* env_name);
|
|
|
|
std::unique_ptr<pqxx::connection> open_pg_connection(const std::string& conninfo,
|
|
bool statement_timeout);
|
|
|
|
|
|
void apply_migrations(pqxx::connection& conn, const std::string& dir);
|
|
|
|
|
|
class PgPool {
|
|
public:
|
|
bool ping();
|
|
PgPool(std::string conninfo, std::size_t n);
|
|
~PgPool();
|
|
|
|
PgPool(const PgPool&) = delete;
|
|
PgPool& operator=(const PgPool&) = delete;
|
|
|
|
class Lease {
|
|
public:
|
|
explicit Lease(PgPool& pool);
|
|
~Lease();
|
|
Lease(Lease&& other) noexcept;
|
|
Lease& operator=(Lease&&) = delete;
|
|
Lease(const Lease&) = delete;
|
|
Lease& operator=(const Lease&) = delete;
|
|
|
|
pqxx::connection& operator*() const { return *conn_; }
|
|
pqxx::connection* operator->() const { return conn_; }
|
|
|
|
private:
|
|
PgPool* pool_;
|
|
pqxx::connection* conn_;
|
|
};
|
|
|
|
Lease acquire();
|
|
|
|
private:
|
|
friend class Lease;
|
|
|
|
pqxx::connection* take();
|
|
void give_back(pqxx::connection* conn);
|
|
|
|
std::string conninfo_;
|
|
std::vector<std::unique_ptr<pqxx::connection>> all_;
|
|
std::vector<pqxx::connection*> idle_;
|
|
std::mutex mu_;
|
|
std::condition_variable cv_;
|
|
bool shutting_down_ = false;
|
|
}; |