Add reusable strict JSON, rate limiting, and sealed secrets
This commit is contained in:
10 files changed
+778
-40
No files matched your search
@@ -7,6 +7,7 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
@@ -25,29 +26,57 @@ namespace Void::Resilience
|
||||
public:
|
||||
|
||||
using Clock = TokenBucket::Clock;
|
||||
using TimePoint = TokenBucket::TimePoint;
|
||||
|
||||
// `capacity` is the burst size; `refill_per_sec` the sustained rate.
|
||||
// The clock is shared with every bucket so tests stay deterministic.
|
||||
// `capacity` is the burst size; `refill_per_sec` the sustained
|
||||
// rate. `max_keys == 0` keeps the historic unbounded behaviour.
|
||||
// Otherwise new keys are rejected at capacity until cleanup/reset
|
||||
// frees space. The clock is shared with every bucket so tests stay
|
||||
// deterministic.
|
||||
explicit KeyedRateLimiter(double capacity, double refill_per_sec,
|
||||
Clock clock = default_clock())
|
||||
Clock clock = default_clock(),
|
||||
std::size_t max_keys = 0)
|
||||
: capacity_{capacity}, rate_{refill_per_sec},
|
||||
clock_{std::move(clock)}
|
||||
clock_{std::move(clock)}, max_keys_{max_keys}
|
||||
{
|
||||
if (max_keys_ > 0) {
|
||||
map_.reserve(max_keys_);
|
||||
}
|
||||
}
|
||||
|
||||
// Consume `tokens` for `key`. Returns false (without consuming) when
|
||||
// the key's bucket is exhausted.
|
||||
// Consume `tokens` for `key`. Returns false (without consuming)
|
||||
// when the key's bucket is exhausted.
|
||||
[[nodiscard]] bool allow(const Key& key, double tokens = 1.0)
|
||||
{
|
||||
return allow_at(key, clock_(), tokens);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool allow_at(const Key& key, TimePoint now,
|
||||
double tokens = 1.0)
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
return entry(key).bucket->try_acquire(tokens);
|
||||
Entry* value = entry(key, now);
|
||||
return value != nullptr &&
|
||||
value->bucket->try_acquire_at(now, tokens);
|
||||
}
|
||||
|
||||
// Tokens currently available to `key` (creates the bucket if new).
|
||||
[[nodiscard]] double remaining(const Key& key)
|
||||
{
|
||||
return remaining_at(key, clock_()).value_or(0.0);
|
||||
}
|
||||
|
||||
// A missing optional means the key could not be created because the
|
||||
// configured key cap is full.
|
||||
[[nodiscard]] std::optional<double> remaining_at(const Key& key,
|
||||
TimePoint now)
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
return entry(key).bucket->available();
|
||||
Entry* value = entry(key, now);
|
||||
if (value == nullptr) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return value->bucket->available_at(now);
|
||||
}
|
||||
|
||||
// Forget a key's bucket (its next use starts full).
|
||||
@@ -57,15 +86,27 @@ namespace Void::Resilience
|
||||
map_.erase(key);
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
map_.clear();
|
||||
}
|
||||
|
||||
// Drop buckets not touched within `max_age`. Returns how many were
|
||||
// removed. Call periodically to bound memory for churny key spaces.
|
||||
std::size_t cleanup(std::chrono::steady_clock::duration max_age)
|
||||
{
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
return cleanup_at(clock_(), max_age);
|
||||
}
|
||||
|
||||
std::size_t cleanup_at(TimePoint now,
|
||||
std::chrono::steady_clock::duration max_age)
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
std::size_t removed = 0;
|
||||
for (auto it = map_.begin(); it != map_.end();) {
|
||||
if (now - it->second.last_seen >= max_age) {
|
||||
if (now >= it->second.last_seen &&
|
||||
now - it->second.last_seen >= max_age) {
|
||||
it = map_.erase(it);
|
||||
++removed;
|
||||
} else {
|
||||
@@ -90,19 +131,20 @@ namespace Void::Resilience
|
||||
}
|
||||
|
||||
struct Entry {
|
||||
// TokenBucket owns a std::mutex (non-movable), so hold it by
|
||||
// pointer to keep Entry movable for the hash map.
|
||||
// TokenBucket owns a std::mutex (non-movable), so hold it
|
||||
// by pointer to keep Entry movable for the hash map.
|
||||
std::unique_ptr<TokenBucket> bucket;
|
||||
std::chrono::steady_clock::time_point last_seen;
|
||||
};
|
||||
|
||||
Entry& entry(const Key& key)
|
||||
Entry* entry(const Key& key, TimePoint now)
|
||||
{
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
auto it = map_.find(key);
|
||||
if (it == map_.end()) {
|
||||
it = map_
|
||||
.emplace(key,
|
||||
if (max_keys_ > 0 && map_.size() >= max_keys_) {
|
||||
return nullptr;
|
||||
}
|
||||
it = map_.emplace(key,
|
||||
Entry{std::make_unique<TokenBucket>(
|
||||
capacity_, rate_, clock_),
|
||||
now})
|
||||
@@ -110,12 +152,13 @@ namespace Void::Resilience
|
||||
} else {
|
||||
it->second.last_seen = now;
|
||||
}
|
||||
return it->second;
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
double capacity_;
|
||||
double rate_;
|
||||
Clock clock_;
|
||||
std::size_t max_keys_;
|
||||
mutable std::mutex mu_;
|
||||
std::unordered_map<Key, Entry, Hash> map_;
|
||||
};
|
||||
|
||||
@@ -37,7 +37,45 @@ namespace Void::Resilience
|
||||
[[nodiscard]] bool try_acquire(double tokens = 1.0)
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
refill();
|
||||
refill(clock_());
|
||||
return try_acquire_locked(tokens);
|
||||
}
|
||||
|
||||
// Explicit-time variant for callers that need a single
|
||||
// deterministic clock value across several coordinated buckets.
|
||||
[[nodiscard]] bool try_acquire_at(TimePoint now,
|
||||
double tokens = 1.0)
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
refill(now);
|
||||
return try_acquire_locked(tokens);
|
||||
}
|
||||
|
||||
// Current token count after refilling for elapsed time.
|
||||
[[nodiscard]] double available() { return available_at(clock_()); }
|
||||
|
||||
[[nodiscard]] double available_at(TimePoint now)
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
refill(now);
|
||||
return tokens_;
|
||||
}
|
||||
|
||||
void reset() { reset_at(clock_()); }
|
||||
|
||||
void reset_at(TimePoint now)
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
tokens_ = capacity_;
|
||||
last_ = now;
|
||||
}
|
||||
|
||||
[[nodiscard]] double capacity() const noexcept { return capacity_; }
|
||||
|
||||
private:
|
||||
|
||||
[[nodiscard]] bool try_acquire_locked(double tokens)
|
||||
{
|
||||
if (tokens_ >= tokens) {
|
||||
tokens_ -= tokens;
|
||||
return true;
|
||||
@@ -45,27 +83,15 @@ namespace Void::Resilience
|
||||
return false;
|
||||
}
|
||||
|
||||
// Current token count after refilling for elapsed time.
|
||||
[[nodiscard]] double available()
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
refill();
|
||||
return tokens_;
|
||||
}
|
||||
|
||||
[[nodiscard]] double capacity() const noexcept { return capacity_; }
|
||||
|
||||
private:
|
||||
|
||||
static Clock default_clock()
|
||||
{
|
||||
return [] { return std::chrono::steady_clock::now(); };
|
||||
}
|
||||
|
||||
void refill()
|
||||
void refill(TimePoint now)
|
||||
{
|
||||
const auto now = clock_();
|
||||
const std::chrono::duration<double> elapsed = now - last_;
|
||||
const std::chrono::duration<double> elapsed =
|
||||
now > last_ ? now - last_ : TimePoint::duration::zero();
|
||||
last_ = now;
|
||||
tokens_ =
|
||||
std::min(capacity_, tokens_ + elapsed.count() * rate_);
|
||||
|
||||
Reference in new issue
Block a user