Reuse VoidLibrary rate limiting and export stable C ABI
This commit is contained in:
7 files changed
+188
-107
No files matched your search
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Resilience/KeyedRateLimiter.hpp>
|
||||
#include <Void/Resilience/RateLimiter.hpp>
|
||||
#include <boost/asio/ip/address.hpp>
|
||||
|
||||
#include <chrono>
|
||||
@@ -10,7 +12,6 @@
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Echoion::Web::Security
|
||||
@@ -23,8 +24,8 @@ namespace Echoion::Web::Security
|
||||
|
||||
CidrBlock(asio::ip::address network, unsigned int prefixLength);
|
||||
|
||||
[[nodiscard]] bool contains(
|
||||
const asio::ip::address& address) const noexcept;
|
||||
[[nodiscard]] bool
|
||||
contains(const asio::ip::address &address) const noexcept;
|
||||
|
||||
private:
|
||||
|
||||
@@ -34,16 +35,17 @@ namespace Echoion::Web::Security
|
||||
|
||||
using ParsedCidrs = std::expected<std::vector<CidrBlock>, std::string>;
|
||||
|
||||
[[nodiscard]] ParsedCidrs parseTrustedProxyCidrs(
|
||||
std::span<const std::string> cidrs);
|
||||
[[nodiscard]] ParsedCidrs
|
||||
parseTrustedProxyCidrs(std::span<const std::string> cidrs);
|
||||
|
||||
[[nodiscard]] std::string effectiveClientAddress(
|
||||
const asio::ip::address& immediatePeer,
|
||||
std::optional<std::string_view> forwardedFor,
|
||||
std::span<const CidrBlock> trustedProxies);
|
||||
[[nodiscard]] std::string
|
||||
effectiveClientAddress(const asio::ip::address &immediatePeer,
|
||||
std::optional<std::string_view> forwardedFor,
|
||||
std::span<const CidrBlock> trustedProxies);
|
||||
|
||||
[[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
|
||||
{
|
||||
@@ -72,25 +74,12 @@ namespace Echoion::Web::Security
|
||||
|
||||
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);
|
||||
|
||||
Config config_;
|
||||
mutable std::mutex mutex_;
|
||||
TokenBucket global_;
|
||||
std::unordered_map<std::string, PeerBucket> peers_;
|
||||
Void::Resilience::TokenBucket global_;
|
||||
Void::Resilience::KeyedRateLimiter<std::string> peers_;
|
||||
TimePoint nextSweep_{};
|
||||
bool sweepInitialized_ = false;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user