Foundation / checks (ecs) (push) Successful in 2m29s
Foundation / checks (deploy) (push) Successful in 4m8s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 18m9s
Foundation / checks (client) (push) Canceled after 18m8s
Foundation / checks (network) (push) Canceled after 18m7s
Foundation / checks (tsan) (push) Canceled after 15m39s
Foundation / checks (postgres) (push) Canceled after 18m13s
176 lines
6.3 KiB
C++
176 lines
6.3 KiB
C++
#include "postgres_relay.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <arpa/inet.h>
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <cerrno>
|
|
#include <fcntl.h>
|
|
#include <poll.h>
|
|
#include <stdexcept>
|
|
#include <sys/socket.h>
|
|
#include <thread>
|
|
#include <unistd.h>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace evo::test {
|
|
namespace {
|
|
struct Socket {
|
|
int fd{-1};
|
|
Socket() = default;
|
|
explicit Socket(int value) : fd(value) {}
|
|
~Socket() {
|
|
if (fd >= 0)
|
|
::close(fd);
|
|
}
|
|
Socket(const Socket&) = delete;
|
|
Socket& operator=(const Socket&) = delete;
|
|
Socket(Socket&& other) noexcept : fd(std::exchange(other.fd, -1)) {}
|
|
Socket& operator=(Socket&& other) noexcept {
|
|
if (fd >= 0)
|
|
::close(fd);
|
|
fd = std::exchange(other.fd, -1);
|
|
return *this;
|
|
}
|
|
};
|
|
sockaddr_in loopback(std::uint16_t port) {
|
|
sockaddr_in address{};
|
|
address.sin_family = AF_INET;
|
|
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
address.sin_port = htons(port);
|
|
return address;
|
|
}
|
|
bool nonblocking(int fd) {
|
|
const int flags = ::fcntl(fd, F_GETFL);
|
|
return flags >= 0 && ::fcntl(fd, F_SETFL, flags | O_NONBLOCK) == 0;
|
|
}
|
|
bool receive(int fd, std::vector<char>& queued, bool drop, std::atomic<std::uint64_t>& discarded) {
|
|
if (queued.size() >= 65536)
|
|
return true;
|
|
std::array<char, 8192> bytes{};
|
|
const auto count = ::recv(fd, bytes.data(), std::min(bytes.size(), 65536 - queued.size()), 0);
|
|
if (count < 0)
|
|
return errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR;
|
|
if (count == 0)
|
|
return false;
|
|
if (drop)
|
|
discarded.fetch_add(static_cast<std::uint64_t>(count));
|
|
else
|
|
queued.insert(queued.end(), bytes.begin(), bytes.begin() + count);
|
|
return true;
|
|
}
|
|
bool transmit(int fd, std::vector<char>& queued) {
|
|
if (queued.empty())
|
|
return true;
|
|
const auto count = ::send(fd, queued.data(), queued.size(), MSG_NOSIGNAL);
|
|
if (count < 0)
|
|
return errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR;
|
|
if (count == 0)
|
|
return false;
|
|
queued.erase(queued.begin(), queued.begin() + count);
|
|
return true;
|
|
}
|
|
} // namespace
|
|
struct PostgresRelay::Impl {
|
|
Socket listener;
|
|
std::uint16_t port{}, backend{};
|
|
std::atomic<bool> requests{}, responses{}, failed{};
|
|
std::atomic<std::uint64_t> request_bytes{}, response_bytes{};
|
|
std::jthread worker;
|
|
explicit Impl(std::uint16_t target)
|
|
: listener(::socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0)), backend(target) {
|
|
auto address = loopback(0);
|
|
socklen_t size = sizeof(address);
|
|
if (target == 0 || listener.fd < 0 ||
|
|
::bind(listener.fd, reinterpret_cast<const sockaddr*>(&address), sizeof(address)) !=
|
|
0 ||
|
|
::listen(listener.fd, 8) != 0 || !nonblocking(listener.fd) ||
|
|
::getsockname(listener.fd, reinterpret_cast<sockaddr*>(&address), &size) != 0)
|
|
throw std::runtime_error("private PostgreSQL relay listener");
|
|
port = ntohs(address.sin_port);
|
|
worker = std::jthread([this](const std::stop_token& stop) {
|
|
try {
|
|
run(stop);
|
|
} catch (...) {
|
|
failed = true;
|
|
}
|
|
});
|
|
}
|
|
void run(const std::stop_token& stop) {
|
|
Socket client, server;
|
|
std::vector<char> upstream, downstream;
|
|
while (!stop.stop_requested()) {
|
|
if (requests.load()) {
|
|
request_bytes.fetch_add(upstream.size());
|
|
upstream.clear();
|
|
}
|
|
if (responses.load()) {
|
|
response_bytes.fetch_add(downstream.size());
|
|
downstream.clear();
|
|
}
|
|
std::array<pollfd, 3> fds{
|
|
{{listener.fd, static_cast<short>(client.fd < 0 ? POLLIN : 0), 0},
|
|
{client.fd, static_cast<short>(POLLIN | (downstream.empty() ? 0 : POLLOUT)), 0},
|
|
{server.fd, static_cast<short>(POLLIN | (upstream.empty() ? 0 : POLLOUT)), 0}}};
|
|
const auto count = ::poll(fds.data(), fds.size(), 10);
|
|
if (count < 0) {
|
|
if (errno == EINTR)
|
|
continue;
|
|
failed = true;
|
|
return;
|
|
}
|
|
if (fds[0].revents & POLLIN) {
|
|
client = Socket(::accept4(listener.fd, nullptr, nullptr, SOCK_CLOEXEC));
|
|
server = Socket(::socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0));
|
|
const auto target = loopback(backend);
|
|
if (client.fd < 0 || server.fd < 0 ||
|
|
::connect(server.fd, reinterpret_cast<const sockaddr*>(&target),
|
|
sizeof(target)) != 0 ||
|
|
!nonblocking(client.fd) || !nonblocking(server.fd)) {
|
|
failed = true;
|
|
return;
|
|
}
|
|
}
|
|
bool alive = true;
|
|
if (fds[1].revents & POLLIN)
|
|
alive = receive(client.fd, upstream, requests.load(), request_bytes);
|
|
if (alive && (fds[2].revents & POLLIN))
|
|
alive = receive(server.fd, downstream, responses.load(), response_bytes);
|
|
if (alive && (fds[1].revents & POLLOUT))
|
|
alive = transmit(client.fd, downstream);
|
|
if (alive && (fds[2].revents & POLLOUT))
|
|
alive = transmit(server.fd, upstream);
|
|
if (!alive || (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) ||
|
|
(fds[2].revents & (POLLERR | POLLHUP | POLLNVAL))) {
|
|
client = Socket{};
|
|
server = Socket{};
|
|
upstream.clear();
|
|
downstream.clear();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
PostgresRelay::PostgresRelay(std::uint16_t backend_port)
|
|
: impl_(std::make_unique<Impl>(backend_port)) {}
|
|
PostgresRelay::~PostgresRelay() = default;
|
|
std::string PostgresRelay::port() const {
|
|
return std::to_string(impl_->port);
|
|
}
|
|
void PostgresRelay::drop_requests(bool value) {
|
|
impl_->requests = value;
|
|
}
|
|
void PostgresRelay::drop_responses(bool value) {
|
|
impl_->responses = value;
|
|
}
|
|
std::uint64_t PostgresRelay::dropped_requests() const {
|
|
return impl_->request_bytes.load();
|
|
}
|
|
std::uint64_t PostgresRelay::dropped_responses() const {
|
|
return impl_->response_bytes.load();
|
|
}
|
|
bool PostgresRelay::healthy() const {
|
|
return !impl_->failed.load();
|
|
}
|
|
} // namespace evo::test
|