Files
aevolution/include/evo/server/ops_http.hpp
T
dingoandClaude Opus 5.5 7273b157d6
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (checks) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m47s
Foundation / postgres-tools (push) Successful in 29s
Foundation / server-image (push) Successful in 4m42s
Foundation / image (push) Successful in 5m55s
feat(admin): live tuning revisions adopted at recorded ticks (schema 66)
ADR-0182 section 13. The admin portal's Tuning page edits the world's
rules-13 tuning (the Git revamp file format); the admin API checks it,
stores an append-only revision and queues an adopt_tuning admin command
(kind 7: revision and rules digest) for every region of the world.

- Lifecycle: adopt_tuning replaces the region's rules after births when
  a resolver names the rules for that revision and digest; the safety
  revision cannot change live. At most one adoption per batch.
- Host: fetches the revision text, rebuilds and digests the rules before
  appending the command; publication and recovery name the rules from
  the successor state, checked against the recorded digest.
- Schema 66: tuning_revisions, admin_set_tuning_v1, load_tuning_revision_v1
  (runtime), admin_tuning_revisions_v1, admin_tuning_text_v1.
- OpsServer: optional bounded request bodies (Content-Length, 413).
- Region view: rules_sha256, so the portal shows each region's revision.
- Portal: Tuning page; baseline from the mounted cellular-revamp.tuning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 19:35:09 -04:00

97 lines
4.4 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"};
std::string headers{}; // Extra header lines, each ending in CRLF (ADR-0182 portal).
};
// Receives the request path (no query handling: probes use exact paths).
using OpsHandler = std::function<OpsResponse(std::string_view path)>;
// ADR-0131 API mode (the grant broker): GET or POST, the path and the Authorization header
// value (empty when absent). A body (Content-Length, no chunked encoding) is accepted only
// when the server is asked for one (ADR-0182 admin tuning); otherwise it is a 400. Views are
// valid during the call only.
struct ApiRequest {
std::string_view method, path, authorization;
std::string_view body{};
};
using ApiHandler = std::function<OpsResponse(const ApiRequest&)>;
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_api_request_bytes = 8192; // Room for an access token.
static constexpr std::size_t max_api_body_bytes = std::size_t{64} << 10U; // Beyond those.
static constexpr std::size_t max_response_bytes = 16384; // The default bound.
// ADR-0182: the admin view and portal answer up to this many bytes when asked to.
static constexpr std::size_t max_configurable_response_bytes = std::size_t{4} << 20U;
static constexpr std::chrono::milliseconds connection_deadline{1000};
// Numeric address only ("127.0.0.1:9100" or "[::1]:9100"); port 0 picks one. A
// response over `response_limit` bytes (at most max_configurable_response_bytes) is
// replaced by a 500.
[[nodiscard]] static std::expected<std::unique_ptr<OpsServer>, OpsError>
listen(std::string_view address, std::size_t response_limit = max_response_bytes);
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);
// The same bounded loop for API requests (max_api_request_bytes), with request bodies of
// at most `body_limit` (at most max_api_body_bytes) bytes; larger ones answer 413.
void serve_api(const ApiHandler& handler, std::size_t body_limit = 0);
[[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, std::size_t response_limit)
: fd_(fd), port_(port), response_limit_(response_limit) {}
void respond(Connection& c, const OpsHandler& handler);
void respond_api(Connection& c, const ApiHandler& handler, std::size_t body_limit);
template <typename Answer> void serve_with(std::size_t request_limit, const Answer& answer);
int fd_;
std::uint16_t port_;
std::size_t response_limit_;
std::vector<Connection> connections_;
std::uint64_t refused_{};
};
} // namespace evo::server