1 parent
3851aeb703
commit
053ee5b296
23 files changed
+845
-93
No files matched your search
@@ -49,6 +49,11 @@ namespace Void::Plugin
|
||||
[[nodiscard]] explicit operator bool() const noexcept;
|
||||
|
||||
[[nodiscard]] const PluginInfo& info() const noexcept;
|
||||
/// Resolve an optional symbol from the exact shared object pinned
|
||||
/// by this handle. The returned address remains valid while the
|
||||
/// handle is alive, even if the manager concurrently reloads the
|
||||
/// plugin.
|
||||
[[nodiscard]] void* raw_symbol(const char* symbol) const noexcept;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
@@ -5,7 +5,10 @@
|
||||
#include <Void/Service/Registry.hpp>
|
||||
#include <Void/Service/Service.hpp>
|
||||
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
@@ -17,8 +20,8 @@ namespace Void::Service
|
||||
/// extern "C" Void::Service::IService* void_plugin_service(void* inst);
|
||||
/// It returns the IService subobject of the plugin's service object
|
||||
/// (which must derive the host interface I, and I must derive
|
||||
/// IService). The host recovers I* by static_cast — no dynamic_cast, so
|
||||
/// it is robust across RTLD_LOCAL plugin boundaries.
|
||||
/// IService). The host verifies the exported subobject with dynamic_cast;
|
||||
/// host and provider must therefore share RTTI identity for I.
|
||||
using service_factory_fn = IService* (*)(void* instance);
|
||||
|
||||
inline constexpr const char* kServiceSymbol = "void_plugin_service";
|
||||
@@ -32,23 +35,28 @@ namespace Void::Service
|
||||
/// callers never see a freed object.
|
||||
///
|
||||
/// Requires: I derives Void::Service::IService; the plugin's service
|
||||
/// type derives I; the plugin exports void_plugin_service.
|
||||
/// type derives I; the plugin exports void_plugin_service. RTTI identity
|
||||
/// for I must be shared by host and provider, just like PluginManager's
|
||||
/// host-specific instance_validator interfaces.
|
||||
template <typename I> class PluginServiceBinding
|
||||
{
|
||||
public:
|
||||
|
||||
static_assert(std::is_base_of_v<IService, I>,
|
||||
"plugin service interface must derive IService");
|
||||
using Validator =
|
||||
std::function<bool(const std::shared_ptr<I>& service)>;
|
||||
|
||||
PluginServiceBinding(Plugin::PluginManager& manager,
|
||||
ServiceRegistry& registry, std::string name)
|
||||
: m_manager(&manager), m_registry(®istry),
|
||||
m_name(std::move(name))
|
||||
ServiceRegistry& registry, std::string name,
|
||||
Validator validator = {})
|
||||
: m_state(std::make_shared<State>(
|
||||
manager, registry, std::move(name), std::move(validator)))
|
||||
{
|
||||
}
|
||||
|
||||
// The reload callback captures `this`; the binding must
|
||||
// deregister it before dying and therefore cannot move.
|
||||
// Lifecycle registration cannot be transferred while callbacks may
|
||||
// be in flight.
|
||||
PluginServiceBinding(const PluginServiceBinding&) = delete;
|
||||
PluginServiceBinding&
|
||||
operator=(const PluginServiceBinding&) = delete;
|
||||
@@ -56,65 +64,181 @@ namespace Void::Service
|
||||
~PluginServiceBinding() { disable_hot_reload(); }
|
||||
|
||||
/// Resolve the current instance's service and (re)register it.
|
||||
[[nodiscard]] bool bind()
|
||||
{
|
||||
auto handle = m_manager->acquire(m_name);
|
||||
if (!handle) {
|
||||
return false;
|
||||
}
|
||||
auto* fn = reinterpret_cast<service_factory_fn>(
|
||||
m_manager->raw_symbol(m_name, kServiceSymbol));
|
||||
if (fn == nullptr) {
|
||||
return false;
|
||||
}
|
||||
IService* base = fn(handle.get());
|
||||
if (base == nullptr) {
|
||||
return false;
|
||||
}
|
||||
I* iface = static_cast<I*>(base); // I : IService
|
||||
|
||||
// Keep the plugin instance (and its mapped .so) alive for
|
||||
// as long as the registry holds this service.
|
||||
auto owner = std::make_shared<Plugin::PluginHandle>(handle);
|
||||
std::shared_ptr<I> pinned(std::move(owner), iface);
|
||||
m_registry->provide<I>(std::move(pinned));
|
||||
Void::Log::debug("Service.Plugin",
|
||||
"bound service from plugin '{}'", m_name);
|
||||
return true;
|
||||
}
|
||||
[[nodiscard]] bool bind() noexcept { return m_state->bind(); }
|
||||
|
||||
/// Auto-rebind on hot reload (debounced file watching). The
|
||||
/// manager keeps old instances alive while pinned, so the swap
|
||||
/// is seamless. Idempotent.
|
||||
void enable_hot_reload()
|
||||
{
|
||||
std::lock_guard lifecycleLock{m_lifecycleMutex};
|
||||
if (m_reload_id != 0) {
|
||||
return;
|
||||
}
|
||||
m_reload_id =
|
||||
m_manager->on_reloaded([this](std::string_view name) {
|
||||
if (name == m_name) {
|
||||
(void)bind();
|
||||
}
|
||||
});
|
||||
m_manager->watch(m_name);
|
||||
m_state->set_hot_reload_enabled(true);
|
||||
try {
|
||||
std::weak_ptr<State> weakState{m_state};
|
||||
m_reload_id = m_state->manager().on_reloaded(
|
||||
[weakState](std::string_view name) noexcept {
|
||||
if (auto state = weakState.lock()) {
|
||||
state->rebind(name);
|
||||
}
|
||||
});
|
||||
m_state->manager().watch(m_state->name());
|
||||
} catch (...) {
|
||||
m_state->set_hot_reload_enabled(false);
|
||||
if (m_reload_id != 0) {
|
||||
m_state->manager().remove_on_reloaded(m_reload_id);
|
||||
m_reload_id = 0;
|
||||
}
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// Stop observing reloads (the file watch itself is shared and
|
||||
/// stays). Idempotent; called by the destructor.
|
||||
void disable_hot_reload()
|
||||
void disable_hot_reload() noexcept
|
||||
{
|
||||
if (m_reload_id != 0) {
|
||||
m_manager->remove_on_reloaded(m_reload_id);
|
||||
m_reload_id = 0;
|
||||
try {
|
||||
std::lock_guard lifecycleLock{m_lifecycleMutex};
|
||||
m_state->set_hot_reload_enabled(false);
|
||||
if (m_reload_id != 0) {
|
||||
m_state->manager().remove_on_reloaded(m_reload_id);
|
||||
m_reload_id = 0;
|
||||
}
|
||||
} catch (...) {
|
||||
// Destruction must remain noexcept. The weak callback
|
||||
// cannot keep State alive after m_state is released even if
|
||||
// observer removal itself reports a failure.
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Plugin::PluginManager* m_manager;
|
||||
ServiceRegistry* m_registry;
|
||||
std::string m_name;
|
||||
class State
|
||||
{
|
||||
public:
|
||||
|
||||
State(Plugin::PluginManager& manager,
|
||||
ServiceRegistry& registry, std::string name,
|
||||
Validator validator)
|
||||
: m_manager(&manager), m_registry(®istry),
|
||||
m_name(std::move(name)),
|
||||
m_validator(std::move(validator))
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] bool bind() noexcept
|
||||
{
|
||||
try {
|
||||
std::lock_guard lock{m_mutex};
|
||||
return bind_locked();
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void rebind(std::string_view name) noexcept
|
||||
{
|
||||
if (name != m_name) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
std::lock_guard lock{m_mutex};
|
||||
if (m_hotReloadEnabled) {
|
||||
(void)bind_locked();
|
||||
}
|
||||
} catch (...) {
|
||||
}
|
||||
}
|
||||
|
||||
void set_hot_reload_enabled(bool enabled)
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
m_hotReloadEnabled = enabled;
|
||||
}
|
||||
|
||||
[[nodiscard]] Plugin::PluginManager&
|
||||
manager() const noexcept
|
||||
{
|
||||
return *m_manager;
|
||||
}
|
||||
|
||||
[[nodiscard]] const std::string& name() const noexcept
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
[[nodiscard]] bool bind_locked() noexcept
|
||||
{
|
||||
try {
|
||||
auto handle = m_manager->acquire(m_name);
|
||||
if (!handle) {
|
||||
return false;
|
||||
}
|
||||
auto* factory =
|
||||
reinterpret_cast<service_factory_fn>(
|
||||
handle.raw_symbol(kServiceSymbol));
|
||||
if (factory == nullptr) {
|
||||
return false;
|
||||
}
|
||||
IService* base = factory(handle.get());
|
||||
if (base == nullptr) {
|
||||
return false;
|
||||
}
|
||||
I* iface = dynamic_cast<I*>(base);
|
||||
if (iface == nullptr) {
|
||||
Void::Log::warn(
|
||||
"Service.Plugin",
|
||||
"plugin '{}' exported an incompatible "
|
||||
"service interface",
|
||||
m_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Pin the exact instance and shared object that
|
||||
// supplied the factory and service subobject.
|
||||
auto owner = std::make_shared<Plugin::PluginHandle>(
|
||||
std::move(handle));
|
||||
std::shared_ptr<I> pinned(std::move(owner), iface);
|
||||
if (m_validator && !m_validator(pinned)) {
|
||||
Void::Log::warn(
|
||||
"Service.Plugin",
|
||||
"plugin '{}' service validation failed",
|
||||
m_name);
|
||||
return false;
|
||||
}
|
||||
m_registry->provide<I>(std::move(pinned));
|
||||
Void::Log::debug("Service.Plugin",
|
||||
"bound service from plugin '{}'",
|
||||
m_name);
|
||||
return true;
|
||||
} catch (const std::exception& error) {
|
||||
Void::Log::warn(
|
||||
"Service.Plugin",
|
||||
"binding service from plugin '{}' threw: {}",
|
||||
m_name, error.what());
|
||||
} catch (...) {
|
||||
Void::Log::warn(
|
||||
"Service.Plugin",
|
||||
"binding service from plugin '{}' threw",
|
||||
m_name);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Plugin::PluginManager* m_manager;
|
||||
ServiceRegistry* m_registry;
|
||||
std::string m_name;
|
||||
Validator m_validator;
|
||||
std::mutex m_mutex;
|
||||
bool m_hotReloadEnabled{false};
|
||||
};
|
||||
|
||||
std::shared_ptr<State> m_state;
|
||||
std::mutex m_lifecycleMutex;
|
||||
Plugin::ReloadCallbackId m_reload_id{0};
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Service/Service.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::ShortUrl::V1
|
||||
{
|
||||
inline constexpr std::string_view kServiceId{"void.short-url"};
|
||||
inline constexpr std::uint32_t kServiceVersion{1};
|
||||
|
||||
enum class ErrorCode : std::uint32_t {
|
||||
none = 0,
|
||||
invalid_request,
|
||||
unsupported,
|
||||
not_found,
|
||||
expired,
|
||||
conflict,
|
||||
unauthorized,
|
||||
rate_limited,
|
||||
timeout,
|
||||
unavailable,
|
||||
provider_error,
|
||||
internal_error,
|
||||
invalid_url,
|
||||
revoked,
|
||||
storage,
|
||||
response_too_large,
|
||||
};
|
||||
|
||||
struct Error {
|
||||
ErrorCode code{ErrorCode::none};
|
||||
std::string message;
|
||||
std::uint32_t retry_after_seconds{0};
|
||||
|
||||
[[nodiscard]] bool ok() const noexcept
|
||||
{
|
||||
return code == ErrorCode::none;
|
||||
}
|
||||
|
||||
[[nodiscard]] explicit operator bool() const noexcept
|
||||
{
|
||||
return !ok();
|
||||
}
|
||||
};
|
||||
|
||||
struct ShortenRequest {
|
||||
std::string target_url;
|
||||
std::string custom_key;
|
||||
std::int64_t expires_at_unix_seconds{0};
|
||||
};
|
||||
|
||||
struct ShortenResult {
|
||||
std::string short_url;
|
||||
std::string key;
|
||||
std::int64_t expires_at_unix_seconds{0};
|
||||
Error error;
|
||||
|
||||
[[nodiscard]] bool ok() const noexcept
|
||||
{
|
||||
return error.ok() && !short_url.empty();
|
||||
}
|
||||
|
||||
[[nodiscard]] explicit operator bool() const noexcept
|
||||
{
|
||||
return ok();
|
||||
}
|
||||
};
|
||||
|
||||
enum class HealthStatus : std::uint32_t {
|
||||
unknown = 0,
|
||||
healthy,
|
||||
degraded,
|
||||
unhealthy,
|
||||
};
|
||||
|
||||
struct Health {
|
||||
HealthStatus status{HealthStatus::unknown};
|
||||
std::string message;
|
||||
};
|
||||
|
||||
struct ResolveRequest {
|
||||
std::string key;
|
||||
};
|
||||
|
||||
struct ResolveResult {
|
||||
std::string target_url;
|
||||
std::int64_t expires_at_unix_seconds{0};
|
||||
Error error;
|
||||
|
||||
[[nodiscard]] bool ok() const noexcept
|
||||
{
|
||||
return error.ok() && !target_url.empty();
|
||||
}
|
||||
|
||||
[[nodiscard]] explicit operator bool() const noexcept
|
||||
{
|
||||
return ok();
|
||||
}
|
||||
};
|
||||
|
||||
class IShortener : public Service::IService
|
||||
{
|
||||
public:
|
||||
|
||||
~IShortener() override = default;
|
||||
|
||||
[[nodiscard]] std::string_view service_name() const final
|
||||
{
|
||||
return kServiceId;
|
||||
}
|
||||
|
||||
[[nodiscard]] virtual std::uint32_t service_version() const noexcept
|
||||
{
|
||||
return kServiceVersion;
|
||||
}
|
||||
|
||||
[[nodiscard]] virtual std::string_view
|
||||
provider_id() const noexcept = 0;
|
||||
|
||||
[[nodiscard]] virtual Health health() const = 0;
|
||||
|
||||
[[nodiscard]] virtual ShortenResult
|
||||
shorten(const ShortenRequest& request) = 0;
|
||||
|
||||
[[nodiscard]] virtual bool supports_resolve() const noexcept
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
[[nodiscard]] virtual ResolveResult resolve(const ResolveRequest&)
|
||||
{
|
||||
return {
|
||||
.target_url = {},
|
||||
.expires_at_unix_seconds = 0,
|
||||
.error =
|
||||
{
|
||||
.code = ErrorCode::unsupported,
|
||||
.message =
|
||||
"this shortener does not support resolution",
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Void::ShortUrl::V1
|
||||
|
||||
namespace Void::ShortUrl
|
||||
{
|
||||
using V1::Error;
|
||||
using V1::ErrorCode;
|
||||
using V1::Health;
|
||||
using V1::HealthStatus;
|
||||
using V1::IShortener;
|
||||
using V1::ResolveRequest;
|
||||
using V1::ResolveResult;
|
||||
using V1::ShortenRequest;
|
||||
using V1::ShortenResult;
|
||||
|
||||
inline constexpr auto kServiceId = V1::kServiceId;
|
||||
inline constexpr auto kServiceVersion = V1::kServiceVersion;
|
||||
} // namespace Void::ShortUrl
|
||||
@@ -103,7 +103,10 @@ template <typename T>
|
||||
inline void do_not_optimize(T& val) noexcept
|
||||
{
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
asm volatile("" : "+r,m"(val) : : "memory");
|
||||
// A memory operand works for every object type and remains accepted by
|
||||
// GCC 16, which rejects the old mixed "+r,m" alternative constraint for
|
||||
// some scalar instantiations.
|
||||
asm volatile("" : "+m"(val) : : "memory");
|
||||
#else
|
||||
volatile T* p = &val;
|
||||
(void)*p;
|
||||
@@ -114,7 +117,7 @@ template <typename T>
|
||||
inline void do_not_optimize(const T& val) noexcept
|
||||
{
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
asm volatile("" : : "r,m"(val) : "memory");
|
||||
asm volatile("" : : "m"(val) : "memory");
|
||||
#else
|
||||
volatile const T* p = &val;
|
||||
(void)*p;
|
||||
|
||||
Reference in new issue
Block a user