ReloadableApplications: pinned release generations
This commit is contained in:
1 parent
51bae9bfdf
commit
0f2cab97d5
4 files changed
+293
-1
No files matched your search
@@ -0,0 +1,117 @@
|
||||
#include "reloadable_host/release_registry.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace ReloadableHost
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] Void::Result::Error state_error(std::string message)
|
||||
{
|
||||
return Void::Result::Error{Void::Result::ErrorKind::conflict,
|
||||
std::move(message)};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Error limit_error(std::string message)
|
||||
{
|
||||
return Void::Result::Error{Void::Result::ErrorKind::exhausted,
|
||||
std::move(message)};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Error invalid_release(std::string message)
|
||||
{
|
||||
return Void::Result::Error{Void::Result::ErrorKind::invalid_argument,
|
||||
std::move(message)};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ReleaseRegistry::ReleaseRegistry(ReleaseLimits limits)
|
||||
: m_limits(limits)
|
||||
{
|
||||
}
|
||||
|
||||
Void::Result::Result<std::uint64_t>
|
||||
ReleaseRegistry::publish(ReleaseDescriptor candidate,
|
||||
const ReleaseValidator& validate)
|
||||
{
|
||||
if (candidate.id.empty()) {
|
||||
return std::unexpected{
|
||||
invalid_release("ReleaseRegistry: release id must not be empty")};
|
||||
}
|
||||
|
||||
Void::Result::Result<void> validated;
|
||||
try {
|
||||
validated =
|
||||
validate ? validate(candidate) : Void::Result::Result<void>{};
|
||||
} catch (const std::exception& error) {
|
||||
validated = std::unexpected{invalid_release(
|
||||
std::string{"validator threw: "} + error.what())};
|
||||
} catch (...) {
|
||||
validated = std::unexpected{
|
||||
invalid_release("validator threw an unknown exception")};
|
||||
}
|
||||
if (!validated) {
|
||||
return std::unexpected{validated.error()};
|
||||
}
|
||||
|
||||
std::lock_guard lock{m_mutex};
|
||||
if (m_current && m_current->id == candidate.id) {
|
||||
return std::unexpected{state_error(
|
||||
"ReleaseRegistry: release id is already published")};
|
||||
}
|
||||
(void)collect_locked();
|
||||
if (m_retained.size() >= m_limits.max_retained_generations) {
|
||||
return std::unexpected{limit_error(
|
||||
"ReleaseRegistry: retained generation cap reached; release "
|
||||
"pins before reloading")};
|
||||
}
|
||||
|
||||
candidate.generation = m_next_generation++;
|
||||
auto published =
|
||||
std::make_shared<const ReleaseDescriptor>(std::move(candidate));
|
||||
if (m_current) {
|
||||
m_retained.push_back(std::move(m_current));
|
||||
}
|
||||
m_current = std::move(published);
|
||||
return m_current->generation;
|
||||
}
|
||||
|
||||
ReleaseHandle ReleaseRegistry::acquire() const
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
return m_current;
|
||||
}
|
||||
|
||||
std::optional<ReleaseDescriptor> ReleaseRegistry::current() const
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
if (!m_current) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return *m_current;
|
||||
}
|
||||
|
||||
std::size_t ReleaseRegistry::collect_locked() noexcept
|
||||
{
|
||||
const auto before = m_retained.size();
|
||||
std::erase_if(m_retained, [](const ReleaseHandle& handle) {
|
||||
return handle.use_count() == 1;
|
||||
});
|
||||
return before - m_retained.size();
|
||||
}
|
||||
|
||||
std::size_t ReleaseRegistry::collect() noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
return collect_locked();
|
||||
}
|
||||
|
||||
std::size_t ReleaseRegistry::retained() const noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
return m_retained.size();
|
||||
}
|
||||
} // namespace ReloadableHost
|
||||
Reference in new issue
Block a user