Tigher loop in KeepAlive; skip syscalls if overloaded
This commit is contained in:
2
firecgi
2
firecgi
Submodule firecgi updated: 0ba446bacb...a39ef8e25d
13
keepalive.cc
13
keepalive.cc
@@ -8,12 +8,12 @@ namespace firesse {
|
||||
KeepAlive::KeepAlive(const std::chrono::nanoseconds& max_stale, Index* index)
|
||||
: max_stale_(max_stale),
|
||||
index_(index),
|
||||
shutdown_(eventfd(0, 0)) {
|
||||
PCHECK(shutdown_ >= 0) << "eventfd()";
|
||||
shutdown_fd_(eventfd(0, 0)) {
|
||||
PCHECK(shutdown_fd_ >= 0) << "eventfd()";
|
||||
}
|
||||
|
||||
KeepAlive::~KeepAlive() {
|
||||
PCHECK(close(shutdown_) == 0);
|
||||
PCHECK(close(shutdown_fd_) == 0);
|
||||
}
|
||||
|
||||
void KeepAlive::Start() {
|
||||
@@ -22,11 +22,11 @@ void KeepAlive::Start() {
|
||||
constexpr auto num_fds = 1;
|
||||
pollfd fds[num_fds] = {
|
||||
{
|
||||
.fd = shutdown_,
|
||||
.fd = shutdown_fd_,
|
||||
.events = POLLIN,
|
||||
},
|
||||
};
|
||||
while (poll(fds, num_fds, timeout) <= 0) {
|
||||
while (running_ && (timeout == 0 || poll(fds, num_fds, timeout) <= 0)) {
|
||||
auto sleep = index_->WithStalest([](Stream* stream) {
|
||||
stream->WriteRaw(":\n");
|
||||
}, max_stale_);
|
||||
@@ -38,8 +38,9 @@ void KeepAlive::Start() {
|
||||
void KeepAlive::Stop() {
|
||||
CHECK(thread_.joinable());
|
||||
|
||||
running_ = false;
|
||||
uint64_t shutdown = 1;
|
||||
PCHECK(write(shutdown_, &shutdown, sizeof(shutdown)) == sizeof(shutdown));
|
||||
PCHECK(write(shutdown_fd_, &shutdown, sizeof(shutdown)) == sizeof(shutdown));
|
||||
thread_.join();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
#include "index.h"
|
||||
@@ -17,7 +18,11 @@ class KeepAlive {
|
||||
private:
|
||||
const std::chrono::nanoseconds max_stale_;
|
||||
Index* index_;
|
||||
int shutdown_;
|
||||
|
||||
// Two shutdown mechanisms, one for if we're in a tight no-syscall loop,
|
||||
// and one for if we're sleeping in poll()
|
||||
int shutdown_fd_;
|
||||
std::atomic<bool> running_ = true;
|
||||
|
||||
std::thread thread_;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user