Add reusable strict JSON, rate limiting, and sealed secrets
This commit is contained in:
10 files changed
+778
-40
No files matched your search
@@ -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