some fixes

This commit is contained in:
Ayzen
2026-06-04 18:33:38 +03:00
parent eacea436a4
commit 22942d9dc9
26 changed files with 1352 additions and 153 deletions
@@ -131,9 +131,15 @@ ClientQueue::ClientQueue(std::size_t capacity) : capacity_(std::max<std::size_t>
auto ClientQueue::try_push(std::vector<std::uint8_t> packet) -> bool {
{
std::lock_guard<std::mutex> guard(mutex_);
if (closed_ || queue_.size() >= capacity_) {
if (closed_) {
return false;
}
// Latest-wins backpressure: never block or disconnect a slow client. When the
// queue is full, drop the oldest queued packet(s) so the client always advances
// toward the freshest result. Bounded memory; freshness over completeness.
while (queue_.size() >= capacity_) {
queue_.pop_front();
}
queue_.push_back(std::move(packet));
}
not_empty_.notify_one();
@@ -195,10 +201,9 @@ void ClientSession::enqueue(std::vector<std::uint8_t> packet) {
if (stop_requested_.load(std::memory_order_acquire)) {
return;
}
if (!queue_.try_push(std::move(packet))) {
log_warning("disconnecting client " + peer_name_ + " after outbound queue overflow");
request_stop();
}
// try_push only fails when the queue is closed (session already shutting down); a
// full queue now drops its oldest entry instead of disconnecting a slow client.
(void)queue_.try_push(std::move(packet));
}
void ClientSession::request_stop() {
@@ -424,7 +429,15 @@ void TcpServer::acceptor_loop() {
&peer_len
);
if (client_fd < 0) {
if (errno == EINTR) {
if (errno == EINTR || errno == ECONNABORTED) {
continue;
}
if (errno == EMFILE || errno == ENFILE || errno == ENOBUFS || errno == ENOMEM) {
// Transient resource exhaustion (often our own finished sessions
// still holding fds): reap them, back off briefly, and keep
// accepting. The acceptor must never die and silently stop serving.
reap_finished_clients();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
continue;
}
// Listening socket closed during shutdown produces EBADF/EINVAL; bail.