Reuse VoidLibrary rate limiting and export stable C ABI

This commit is contained in:
faumaray committed 2026-07-29 22:40:35 +03:00
1 parent 2d132d2806
commit c85bf13244
7 files changed
+166 -85

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
name: Independent component release
on:
push:
tags: [v*]
schedule:
- cron: '17 2 * * *'
workflow_dispatch:
jobs:
publish:
runs-on: echoion-release-serial
steps:
- name: Check out component
uses: https://github.com/actions/checkout@11d5960a326750d5838078e36cf38b85af677262
with:
fetch-depth: 0
ssh-key: ${{ secrets.RELEASE_CHECKOUT_SSH_KEY }}
persist-credentials: false
- name: Check out pinned public release tools
uses: https://github.com/actions/checkout@11d5960a326750d5838078e36cf38b85af677262
with:
repository: Void/Echoion
ref: ${{ vars.ECHOION_RELEASE_TOOLS_REF }}
path: .echoion-release-tools
fetch-depth: 0
ssh-key: ${{ secrets.RELEASE_CHECKOUT_SSH_KEY }}
persist-credentials: false
- name: Build, publish, and merge feed transaction
shell: bash
env:
AWS_ACCESS_KEY_ID: ${{ secrets.COMPONENT_OBJECT_STORAGE_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.COMPONENT_OBJECT_STORAGE_SECRET_KEY }}
AWS_DEFAULT_REGION: ${{ vars.COMPONENT_OBJECT_STORAGE_REGION }}
ECHOION_COMPONENT_SSH_KEY: ${{ secrets.RELEASE_CHECKOUT_SSH_KEY }}
ECHOION_DISTRIBUTION_BASE_URL: ${{ vars.ECHOION_DISTRIBUTION_BASE_URL }}
ECHOION_FEED_BASE_URL: ${{ vars.ECHOION_FEED_BASE_URL }}
ECHOION_FEED_COMMIT_SIGNING_KEY: ${{ secrets.RELEASE_FEED_GIT_SIGNING_KEY }}
ECHOION_FEED_PRIVATE_KEY: ${{ secrets.RELEASE_FEED_ED25519_PRIVATE_KEY }}
ECHOION_FEED_REPOSITORY: ssh://gitea@git.faumaray.ru/Void/Echoion.git
ECHOION_FEED_SIGNING_KEY_ID: ${{ vars.ECHOION_FEED_SIGNING_KEY_ID }}
ECHOION_OBJECT_STORAGE_BUCKET: ${{ vars.COMPONENT_OBJECT_STORAGE_BUCKET }}
ECHOION_OBJECT_STORAGE_ENDPOINT: ${{ vars.COMPONENT_OBJECT_STORAGE_ENDPOINT }}
ECHOION_OBJECT_STORAGE_PREFIX: ${{ vars.COMPONENT_OBJECT_STORAGE_PREFIX }}
ECHOION_RELEASE_TAG_ALLOWED_SIGNERS: ${{ secrets.RELEASE_TAG_ALLOWED_SIGNERS }}
GITEA_API_URL: ${{ gitea.api_url }}
GITEA_EVENT_NAME: ${{ gitea.event_name }}
GITEA_REF_NAME: ${{ gitea.ref_name }}
GITEA_RUN_NUMBER: ${{ gitea.run_number }}
GITEA_SHA: ${{ gitea.sha }}
GITEA_REPOSITORY: ${{ gitea.repository }}
GITEA_RELEASE_TOKEN: ${{ secrets.RELEASE_BOT_TOKEN }}
RELEASE_TOOLS_REF: ${{ vars.ECHOION_RELEASE_TOOLS_REF }}
run: |
set -euo pipefail
[[ "$RELEASE_TOOLS_REF" =~ ^[0-9a-f]{40}$ ]]
test "$(git -C .echoion-release-tools rev-parse HEAD)" = "$RELEASE_TOOLS_REF"
.echoion-release-tools/scripts/release-component.sh component.release.json
+6 -2
View File
@@ -1,8 +1,11 @@
cmake_minimum_required(VERSION 4.0.0) cmake_minimum_required(VERSION 4.0.0)
if(NOT DEFINED ECHOION_PRODUCT_VERSION) if(NOT DEFINED ECHOION_PRODUCT_VERSION)
set(ECHOION_PRODUCT_VERSION 2.0.0) set(ECHOION_PRODUCT_VERSION 0.0.0)
endif() endif()
project(EchoionWeb VERSION "${ECHOION_PRODUCT_VERSION}" LANGUAGES CXX) if(NOT DEFINED ECHOION_COMPONENT_VERSION)
set(ECHOION_COMPONENT_VERSION "${ECHOION_PRODUCT_VERSION}")
endif()
project(EchoionWeb VERSION "${ECHOION_COMPONENT_VERSION}" LANGUAGES CXX)
include(GNUInstallDirs) include(GNUInstallDirs)
set(CMAKE_CXX_STANDARD 26) set(CMAKE_CXX_STANDARD 26)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
@@ -73,6 +76,7 @@ target_link_libraries(EchoionWeb
Void::Data Void::Data
Void::Log Void::Log
Void::Async Void::Async
Void::Resilience
PRIVATE PRIVATE
Threads::Threads Threads::Threads
Boost::beast Boost::beast
+3 -2
View File
@@ -1,14 +1,15 @@
{ {
"schemaVersion": 1, "schemaVersion": 1,
"contractId": "echoion.module.web", "contractId": "echoion.module.web",
"descriptor": "Void::Plugin::ModuleDescriptor", "descriptor": "echoion_module_descriptor_v1",
"querySymbol": "echoion_module_abi_query",
"moduleId": "EchoionWeb", "moduleId": "EchoionWeb",
"activation": "restart-required", "activation": "restart-required",
"dependencies": [ "dependencies": [
{ {
"moduleId": "EchoionApi", "moduleId": "EchoionApi",
"versionRange": "^2.0.0", "versionRange": "^2.0.0",
"abiHash": "sha256:336492a52097dd12f222bb6d9acab19e8eb6a502eab45bb78127bce6d733fbd3" "abiHash": "sha256:d50293d345a97e52f9844088bdd42277f566bc815a21b7636d3e053f06c6c113"
} }
] ]
} }
+9
View File
@@ -0,0 +1,9 @@
{
"schemaVersion": 1,
"componentId": "EchoionWeb",
"productCompatibility": ">=2.0.0 <3.0.0",
"publishNightly": true,
"cmakeOptions": {
"ECHOION_BUILD_WEB": "ON"
}
}
+12 -23
View File
@@ -1,5 +1,7 @@
#pragma once #pragma once
#include <Void/Resilience/KeyedRateLimiter.hpp>
#include <Void/Resilience/RateLimiter.hpp>
#include <boost/asio/ip/address.hpp> #include <boost/asio/ip/address.hpp>
#include <chrono> #include <chrono>
@@ -10,7 +12,6 @@
#include <span> #include <span>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <unordered_map>
#include <vector> #include <vector>
namespace Echoion::Web::Security namespace Echoion::Web::Security
@@ -23,8 +24,8 @@ namespace Echoion::Web::Security
CidrBlock(asio::ip::address network, unsigned int prefixLength); CidrBlock(asio::ip::address network, unsigned int prefixLength);
[[nodiscard]] bool contains( [[nodiscard]] bool
const asio::ip::address& address) const noexcept; contains(const asio::ip::address &address) const noexcept;
private: private:
@@ -34,16 +35,17 @@ namespace Echoion::Web::Security
using ParsedCidrs = std::expected<std::vector<CidrBlock>, std::string>; using ParsedCidrs = std::expected<std::vector<CidrBlock>, std::string>;
[[nodiscard]] ParsedCidrs parseTrustedProxyCidrs( [[nodiscard]] ParsedCidrs
std::span<const std::string> cidrs); parseTrustedProxyCidrs(std::span<const std::string> cidrs);
[[nodiscard]] std::string effectiveClientAddress( [[nodiscard]] std::string
const asio::ip::address& immediatePeer, effectiveClientAddress(const asio::ip::address &immediatePeer,
std::optional<std::string_view> forwardedFor, std::optional<std::string_view> forwardedFor,
std::span<const CidrBlock> trustedProxies); std::span<const CidrBlock> trustedProxies);
[[nodiscard]] bool validShortSlug(std::string_view slug) noexcept; [[nodiscard]] bool validShortSlug(std::string_view slug) noexcept;
[[nodiscard]] bool validRedirectLocation(std::string_view location) noexcept; [[nodiscard]] bool
validRedirectLocation(std::string_view location) noexcept;
class ShortLinkRateLimiter class ShortLinkRateLimiter
{ {
@@ -72,25 +74,12 @@ namespace Echoion::Web::Security
private: private:
struct TokenBucket {
double tokens = 0.0;
TimePoint updated{};
bool initialized = false;
};
struct PeerBucket {
TokenBucket bucket;
TimePoint lastSeen{};
};
static void refill(TokenBucket& bucket, double rate, double burst,
TimePoint now) noexcept;
void evictIdle(TimePoint now, bool force); void evictIdle(TimePoint now, bool force);
Config config_; Config config_;
mutable std::mutex mutex_; mutable std::mutex mutex_;
TokenBucket global_; Void::Resilience::TokenBucket global_;
std::unordered_map<std::string, PeerBucket> peers_; Void::Resilience::KeyedRateLimiter<std::string> peers_;
TimePoint nextSweep_{}; TimePoint nextSweep_{};
bool sweepInitialized_ = false; bool sweepInitialized_ = false;
}; };
+20 -51
View File
@@ -240,7 +240,11 @@ namespace Echoion::Web::Security
{ {
} }
ShortLinkRateLimiter::ShortLinkRateLimiter(Config config) : config_(config) ShortLinkRateLimiter::ShortLinkRateLimiter(Config config)
: config_(config), global_(config.globalBurst, config.globalRate),
peers_(
config.peerBurst, config.peerRate, [] { return Clock::now(); },
config.maxPeers)
{ {
if (!std::isfinite(config_.globalRate) || config_.globalRate < 0.0 || if (!std::isfinite(config_.globalRate) || config_.globalRate < 0.0 ||
!std::isfinite(config_.globalBurst) || config_.globalBurst < 1.0 || !std::isfinite(config_.globalBurst) || config_.globalBurst < 1.0 ||
@@ -252,7 +256,6 @@ namespace Echoion::Web::Security
throw std::invalid_argument( throw std::invalid_argument(
"invalid short-link rate limiter settings"); "invalid short-link rate limiter settings");
} }
peers_.reserve(config_.maxPeers);
} }
bool ShortLinkRateLimiter::allow(std::string_view peer) bool ShortLinkRateLimiter::allow(std::string_view peer)
@@ -264,44 +267,36 @@ namespace Echoion::Web::Security
{ {
std::lock_guard lock(mutex_); std::lock_guard lock(mutex_);
evictIdle(now, false); evictIdle(now, false);
refill(global_, config_.globalRate, config_.globalBurst, now); if (global_.available_at(now) < 1.0) {
if (global_.tokens < 1.0) {
return false; return false;
} }
const std::string peerKey{peer}; const std::string peerKey{peer};
auto found = peers_.find(peerKey); auto available = peers_.remaining_at(peerKey, now);
if (found == peers_.end()) { if (!available) {
if (peers_.size() >= config_.maxPeers) {
evictIdle(now, true); evictIdle(now, true);
available = peers_.remaining_at(peerKey, now);
} }
if (peers_.size() >= config_.maxPeers) { if (!available || *available < 1.0) {
return false;
}
found =
peers_
.emplace(peerKey, PeerBucket{.bucket = {}, .lastSeen = now})
.first;
}
found->second.lastSeen = now;
refill(found->second.bucket, config_.peerRate, config_.peerBurst, now);
if (found->second.bucket.tokens < 1.0) {
return false; return false;
} }
// Commit both token deductions together. A peer-local rejection never // Commit both token deductions together. A peer-local rejection never
// consumes global capacity and cannot starve unrelated clients. // consumes global capacity and cannot starve unrelated clients.
global_.tokens -= 1.0; const bool peerAllowed = peers_.allow_at(peerKey, now);
found->second.bucket.tokens -= 1.0; const bool globalAllowed = global_.try_acquire_at(now);
if (!peerAllowed || !globalAllowed) {
throw std::logic_error{
"rate limiter transaction became inconsistent"};
}
return true; return true;
} }
void ShortLinkRateLimiter::reset() void ShortLinkRateLimiter::reset()
{ {
std::lock_guard lock(mutex_); std::lock_guard lock(mutex_);
global_ = {}; global_.reset();
peers_.clear(); peers_.reset();
nextSweep_ = {}; nextSweep_ = {};
sweepInitialized_ = false; sweepInitialized_ = false;
} }
@@ -309,24 +304,7 @@ namespace Echoion::Web::Security
std::size_t ShortLinkRateLimiter::trackedPeerCount() const std::size_t ShortLinkRateLimiter::trackedPeerCount() const
{ {
std::lock_guard lock(mutex_); std::lock_guard lock(mutex_);
return peers_.size(); return peers_.tracked();
}
void ShortLinkRateLimiter::refill(TokenBucket& bucket, double rate,
double burst, TimePoint now) noexcept
{
if (!bucket.initialized) {
bucket.tokens = burst;
bucket.updated = now;
bucket.initialized = true;
return;
}
const auto elapsed =
now > bucket.updated
? std::chrono::duration<double>(now - bucket.updated).count()
: 0.0;
bucket.tokens = std::min(burst, bucket.tokens + elapsed * rate);
bucket.updated = now;
} }
void ShortLinkRateLimiter::evictIdle(TimePoint now, bool force) void ShortLinkRateLimiter::evictIdle(TimePoint now, bool force)
@@ -334,16 +312,7 @@ namespace Echoion::Web::Security
if (!force && sweepInitialized_ && now < nextSweep_) { if (!force && sweepInitialized_ && now < nextSweep_) {
return; return;
} }
for (auto peer = peers_.begin(); peer != peers_.end();) { (void)peers_.cleanup_at(now, config_.idleTimeout);
const bool expired =
now >= peer->second.lastSeen &&
now - peer->second.lastSeen >= config_.idleTimeout;
if (expired) {
peer = peers_.erase(peer);
} else {
++peer;
}
}
nextSweep_ = now + config_.sweepInterval; nextSweep_ = now + config_.sweepInterval;
sweepInitialized_ = true; sweepInitialized_ = true;
} }
+60 -7
View File
@@ -1,5 +1,6 @@
#include <echoion/api/module/Module.hpp> #include <echoion/api/module/LegacyModuleAbi.hpp>
#include <iterator>
#include <memory> #include <memory>
#include <string_view> #include <string_view>
@@ -10,6 +11,7 @@ namespace Echoion::Web
class WebModule final : public Void::Plugin::IModule class WebModule final : public Void::Plugin::IModule
{ {
public: public:
[[nodiscard]] std::string_view name() const noexcept override [[nodiscard]] std::string_view name() const noexcept override
{ {
return "EchoionWeb"; return "EchoionWeb";
@@ -26,25 +28,31 @@ namespace Echoion::Web
{ {
static const Void::Plugin::ModuleDescriptor descriptor{ static const Void::Plugin::ModuleDescriptor descriptor{
.module_id = "EchoionWeb", .module_id = "EchoionWeb",
.component_version = { .component_version =
{
.major = ECHOION_COMPONENT_VERSION_MAJOR, .major = ECHOION_COMPONENT_VERSION_MAJOR,
.minor = ECHOION_COMPONENT_VERSION_MINOR, .minor = ECHOION_COMPONENT_VERSION_MINOR,
.patch = ECHOION_COMPONENT_VERSION_PATCH, .patch = ECHOION_COMPONENT_VERSION_PATCH,
.pre_release = {}, .pre_release = {},
.build = {}, .build = {},
}, },
.abi_hash = .abi_hash = "sha256:"
"sha256:858dfd3a19804e42a63fa8694a70ebe07ee90485a886df1ac07cf91bb5d34316", "e1f826ddb06caadc394af7a3db27af32789a7e580cde6ee516edd0"
"ebda2a5c9f",
.abi_contract = Void::Json::object({ .abi_contract = Void::Json::object({
{"schemaVersion", 1}, {"schemaVersion", 1},
{"contractId", "echoion.module.web"}, {"contractId", "echoion.module.web"},
{"descriptor", "Void::Plugin::ModuleDescriptor"}, {"descriptor", "echoion_module_descriptor_v1"},
}), }),
.dependencies = { .dependencies =
{
{ {
.module_id = "EchoionApi", .module_id = "EchoionApi",
.version_range = "^2.0.0", .version_range = "^2.0.0",
.abi_hash = "sha256:336492a52097dd12f222bb6d9acab19e8eb6a502eab45bb78127bce6d733fbd3", .abi_hash =
"sha256:"
"d50293d345a97e52f9844088bdd42277f566bc815a21b763"
"6d3e053f06c6c113",
}, },
}, },
.activation_policy = .activation_policy =
@@ -53,6 +61,45 @@ namespace Echoion::Web
}; };
return descriptor; return descriptor;
} }
const echoion_module_descriptor_v1 &webModuleAbiDescriptor()
{
using Bridge = Api::Module::LegacyModuleAbi<createWebModule>;
static const echoion_module_dependency_v1 dependencies[]{
{
.struct_size = sizeof(echoion_module_dependency_v1),
.component_id = Api::Module::Detail::stringView("EchoionApi"),
.version_range =
Api::Module::Detail::stringView(">=2.0.0 <3.0.0"),
.abi_hash = Api::Module::Detail::stringView(
"sha256:"
"d50293d345a97e52f9844088bdd42277f566bc815a21b7636d3e053f06"
"c6c11"
"3"),
.optional = 0,
.reserved_bytes = {},
},
};
static const echoion_module_descriptor_v1 descriptor{
.struct_size = sizeof(echoion_module_descriptor_v1),
.abi_version = ECHOION_MODULE_ABI_VERSION,
.component_id = Api::Module::Detail::stringView("EchoionWeb"),
.version_major = ECHOION_COMPONENT_VERSION_MAJOR,
.version_minor = ECHOION_COMPONENT_VERSION_MINOR,
.version_patch = ECHOION_COMPONENT_VERSION_PATCH,
.version_prerelease = {},
.abi_hash = Api::Module::Detail::stringView(
"sha256:"
"e1f826ddb06caadc394af7a3db27af32789a7e580cde6ee516edd0ebda2a5c"
"9f"),
.dependencies = dependencies,
.dependency_count = std::size(dependencies),
.activation_policy = ECHOION_ACTIVATION_RESTART_REQUIRED_V1,
.api = &Bridge::api,
.reserved = {},
};
return descriptor;
}
} // namespace Echoion::Web } // namespace Echoion::Web
extern "C" ECHOION_MODULE_EXPORT const Void::Plugin::ModuleDescriptor * extern "C" ECHOION_MODULE_EXPORT const Void::Plugin::ModuleDescriptor *
@@ -60,3 +107,9 @@ echoion_module_query()
{ {
return &Echoion::Web::webModuleDescriptor(); return &Echoion::Web::webModuleDescriptor();
} }
extern "C" ECHOION_MODULE_ABI_EXPORT const echoion_module_descriptor_v1 *
echoion_module_abi_query()
{
return &Echoion::Web::webModuleAbiDescriptor();
}