Files
aevolution/include/evo/server/ops_http.hpp
T
dingoandClaude Opus 5.5 168c90d5b2 Serialize build retirement and clone classification, bound ops accepts
Review fixes for M7.6-M7.9. Schema v9 applies one lock order: the
barrier takes a database-wide upgrade lock exclusively, while
acquisitions, lease checks, provisioning and the retired-build trigger
take it shared first, so a queued acquisition can no longer regain
authority for a just-retired build and concurrent barriers cannot
retire the last current build. Provisioning takes a per-world lock
before classifying a world as original or a clone of one source. The
operations listener makes at most four accept attempts per serve().
Race regressions prove each contender is blocked before commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 08:38:13 -04:00

74 lines
2.8 KiB
C++

#pragma once
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <functional>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
// Minimal operational HTTP/1.1 responder for probes and metrics (M7.8, ADR-0066).
// Linux. Not a general web server: GET only, one request per connection
// (Connection: close), bounded connections, request bytes, response bytes and
// per-connection time. serve() never blocks; it runs on the host's own loop so
// answers always describe the current state and no thread is added.
namespace evo::server {
struct OpsResponse {
int status{200};
std::string body;
std::string content_type{"text/plain; charset=utf-8"};
};
// Receives the request path (no query handling: probes use exact paths).
using OpsHandler = std::function<OpsResponse(std::string_view path)>;
enum class OpsError : std::uint8_t { address, socket };
class OpsServer {
public:
static constexpr std::size_t max_connections = 8;
// Accept attempts per serve(), refused ones included: sustained connection
// pressure cannot hold the host loop; the rest wait in the listen backlog.
static constexpr std::size_t max_accepts_per_serve = 4;
static constexpr std::size_t max_request_bytes = 4096;
static constexpr std::size_t max_response_bytes = 16384;
static constexpr std::chrono::milliseconds connection_deadline{1000};
// Numeric address only ("127.0.0.1:9100" or "[::1]:9100"); port 0 picks one.
[[nodiscard]] static std::expected<std::unique_ptr<OpsServer>, OpsError>
listen(std::string_view address);
OpsServer(const OpsServer&) = delete;
OpsServer& operator=(const OpsServer&) = delete;
OpsServer(OpsServer&&) = delete;
OpsServer& operator=(OpsServer&&) = delete;
~OpsServer();
[[nodiscard]] std::uint16_t port() const noexcept {
return port_;
}
// Accepts at most max_accepts_per_serve connections, then reads, answers and
// expires connections without waiting. Connections beyond the bound are closed
// at once; slow or oversized ones are answered or dropped without delaying others.
void serve(const OpsHandler& handler);
[[nodiscard]] std::uint64_t refused() const noexcept {
return refused_;
}
[[nodiscard]] std::size_t open_connections() const noexcept {
return connections_.size();
}
private:
struct Connection {
int fd{-1};
std::string in, out;
std::size_t sent{};
std::chrono::steady_clock::time_point deadline{};
};
OpsServer(int fd, std::uint16_t port) : fd_(fd), port_(port) {}
void respond(Connection& c, const OpsHandler& handler);
int fd_;
std::uint16_t port_;
std::vector<Connection> connections_;
std::uint64_t refused_{};
};
} // namespace evo::server