NetworkedApplications: networked-kv composition skeleton
This commit is contained in:
1 parent
a4c0bee501
commit
808b6d73df
4 files changed
+409
No files matched your search
@@ -0,0 +1,202 @@
|
||||
#include "networked_kv/application.hpp"
|
||||
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace NetworkedKv
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] Void::Result::Error
|
||||
config_error(std::string message)
|
||||
{
|
||||
return Void::Result::Error{Void::Result::ErrorKind::invalid_argument,
|
||||
std::move(message)};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Error state_error(std::string message)
|
||||
{
|
||||
return Void::Result::Error{Void::Result::ErrorKind::conflict,
|
||||
std::move(message)};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Async::OperationOptions
|
||||
deadline_after(std::chrono::milliseconds grace)
|
||||
{
|
||||
Void::Async::OperationOptions options;
|
||||
options.deadline = Void::Time::SystemMonotonicClock::instance().now() +
|
||||
grace;
|
||||
return options;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<void> NetworkedConfig::validate() const
|
||||
{
|
||||
if (listen_address.empty()) {
|
||||
return std::unexpected{
|
||||
config_error("NetworkedConfig: listen_address must not be empty")};
|
||||
}
|
||||
if (max_connections == 0) {
|
||||
return std::unexpected{
|
||||
config_error("NetworkedConfig: max_connections must be positive")};
|
||||
}
|
||||
if (max_request_bytes == 0) {
|
||||
return std::unexpected{config_error(
|
||||
"NetworkedConfig: max_request_bytes must be positive")};
|
||||
}
|
||||
if (max_scan_page == 0) {
|
||||
return std::unexpected{
|
||||
config_error("NetworkedConfig: max_scan_page must be positive")};
|
||||
}
|
||||
if (request_deadline.count() <= 0) {
|
||||
return std::unexpected{config_error(
|
||||
"NetworkedConfig: request_deadline must be positive")};
|
||||
}
|
||||
if (shutdown_grace.count() < 0) {
|
||||
return std::unexpected{config_error(
|
||||
"NetworkedConfig: shutdown_grace must not be negative")};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
NetworkedApplication::NetworkedApplication(NetworkedApplication&& other) noexcept
|
||||
: m_config(std::move(other.m_config))
|
||||
, m_resources(other.m_resources)
|
||||
, m_owned_host(std::move(other.m_owned_host))
|
||||
, m_host(other.m_host)
|
||||
, m_state(other.m_state)
|
||||
{
|
||||
other.m_host = nullptr;
|
||||
other.m_state = ApplicationState::Stopped;
|
||||
}
|
||||
|
||||
NetworkedApplication&
|
||||
NetworkedApplication::operator=(NetworkedApplication&& other) noexcept
|
||||
{
|
||||
if (this != &other) {
|
||||
stop_owned_host();
|
||||
m_config = std::move(other.m_config);
|
||||
m_resources = other.m_resources;
|
||||
m_owned_host = std::move(other.m_owned_host);
|
||||
m_host = other.m_host;
|
||||
m_state = other.m_state;
|
||||
other.m_host = nullptr;
|
||||
other.m_state = ApplicationState::Stopped;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
NetworkedApplication::~NetworkedApplication()
|
||||
{
|
||||
stop_owned_host();
|
||||
}
|
||||
|
||||
Void::Result::Result<NetworkedApplication>
|
||||
NetworkedApplication::create(ApplicationResources resources,
|
||||
NetworkedConfig config)
|
||||
{
|
||||
if (auto valid = config.validate(); !valid) {
|
||||
return std::unexpected{std::move(valid).error()};
|
||||
}
|
||||
if (resources.ownership == ResourceOwnership::Embedded &&
|
||||
(resources.borrowed_host == nullptr ||
|
||||
!resources.borrowed_host->valid())) {
|
||||
return std::unexpected{config_error(
|
||||
"NetworkedApplication: embedded mode requires a valid borrowed "
|
||||
"host")};
|
||||
}
|
||||
NetworkedApplication app;
|
||||
app.m_config = std::move(config);
|
||||
app.m_resources = resources;
|
||||
return app;
|
||||
}
|
||||
|
||||
Void::Result::Result<void> NetworkedApplication::start()
|
||||
{
|
||||
if (m_state == ApplicationState::Running) {
|
||||
return std::unexpected{
|
||||
state_error("NetworkedApplication: already running")};
|
||||
}
|
||||
if (m_resources.ownership == ResourceOwnership::Managed) {
|
||||
if (!m_owned_host) {
|
||||
auto created =
|
||||
Void::Runtime::Host::create(m_resources.runtime_options);
|
||||
if (!created) {
|
||||
m_state = ApplicationState::Failed;
|
||||
return std::unexpected{std::move(created).error()};
|
||||
}
|
||||
m_owned_host =
|
||||
std::make_unique<Void::Runtime::Host>(std::move(*created));
|
||||
m_host = m_owned_host.get();
|
||||
}
|
||||
if (!m_host->is_running()) {
|
||||
if (auto started = m_host->start(); !started) {
|
||||
m_state = ApplicationState::Failed;
|
||||
return started;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
m_host = m_resources.borrowed_host;
|
||||
if (m_host == nullptr || !m_host->is_running()) {
|
||||
return std::unexpected{state_error(
|
||||
"NetworkedApplication: embedded mode requires a running "
|
||||
"borrowed host")};
|
||||
}
|
||||
}
|
||||
m_state = ApplicationState::Running;
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<Void::Runtime::ShutdownReport>
|
||||
NetworkedApplication::stop()
|
||||
{
|
||||
if (m_state == ApplicationState::Stopped) {
|
||||
return Void::Runtime::ShutdownReport{};
|
||||
}
|
||||
if (m_state != ApplicationState::Running) {
|
||||
m_state = ApplicationState::Stopped;
|
||||
return Void::Runtime::ShutdownReport{};
|
||||
}
|
||||
m_state = ApplicationState::Stopping;
|
||||
if (m_resources.ownership == ResourceOwnership::Managed && m_host) {
|
||||
auto report =
|
||||
m_host->shutdown_blocking(deadline_after(m_config.shutdown_grace));
|
||||
m_state = ApplicationState::Stopped;
|
||||
return report;
|
||||
}
|
||||
// Embedded: the borrowed host belongs to the application, so only
|
||||
// this composition's view stops.
|
||||
m_state = ApplicationState::Stopped;
|
||||
Void::Runtime::ShutdownReport report;
|
||||
report.complete = true;
|
||||
report.state = m_host ? m_host->state()
|
||||
: Void::Runtime::HostState::stopped;
|
||||
return report;
|
||||
}
|
||||
|
||||
HealthSnapshot NetworkedApplication::health() const
|
||||
{
|
||||
HealthSnapshot snapshot;
|
||||
snapshot.state = m_state;
|
||||
snapshot.managed = m_resources.ownership == ResourceOwnership::Managed;
|
||||
if (m_host != nullptr) {
|
||||
snapshot.ready = m_host->is_ready();
|
||||
snapshot.started_workers = m_host->started_worker_count();
|
||||
}
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
void NetworkedApplication::stop_owned_host() noexcept
|
||||
{
|
||||
if (m_resources.ownership != ResourceOwnership::Managed ||
|
||||
!m_owned_host || m_state != ApplicationState::Running) {
|
||||
return;
|
||||
}
|
||||
m_state = ApplicationState::Stopping;
|
||||
(void)m_owned_host->shutdown_blocking(
|
||||
deadline_after(m_config.shutdown_grace));
|
||||
m_state = ApplicationState::Stopped;
|
||||
}
|
||||
} // namespace NetworkedKv
|
||||
Reference in new issue
Block a user