readpolicy: shared-memory per-IP read-bandwidth buckets
strfry constructs its PluginReadPolicy as a per-thread local, spawning one `noteguard read` process per read-serving thread (reqWorker + reqMonitor). Each process kept its own in-memory ByteBucket, so an IP's reconnects — pinned to a thread by connId % numThreads — fanned across up to N processes, making the effective per-IP budget bytes_per_minute × N (~12x on the damus relay). The only exact-enforcement lever was reqWorker=1/reqMonitor=1, which kills read parallelism. Back read_bandwidth's per-IP byte buckets with a single mmap'd MAP_SHARED file (memmap2) shared by every read process on the host: a fixed-size open-addressing table keyed by client IP (repr(C): IP bytes + tokens f64-bits + last_refill), FNV-1a home slot + linear probe, one per-slot spinlock guarding each bucket, and a versioned header with a one-time CAS init handshake (first process creates + zeroes, others attach + validate). last_refill is CLOCK_MONOTONIC nanoseconds (std::time::Instant is process-relative and can't live in shared memory); a future timestamp (stale from a prior boot) resets the slot. Fails open — delivers — when a slot lock can't be taken, the table is full, the key is over-long, or the mmap/init fails, matching the ReadFilter contract. No eviction: the table sizes to max_ips and lives on tmpfs, so it survives strfry restarts and resets on reboot. New config keys shared_state_path + max_ips (unset path => a private per-process map, the old behavior). Scope is read_bandwidth only: max_events_per_conn is keyed by connId, already pinned to one process. Verified E2E against strfry (req-plugin-simplified, reqWorker=3 reqMonitor=3), one IP across 8 reconnects, budget 1000 B/min, ~413 B events: with shared_state_path set the aggregate delivered ONE budget (2 events) and every later reconnect got CLOSED "budget exhausted" from whichever of the 6 processes served it; with the path unset (per-process control) the same drive delivered 6 = 3 reqWorker processes × 2 — the sharding blowup, reproduced and then removed. Changelog-Added: Shared-memory per-IP read-bandwidth buckets for accurate cross-process enforcement without reducing read thread count. Headway: headway:noteguard/ethics-palm-remember Agentium: agentium:swim-brother-army
This commit is contained in:
Generated
+11
@@ -931,6 +931,15 @@ dependencies = [
|
||||
"rustix 1.1.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memmap2"
|
||||
version = "0.9.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d1219ed1b7f229ee7104d281dd01d6802fe28bb6e95d292942c4daacdeb798c0"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.7.4"
|
||||
@@ -985,7 +994,9 @@ dependencies = [
|
||||
"env_logger",
|
||||
"futures-util",
|
||||
"ipnetwork",
|
||||
"libc",
|
||||
"log",
|
||||
"memmap2",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
|
||||
@@ -17,6 +17,12 @@ toml = "0.5"
|
||||
ipnetwork = "0.20.0"
|
||||
unicode-security = "0.1.2"
|
||||
|
||||
# shared-memory per-IP read-bandwidth buckets: one mmap'd MAP_SHARED file shared
|
||||
# by every `noteguard read` plugin process on the host (memmap2), timestamped
|
||||
# against the system-wide CLOCK_MONOTONIC (libc clock_gettime).
|
||||
memmap2 = "0.9"
|
||||
libc = "0.2"
|
||||
|
||||
# forwarder deps
|
||||
tokio-tungstenite = { version = "0.23.1", optional = true, features = ["native-tls"] }
|
||||
tokio = { version = "1.38.0", features = ["macros", "time", "sync", "rt-multi-thread"], optional = true }
|
||||
|
||||
@@ -342,17 +342,28 @@ strfry builds, alongside `connId`/`authed`) under its `connId`. Against an older
|
||||
strfry that doesn't send `ip`, metering degrades gracefully to per-connection
|
||||
(logged once per connection as a warning) — a reconnect then gets a fresh budget.
|
||||
|
||||
**Per-thread scoping (important):** strfry runs *one plugin process per
|
||||
read-serving thread* (`relay.numThreads.reqWorker` / `reqMonitor`), each with its
|
||||
own in-memory byte buckets. A given connection is served by one thread, so the
|
||||
per-IP budget only survives a reconnect that lands on the *same* thread; the
|
||||
effective per-IP budget is therefore up to `numThreads ×
|
||||
read_bandwidth.bytes_per_minute`. This is the same per-process scoping the write-side
|
||||
`ratelimit` filter has (its buckets live in the per-ingester-thread process).
|
||||
For exact per-IP enforcement, set `relay.numThreads.reqWorker = 1` and
|
||||
`relay.numThreads.reqMonitor = 1`; otherwise treat the limit as approximate.
|
||||
(True cross-thread enforcement would require sharing bucket state across plugin
|
||||
processes, which this phase does not do.)
|
||||
**Per-thread scoping and shared memory:** strfry runs *one plugin process per
|
||||
read-serving thread* (`relay.numThreads.reqWorker` / `reqMonitor`), each a
|
||||
separate `noteguard read` process. A connection is pinned to one thread
|
||||
(`connId % numThreads`), so a naive per-process bucket only survives a reconnect
|
||||
that lands on the *same* thread — an IP's reconnects fan across up to `numThreads`
|
||||
processes, making the effective per-IP budget up to `numThreads ×
|
||||
read_bandwidth.bytes_per_minute`.
|
||||
|
||||
To enforce **one** authoritative budget per IP while keeping every read thread,
|
||||
set `read_bandwidth.shared_state_path` to a path on `tmpfs`
|
||||
(e.g. `/dev/shm/noteguard-read-bandwidth`): every `noteguard read` process on the
|
||||
host then `mmap`s the same file, a fixed open-addressing IP→bucket table guarded
|
||||
by per-slot spinlocks, so an IP's budget is a single shared bucket regardless of
|
||||
which process serves a given connection. The table sizes to `max_ips` distinct
|
||||
IPs (default 65536, no eviction — buckets must survive a reconnect) and *fails
|
||||
open* (delivers, unmetered) if it fills, if a slot lock can't be taken, or if the
|
||||
`mmap` can't be created. It lives on `tmpfs`, so it survives strfry restarts and
|
||||
resets on reboot. Leaving `shared_state_path` unset keeps the old per-process
|
||||
behavior (accurate only within a single process). Only `read_bandwidth` needs
|
||||
this; `max_events_per_conn` is keyed by `connId`, which is already pinned to one
|
||||
process, so its counter is authoritative as-is. (The write-side `ratelimit`
|
||||
filter has the same per-process scoping and is unaffected by this key.)
|
||||
|
||||
### NScript read filter (experimental)
|
||||
|
||||
|
||||
+9
-3
@@ -91,11 +91,17 @@ max = 5000
|
||||
|
||||
# Read-bandwidth throttle: cap the BYTES of events delivered per minute, keyed
|
||||
# per client IP so reconnecting with a fresh connId does not reset the budget.
|
||||
# The read-side analogue of `filters.ratelimit` above. strfry runs one plugin
|
||||
# process per read-serving thread, so the effective per-IP limit is up to
|
||||
# `relay.numThreads.reqWorker` times this value — see the README.
|
||||
# The read-side analogue of `filters.ratelimit` above.
|
||||
[readpolicy.filters.read_bandwidth]
|
||||
bytes_per_minute = 5000000
|
||||
# Shared-memory buckets: strfry runs one plugin process per read-serving thread,
|
||||
# so without this the effective per-IP limit is up to `relay.numThreads.reqWorker
|
||||
# + reqMonitor` times `bytes_per_minute`. Point every process at one tmpfs file
|
||||
# and the per-IP budget becomes a single authoritative bucket across all of them
|
||||
# (see the README). Unset => per-process buckets (accurate only within one
|
||||
# process). Fails open (delivers) if the file can't be mapped or the table fills.
|
||||
# shared_state_path = "/dev/shm/noteguard-read-bandwidth"
|
||||
# max_ips = 65536 # distinct IPs the shared table holds before it fails open
|
||||
# Exempt entries; each matches a connection's client IP OR its authed pubkey
|
||||
# hex. A whitelisted connection is never metered, so this filter asks for no
|
||||
# per-event work for it (the subscription is then passthrough unless another
|
||||
|
||||
+156
-80
@@ -1,36 +1,8 @@
|
||||
use crate::read_filter::{ConnContext, EventVerdict, OpenDecision, ReadFilter};
|
||||
use crate::readfilters::shm::SharedByteBuckets;
|
||||
use crate::readmsg::UnparsedNote;
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
/// An `Instant` token bucket of *bytes*, keyed per client IP. Mirrors the
|
||||
/// write-side `ratelimit.rs` mechanism (there tokens are posts; here they are
|
||||
/// bytes). Refills at `bytes_per_minute` bytes per minute up to that capacity,
|
||||
/// and — like `ratelimit.rs`'s `sources` map — is **never evicted**: surviving a
|
||||
/// reconnect is the entire point of keying on IP.
|
||||
struct ByteBucket {
|
||||
/// Bytes of budget currently available. Starts full (capacity).
|
||||
tokens: f64,
|
||||
/// When `tokens` was last refilled.
|
||||
last_refill: Instant,
|
||||
}
|
||||
|
||||
impl ByteBucket {
|
||||
/// Refill toward `cap` by the elapsed fraction of a minute (same shape as
|
||||
/// `ratelimit.rs`), clamped to one minute's worth and capped at `cap`.
|
||||
fn refill(&mut self, cap: f64) {
|
||||
let now = Instant::now();
|
||||
let mut elapsed = now.duration_since(self.last_refill);
|
||||
let min = Duration::from_secs(60);
|
||||
if elapsed > min {
|
||||
elapsed = min;
|
||||
}
|
||||
let refill = (elapsed.as_secs_f64() / 60.0) * cap;
|
||||
self.tokens = (self.tokens + refill).min(cap);
|
||||
self.last_refill = now;
|
||||
}
|
||||
}
|
||||
|
||||
/// Nominal event size (bytes) assumed at `reqOpen`, where the next event's real
|
||||
/// wire size is unknown. Used both to decide whether the IP's bucket can afford
|
||||
@@ -41,12 +13,45 @@ impl ByteBucket {
|
||||
/// always affords it and only a drained IP trips the reject.
|
||||
const NOMINAL_EVENT_SIZE: u64 = 512;
|
||||
|
||||
/// Default table capacity: distinct IPs before the table fills and fails open.
|
||||
/// ~65k slots × ~80 bytes ≈ 5 MB on tmpfs.
|
||||
fn default_max_ips() -> u32 {
|
||||
65536
|
||||
}
|
||||
|
||||
/// Where the per-IP byte buckets live for this process.
|
||||
#[derive(Default)]
|
||||
enum Store {
|
||||
/// Not yet built (built lazily on first `reqOpen`, since construction can't
|
||||
/// do I/O).
|
||||
#[default]
|
||||
Uninit,
|
||||
/// A shared path was configured but its `mmap`/init failed: this process
|
||||
/// meters nothing and fails open (delivers). Distinct from [`Store::Anon`],
|
||||
/// which is the intentional no-path mode.
|
||||
Failed,
|
||||
/// A working per-IP byte-bucket table (shared file, or a private anonymous
|
||||
/// map when no path was configured).
|
||||
Map(SharedByteBuckets),
|
||||
}
|
||||
|
||||
/// Read-bandwidth throttle: cap the *bytes* of events delivered per minute,
|
||||
/// keyed per client IP so reconnecting with a fresh `connId` from the same IP
|
||||
/// does not reset the budget. The read-side analogue of the write-side
|
||||
/// `ratelimit` filter, metering the exact wire byte size of each delivered event
|
||||
/// via a per-IP byte bucket.
|
||||
///
|
||||
/// ## Cross-process enforcement
|
||||
///
|
||||
/// strfry runs one `noteguard read` process per read-serving thread, each with
|
||||
/// its own memory, so an IP's reconnects fan across up to `numThreads`
|
||||
/// processes — making a naive per-process bucket enforce `bytes_per_minute × N`.
|
||||
/// To enforce ONE authoritative budget per IP while keeping N processes, the
|
||||
/// buckets live in a shared `mmap`'d table
|
||||
/// ([`SharedByteBuckets`](crate::readfilters::shm)) named by `shared_state_path`.
|
||||
/// With no path configured the map is per-process (the old behavior); a
|
||||
/// configured path that fails to attach fails open (delivers, unmetered).
|
||||
///
|
||||
/// Once an IP's bucket can't cover the next event, the rest of the batch is
|
||||
/// withheld ([`EventVerdict::Stop`]). A whitelisted connection is exempt
|
||||
/// entirely — and, since it then does no per-event work, is reported as
|
||||
@@ -61,10 +66,25 @@ pub struct ReadBandwidth {
|
||||
#[serde(default)]
|
||||
pub whitelist: Vec<String>,
|
||||
|
||||
/// Client IP (or `conn:<id>` fallback) -> its byte bucket. Never evicted, so
|
||||
/// a reconnect from the same IP shares the same budget.
|
||||
/// Path to the shared-memory file holding the per-IP buckets, shared by every
|
||||
/// `noteguard read` process on the host (all must agree on the path and
|
||||
/// `max_ips`). `None` (unset, or empty) => a private per-process map —
|
||||
/// accurate only within one process, i.e. the pre-shared-memory behavior. Set
|
||||
/// it to a `tmpfs` path such as `/dev/shm/noteguard-read-bandwidth` to enforce
|
||||
/// ONE budget per IP across all read processes. A configured but unattachable
|
||||
/// path fails open (delivers, unmetered).
|
||||
#[serde(default)]
|
||||
pub shared_state_path: Option<String>,
|
||||
|
||||
/// Table capacity: distinct IPs held before the table fills and new IPs fail
|
||||
/// open (there is no eviction — buckets must survive reconnect). Rounded up
|
||||
/// to a power of two.
|
||||
#[serde(default = "default_max_ips")]
|
||||
pub max_ips: u32,
|
||||
|
||||
/// The per-IP byte buckets for this process. Built lazily on first use.
|
||||
#[serde(skip)]
|
||||
buckets: HashMap<String, ByteBucket>,
|
||||
store: Store,
|
||||
|
||||
/// `connId` -> the bucket key to charge, resolved once at `reqOpen` (a
|
||||
/// connection's IP, whitelist status and authed pubkey are fixed for its
|
||||
@@ -105,6 +125,64 @@ impl ReadBandwidth {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Build the byte-bucket store on first use (construction can't do I/O). A
|
||||
/// configured `shared_state_path` opens a shared file; unset (or empty) uses
|
||||
/// a private per-process map; a configured path that fails to attach records
|
||||
/// [`Store::Failed`] so the filter fails open (delivers, unmetered).
|
||||
fn ensure_store(&mut self) {
|
||||
if !matches!(self.store, Store::Uninit) {
|
||||
return;
|
||||
}
|
||||
let path = match self.shared_state_path.as_deref() {
|
||||
Some("") | None => None,
|
||||
Some(p) => Some(p.to_string()),
|
||||
};
|
||||
self.store = match path {
|
||||
None => match SharedByteBuckets::anonymous(self.max_ips) {
|
||||
Ok(m) => {
|
||||
log::info!(
|
||||
"read_bandwidth: no shared_state_path; per-process byte buckets \
|
||||
({} slots) — cross-process enforcement is OFF",
|
||||
m.slot_count()
|
||||
);
|
||||
Store::Map(m)
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("read_bandwidth: could not allocate byte buckets ({e}); failing open");
|
||||
Store::Failed
|
||||
}
|
||||
},
|
||||
Some(path) => match SharedByteBuckets::open(&path, self.max_ips) {
|
||||
Ok(m) => {
|
||||
log::info!(
|
||||
"read_bandwidth: shared byte buckets at {} ({} slots)",
|
||||
path,
|
||||
m.slot_count()
|
||||
);
|
||||
Store::Map(m)
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!(
|
||||
"read_bandwidth: shared byte-bucket init failed at {} ({}); \
|
||||
FAILING OPEN (delivering unmetered)",
|
||||
path,
|
||||
e
|
||||
);
|
||||
Store::Failed
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/// The working byte-bucket table, or `None` if metering is unavailable
|
||||
/// (a failed shared init) — in which case the filter fails open.
|
||||
fn map(&self) -> Option<&SharedByteBuckets> {
|
||||
match &self.store {
|
||||
Store::Map(m) => Some(m),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ReadFilter for ReadBandwidth {
|
||||
@@ -113,9 +191,11 @@ impl ReadFilter for ReadBandwidth {
|
||||
}
|
||||
|
||||
fn req_open(&mut self, ctx: &ConnContext, _filter: Option<&serde_json::Value>) -> OpenDecision {
|
||||
// Resolve identity once per connection, so the per-event path is a
|
||||
// single `connId` lookup with no allocation, no whitelist rescan, and
|
||||
// (against an old strfry) one missing-IP warning rather than a flood.
|
||||
// Build the shared store on first use, then resolve identity once per
|
||||
// connection, so the per-event path is a single `connId` lookup with no
|
||||
// allocation, no whitelist rescan, and (against an old strfry) one
|
||||
// missing-IP warning rather than a flood.
|
||||
self.ensure_store();
|
||||
let key = self.resolve_key(ctx);
|
||||
let cap = self.bytes_per_minute as f64;
|
||||
|
||||
@@ -124,27 +204,27 @@ impl ReadFilter for ReadBandwidth {
|
||||
// gets a NIP-01 CLOSED + retry hint (real backpressure) instead of a
|
||||
// silent empty/truncated feed — the reconnect/churn abuse case. While
|
||||
// there is still budget we admit and meter per-event on `req` as before.
|
||||
//
|
||||
// A `None` from the map (fail open) or a failed store leaves this None,
|
||||
// so we admit.
|
||||
let mut reject_retry_ms: Option<u64> = None;
|
||||
if let Some(key) = &key {
|
||||
// Create the bucket full on first sight of this IP — but never
|
||||
// reset an existing one, which must survive a reconnect.
|
||||
let bucket = self.buckets.entry(key.clone()).or_insert(ByteBucket {
|
||||
tokens: cap,
|
||||
last_refill: Instant::now(),
|
||||
});
|
||||
bucket.refill(cap);
|
||||
// Nominal event size clamped to capacity: a fresh (full) bucket
|
||||
// always affords it, so only a drained IP trips the reject.
|
||||
let nominal = (NOMINAL_EVENT_SIZE as f64).min(cap);
|
||||
if bucket.tokens < nominal {
|
||||
// retry_ms: time to refill one nominal event at
|
||||
// bytes_per_minute. An estimate — the next event's real size is
|
||||
// unknown at reqOpen (documented on NOMINAL_EVENT_SIZE).
|
||||
reject_retry_ms = Some(((nominal / cap) * 60_000.0).ceil() as u64);
|
||||
if let (Some(key), Some(map)) = (&key, self.map()) {
|
||||
if let Some(tokens) = map.tokens_after_refill(key.as_bytes(), cap) {
|
||||
// Nominal event size clamped to capacity: a fresh (full) bucket
|
||||
// always affords it, so only a drained IP trips the reject.
|
||||
let nominal = (NOMINAL_EVENT_SIZE as f64).min(cap);
|
||||
if tokens < nominal {
|
||||
// retry_ms: time to refill one nominal event at
|
||||
// bytes_per_minute. An estimate — the next event's real size
|
||||
// is unknown at reqOpen (documented on NOMINAL_EVENT_SIZE).
|
||||
reject_retry_ms = Some(((nominal / cap) * 60_000.0).ceil() as u64);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let exempt = key.is_none();
|
||||
// Metering happens only for a keyed connection with a working map; a
|
||||
// whitelisted conn (no key) or a failed store does no per-event work.
|
||||
let meters = key.is_some() && self.map().is_some();
|
||||
self.conn_keys.insert(ctx.conn_id, key);
|
||||
|
||||
if let Some(retry_ms) = reject_retry_ms {
|
||||
@@ -157,13 +237,13 @@ impl ReadFilter for ReadBandwidth {
|
||||
);
|
||||
}
|
||||
|
||||
// A whitelisted connection is never metered, so it does no per-event
|
||||
// work and can take the passthrough fast path. Everyone else needs `req`
|
||||
// batches so we can meter bytes.
|
||||
if exempt {
|
||||
OpenDecision::passthrough()
|
||||
} else {
|
||||
// A connection we won't meter does no per-event work and can take the
|
||||
// passthrough fast path (whitelisted, or the store failed open).
|
||||
// Everyone else needs `req` batches so we can meter bytes.
|
||||
if meters {
|
||||
OpenDecision::needs_events()
|
||||
} else {
|
||||
OpenDecision::passthrough()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,40 +253,36 @@ impl ReadFilter for ReadBandwidth {
|
||||
// byte is charged in `on_delivered`.
|
||||
//
|
||||
// A missing entry means whitelisted, or a `req` for a subscription we
|
||||
// never saw open: deliver either way (fail open).
|
||||
// never saw open; a missing/failed map means fail open: deliver either
|
||||
// way.
|
||||
let cap = self.bytes_per_minute as f64;
|
||||
let Self {
|
||||
buckets, conn_keys, ..
|
||||
} = self;
|
||||
let Some(Some(key)) = conn_keys.get(&ctx.conn_id) else {
|
||||
let Some(Some(key)) = self.conn_keys.get(&ctx.conn_id) else {
|
||||
return EventVerdict::Deliver;
|
||||
};
|
||||
let Some(bucket) = buckets.get_mut(key) else {
|
||||
let Some(map) = self.map() else {
|
||||
return EventVerdict::Deliver;
|
||||
};
|
||||
|
||||
// Refill (elapsed/60 of a minute's bytes, capped at capacity) then test
|
||||
// affordability. Same refill used at reqOpen to peek for backpressure.
|
||||
bucket.refill(cap);
|
||||
|
||||
if bucket.tokens < ev.size() as f64 {
|
||||
EventVerdict::Stop
|
||||
} else {
|
||||
EventVerdict::Deliver
|
||||
// affordability. `None` (fail open) delivers.
|
||||
match map.tokens_after_refill(key.as_bytes(), cap) {
|
||||
Some(tokens) if tokens < ev.size() as f64 => EventVerdict::Stop,
|
||||
_ => EventVerdict::Deliver,
|
||||
}
|
||||
}
|
||||
|
||||
fn on_delivered(&mut self, ctx: &ConnContext, ev: &UnparsedNote) {
|
||||
// The bucket was resolved (and refilled) in `filter_event` for this same
|
||||
// event, so it exists; charge the delivered event's exact wire size.
|
||||
let Self {
|
||||
buckets, conn_keys, ..
|
||||
} = self;
|
||||
if let Some(Some(key)) = conn_keys.get(&ctx.conn_id) {
|
||||
if let Some(bucket) = buckets.get_mut(key) {
|
||||
bucket.tokens -= ev.size() as f64;
|
||||
}
|
||||
}
|
||||
// event; charge the delivered event's exact wire size. Best-effort: a
|
||||
// fail-open acquire just skips the charge.
|
||||
let cap = self.bytes_per_minute as f64;
|
||||
let Some(Some(key)) = self.conn_keys.get(&ctx.conn_id) else {
|
||||
return;
|
||||
};
|
||||
let Some(map) = self.map() else {
|
||||
return;
|
||||
};
|
||||
map.spend(key.as_bytes(), cap, ev.size() as f64);
|
||||
}
|
||||
|
||||
fn conn_close(&mut self, conn_id: u64) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
mod bandwidth;
|
||||
mod max_events;
|
||||
mod shm;
|
||||
|
||||
#[cfg(feature = "nscript")]
|
||||
mod nscript;
|
||||
|
||||
@@ -0,0 +1,639 @@
|
||||
//! Shared-memory per-IP byte buckets, backing [`ReadBandwidth`](super::ReadBandwidth).
|
||||
//!
|
||||
//! ## Why
|
||||
//!
|
||||
//! strfry constructs its `PluginReadPolicy` as a **per-thread local**, lazily
|
||||
//! spawning one `./noteguard read` child process per read-serving thread
|
||||
//! (`reqWorker` + `reqMonitor`). With N such threads an IP's connections — spread
|
||||
//! across processes by reconnects (dispatch is `connId % numThreads`) — hit N
|
||||
//! independent in-memory buckets, so the effective per-IP budget is
|
||||
//! `bytes_per_minute × N`. To enforce ONE authoritative budget per IP while
|
||||
//! keeping N parallel processes, all `noteguard read` processes on a host share a
|
||||
//! single [`ByteBucket`]-table through one `mmap`'d `MAP_SHARED` file.
|
||||
//!
|
||||
//! ## Layout (a `repr(C)` header + a fixed open-addressing slot table)
|
||||
//!
|
||||
//! ```text
|
||||
//! Header @0 : magic u64 | version u32 | slot_count u32 | init_state AtomicU32 | pad
|
||||
//! Slot[i] : lock AtomicU32 | state u32 | key_len u32 | pad | key [u8; KEY_LEN]
|
||||
//! | tokens u64 (f64 bits) | last_refill u64 (monotonic ns)
|
||||
//! ```
|
||||
//!
|
||||
//! The table is keyed by the client IP text (or a `conn:<id>` fallback), hashed
|
||||
//! with a **fixed** FNV-1a — never the std `RandomState`, whose per-process seed
|
||||
//! would send the same IP to different slots in different processes and defeat
|
||||
//! the whole point. Slots are found by linear probing from the hashed home slot.
|
||||
//!
|
||||
//! ## Concurrency
|
||||
//!
|
||||
//! Each slot carries its own spinlock (`AtomicU32`, `Acquire`/`Release`). All of
|
||||
//! a slot's body — `tokens`, `last_refill`, `key`, `state` — is read and written
|
||||
//! only while that lock is held; the lock's acquire/release fences publish those
|
||||
//! plain (non-atomic) accesses across processes, the standard shared-memory-IPC
|
||||
//! pattern (sound on one host + one architecture with coherent caches). Because
|
||||
//! the empty-slot check happens *while holding the slot's lock*, two processes
|
||||
//! inserting the same key serialize on the first-empty slot and cannot
|
||||
//! double-insert; and since entries are **never evicted**, probe chains never
|
||||
//! break, so a key's insertion slot is deterministic.
|
||||
//!
|
||||
//! ## Fail-open
|
||||
//!
|
||||
//! Matching the [`ReadFilter`](crate::ReadFilter) contract, every operation
|
||||
//! returns `None` (→ the caller delivers) rather than block or withhold when it
|
||||
//! cannot proceed: a slot lock it cannot acquire within a bound, a full table, an
|
||||
//! over-long key, or (at construction) a failed `mmap`/init. A throttle that
|
||||
//! breaks must never silence a subscriber's own authorized feed.
|
||||
//!
|
||||
//! ## Lifetime / staleness
|
||||
//!
|
||||
//! The default path lives on `tmpfs` (`/dev/shm`), so the table survives strfry
|
||||
//! *restarts* (the buckets, and `CLOCK_MONOTONIC`, persist across a plugin
|
||||
//! process dying and respawning — the "reconcile on restart" property, for free)
|
||||
//! but is cleared on *reboot*. As belt-and-suspenders for a non-tmpfs path
|
||||
//! carried across a reboot (where `CLOCK_MONOTONIC` resets and a stored
|
||||
//! `last_refill` would sit in the future), [`SlotGuard::refill`] treats a
|
||||
//! future timestamp as fully stale and resets the slot to a full bucket.
|
||||
|
||||
use memmap2::{MmapMut, MmapOptions};
|
||||
use std::fs::OpenOptions;
|
||||
use std::io;
|
||||
use std::ptr::{addr_of, addr_of_mut};
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
|
||||
/// File magic: `"NGRDBW"` + version bytes, little-endian. Bumped with `VERSION`
|
||||
/// when the on-disk layout changes so an attaching process rejects a stale file.
|
||||
const MAGIC: u64 = u64::from_le_bytes(*b"NGRDBW\x00\x01");
|
||||
/// On-disk layout version. Attaching to a file with a different version fails
|
||||
/// (→ fail open), rather than misinterpreting its bytes.
|
||||
const VERSION: u32 = 1;
|
||||
|
||||
/// Fixed key capacity: an IPv6 textual address is at most 45 bytes
|
||||
/// (`INET6_ADDRSTRLEN`-ish), and the `conn:<id>` fallback is at most ~25, so 48
|
||||
/// covers every key. A longer key fails open.
|
||||
const KEY_LEN: usize = 48;
|
||||
|
||||
// Slot `state`.
|
||||
const EMPTY: u32 = 0;
|
||||
const OCCUPIED: u32 = 1;
|
||||
|
||||
// Header `init_state` — the one-time init handshake.
|
||||
const UNINIT: u32 = 0;
|
||||
const INITIALIZING: u32 = 1;
|
||||
const READY: u32 = 2;
|
||||
|
||||
/// Bounded spins before a slot-lock acquire gives up and fails open. Critical
|
||||
/// sections are a few memory ops (nanoseconds), so this only ever trips on a
|
||||
/// process that died holding the lock — in which case failing open is correct.
|
||||
const SPIN_LIMIT: u32 = 100_000;
|
||||
/// Bounded spins waiting for another process to finish one-time init.
|
||||
const INIT_SPIN_LIMIT: u64 = 10_000_000;
|
||||
|
||||
/// One minute in nanoseconds — the refill window (and the clamp on elapsed time).
|
||||
const MINUTE_NS: u64 = 60 * 1_000_000_000;
|
||||
|
||||
/// Shared header. `repr(C)` so every process agrees on the layout.
|
||||
#[repr(C)]
|
||||
struct Header {
|
||||
magic: u64,
|
||||
version: u32,
|
||||
slot_count: u32,
|
||||
init_state: AtomicU32,
|
||||
_pad: u32,
|
||||
}
|
||||
|
||||
/// One bucket slot. `repr(C, align(8))` so `tokens`/`last_refill` are naturally
|
||||
/// aligned and the array packs deterministically across processes.
|
||||
#[repr(C, align(8))]
|
||||
struct Slot {
|
||||
/// Per-slot spinlock: 0 = free, 1 = held. First field, so its address is the
|
||||
/// slot's address.
|
||||
lock: AtomicU32,
|
||||
/// `EMPTY` or `OCCUPIED`. Written under `lock`.
|
||||
state: u32,
|
||||
/// Bytes of `key` in use. Written under `lock`.
|
||||
key_len: u32,
|
||||
_pad: u32,
|
||||
/// The client IP text (or `conn:<id>` fallback). Written once under `lock`,
|
||||
/// immutable thereafter (no eviction).
|
||||
key: [u8; KEY_LEN],
|
||||
/// Bucket balance in bytes, stored as `f64::to_bits`. Under `lock`.
|
||||
tokens: u64,
|
||||
/// Last refill, `CLOCK_MONOTONIC` nanoseconds. Under `lock`.
|
||||
last_refill: u64,
|
||||
}
|
||||
|
||||
const HEADER_SIZE: usize = std::mem::size_of::<Header>();
|
||||
const SLOT_SIZE: usize = std::mem::size_of::<Slot>();
|
||||
|
||||
/// System-wide monotonic time in nanoseconds. `CLOCK_MONOTONIC` is boot-relative
|
||||
/// on Linux and so comparable across every process on the host — unlike
|
||||
/// [`std::time::Instant`], which is process-relative and cannot live in shared
|
||||
/// memory.
|
||||
fn monotonic_ns() -> u64 {
|
||||
// SAFETY: `clock_gettime` fills the provided `timespec`; CLOCK_MONOTONIC is
|
||||
// always available on Linux.
|
||||
let mut ts = libc::timespec {
|
||||
tv_sec: 0,
|
||||
tv_nsec: 0,
|
||||
};
|
||||
unsafe {
|
||||
libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts);
|
||||
}
|
||||
(ts.tv_sec as u64)
|
||||
.wrapping_mul(1_000_000_000)
|
||||
.wrapping_add(ts.tv_nsec as u64)
|
||||
}
|
||||
|
||||
/// FNV-1a (64-bit). A *fixed* hash — deterministic across processes, unlike the
|
||||
/// std hasher's per-process-seeded `RandomState`, which would place the same IP
|
||||
/// in different slots in different processes and re-shard the buckets.
|
||||
fn fnv1a(bytes: &[u8]) -> u64 {
|
||||
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
|
||||
for &b in bytes {
|
||||
h ^= b as u64;
|
||||
h = h.wrapping_mul(0x0000_0100_0000_01b3);
|
||||
}
|
||||
h
|
||||
}
|
||||
|
||||
fn slot_count_for(max_ips: u32) -> usize {
|
||||
// Power of two so the home slot is a cheap mask. At least one slot.
|
||||
(max_ips.max(1) as usize).next_power_of_two()
|
||||
}
|
||||
|
||||
fn invalid(msg: &'static str) -> io::Error {
|
||||
io::Error::new(io::ErrorKind::InvalidData, msg)
|
||||
}
|
||||
|
||||
/// A held slot lock plus typed access to the slot body. Releases the lock on
|
||||
/// drop. All body access goes through this guard, so it only happens under the
|
||||
/// lock.
|
||||
struct SlotGuard {
|
||||
slot: *mut Slot,
|
||||
}
|
||||
|
||||
impl SlotGuard {
|
||||
/// Try to take a slot's lock within [`SPIN_LIMIT`]. `None` (→ fail open) if it
|
||||
/// stays held — only a crashed lock-holder makes that happen in practice.
|
||||
fn lock(slot: *mut Slot) -> Option<SlotGuard> {
|
||||
// The lock is the slot's first field, so the slot pointer is the lock's.
|
||||
let lock = unsafe { &*(slot as *const AtomicU32) };
|
||||
for _ in 0..SPIN_LIMIT {
|
||||
if lock
|
||||
.compare_exchange_weak(0, 1, Ordering::Acquire, Ordering::Relaxed)
|
||||
.is_ok()
|
||||
{
|
||||
return Some(SlotGuard { slot });
|
||||
}
|
||||
std::hint::spin_loop();
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn state(&self) -> u32 {
|
||||
unsafe { addr_of!((*self.slot).state).read() }
|
||||
}
|
||||
fn set_state(&self, v: u32) {
|
||||
unsafe { addr_of_mut!((*self.slot).state).write(v) }
|
||||
}
|
||||
fn tokens(&self) -> f64 {
|
||||
f64::from_bits(unsafe { addr_of!((*self.slot).tokens).read() })
|
||||
}
|
||||
fn set_tokens(&self, v: f64) {
|
||||
unsafe { addr_of_mut!((*self.slot).tokens).write(v.to_bits()) }
|
||||
}
|
||||
fn last_refill(&self) -> u64 {
|
||||
unsafe { addr_of!((*self.slot).last_refill).read() }
|
||||
}
|
||||
fn set_last_refill(&self, v: u64) {
|
||||
unsafe { addr_of_mut!((*self.slot).last_refill).write(v) }
|
||||
}
|
||||
|
||||
fn key_matches(&self, key: &[u8]) -> bool {
|
||||
unsafe {
|
||||
let len = addr_of!((*self.slot).key_len).read() as usize;
|
||||
if len != key.len() {
|
||||
return false;
|
||||
}
|
||||
let p = addr_of!((*self.slot).key) as *const u8;
|
||||
std::slice::from_raw_parts(p, len) == key
|
||||
}
|
||||
}
|
||||
|
||||
/// Claim an empty slot for `key` with a full bucket. Caller must hold the
|
||||
/// slot lock (i.e. be this guard) and have checked the slot is `EMPTY`.
|
||||
fn claim(&self, key: &[u8], cap: f64) {
|
||||
unsafe {
|
||||
let p = addr_of_mut!((*self.slot).key) as *mut u8;
|
||||
std::ptr::copy_nonoverlapping(key.as_ptr(), p, key.len());
|
||||
addr_of_mut!((*self.slot).key_len).write(key.len() as u32);
|
||||
}
|
||||
self.set_tokens(cap);
|
||||
self.set_last_refill(monotonic_ns());
|
||||
self.set_state(OCCUPIED);
|
||||
}
|
||||
|
||||
/// Refill toward `cap` by the elapsed fraction of a minute (same shape as the
|
||||
/// write-side `ratelimit`), clamped to one minute and capped at `cap`. A
|
||||
/// `last_refill` in the future (a stale entry carried across a reboot, where
|
||||
/// the monotonic clock reset) resets the slot to full.
|
||||
fn refill(&self, cap: f64) {
|
||||
let now = monotonic_ns();
|
||||
let last = self.last_refill();
|
||||
if last > now {
|
||||
// Stale from a prior boot: reset rather than freeze the bucket.
|
||||
self.set_tokens(cap);
|
||||
self.set_last_refill(now);
|
||||
return;
|
||||
}
|
||||
let elapsed = (now - last).min(MINUTE_NS);
|
||||
let refill = (elapsed as f64 / MINUTE_NS as f64) * cap;
|
||||
self.set_tokens((self.tokens() + refill).min(cap));
|
||||
self.set_last_refill(now);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for SlotGuard {
|
||||
fn drop(&mut self) {
|
||||
let lock = unsafe { &*(self.slot as *const AtomicU32) };
|
||||
lock.store(0, Ordering::Release);
|
||||
}
|
||||
}
|
||||
|
||||
/// A fixed-size open-addressing table of per-IP byte buckets over one `mmap`.
|
||||
///
|
||||
/// Cloned only conceptually — construct one per process ([`Self::open`] to share
|
||||
/// a file across processes, [`Self::anonymous`] for a private per-process map).
|
||||
pub struct SharedByteBuckets {
|
||||
// Keeps the mapping alive; the raw `base`/pointers below borrow from it. The
|
||||
// mapping's virtual address is stable across a move of this struct.
|
||||
_mmap: MmapMut,
|
||||
base: *mut u8,
|
||||
slot_count: usize,
|
||||
}
|
||||
|
||||
impl SharedByteBuckets {
|
||||
/// Attach to (creating + initializing if absent) the shared file at `path`,
|
||||
/// sized for `max_ips` distinct IPs (rounded up to a power of two). Every
|
||||
/// `noteguard read` process on the host passes the same path + `max_ips`.
|
||||
pub fn open(path: &str, max_ips: u32) -> io::Result<Self> {
|
||||
let slot_count = slot_count_for(max_ips);
|
||||
let total = HEADER_SIZE + slot_count * SLOT_SIZE;
|
||||
|
||||
let file = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.create(true)
|
||||
// Never truncate: attaching to an existing table must preserve it
|
||||
// (grow-only sizing below), which is how the buckets survive a restart.
|
||||
.truncate(false)
|
||||
.open(path)?;
|
||||
// Grow-only: the first process sizes a fresh (zero-length) file; a process
|
||||
// attaching to an existing one leaves it be (and validates via the header).
|
||||
if (file.metadata()?.len() as usize) < total {
|
||||
file.set_len(total as u64)?;
|
||||
}
|
||||
// SAFETY: the file is sized to at least `total`; the mapping is `MAP_SHARED`.
|
||||
let mmap = unsafe { MmapOptions::new().len(total).map_mut(&file)? };
|
||||
Self::from_mmap(mmap, slot_count)
|
||||
}
|
||||
|
||||
/// A private, per-process map (zero-filled anonymous mapping). Used when no
|
||||
/// shared path is configured, and in tests. Behaves exactly like the old
|
||||
/// per-process `HashMap`, through the same slot logic.
|
||||
pub fn anonymous(max_ips: u32) -> io::Result<Self> {
|
||||
let slot_count = slot_count_for(max_ips);
|
||||
let total = HEADER_SIZE + slot_count * SLOT_SIZE;
|
||||
let mmap = MmapMut::map_anon(total)?;
|
||||
Self::from_mmap(mmap, slot_count)
|
||||
}
|
||||
|
||||
fn from_mmap(mmap: MmapMut, slot_count: usize) -> io::Result<Self> {
|
||||
let base = mmap.as_ptr() as *mut u8;
|
||||
let this = SharedByteBuckets {
|
||||
_mmap: mmap,
|
||||
base,
|
||||
slot_count,
|
||||
};
|
||||
this.init_or_attach(slot_count)?;
|
||||
Ok(this)
|
||||
}
|
||||
|
||||
/// One-time init handshake: the first process to see `UNINIT` writes the
|
||||
/// header and flips to `READY`; concurrent creators spin until `READY`; a
|
||||
/// process attaching to an already-`READY` file validates magic/version and
|
||||
/// that its `max_ips` agrees (a mismatch fails, → fail open, rather than
|
||||
/// silently sharding against a different-sized table). A freshly created file
|
||||
/// (or anon mapping) is zero-filled, so its slots are already `EMPTY`.
|
||||
fn init_or_attach(&self, our_slot_count: usize) -> io::Result<()> {
|
||||
let hdr = self.base as *mut Header;
|
||||
let state = unsafe { &*addr_of!((*hdr).init_state) };
|
||||
let mut spins = 0u64;
|
||||
loop {
|
||||
match state.compare_exchange(
|
||||
UNINIT,
|
||||
INITIALIZING,
|
||||
Ordering::Acquire,
|
||||
Ordering::Acquire,
|
||||
) {
|
||||
Ok(_) => {
|
||||
unsafe {
|
||||
addr_of_mut!((*hdr).magic).write(MAGIC);
|
||||
addr_of_mut!((*hdr).version).write(VERSION);
|
||||
addr_of_mut!((*hdr).slot_count).write(our_slot_count as u32);
|
||||
}
|
||||
// Slots are already zeroed (fresh ftruncate / anon map) => EMPTY.
|
||||
state.store(READY, Ordering::Release);
|
||||
return Ok(());
|
||||
}
|
||||
Err(INITIALIZING) => {
|
||||
spins += 1;
|
||||
if spins > INIT_SPIN_LIMIT {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::TimedOut,
|
||||
"shared byte-bucket init stalled (crashed initializer?)",
|
||||
));
|
||||
}
|
||||
std::hint::spin_loop();
|
||||
}
|
||||
Err(READY) => {
|
||||
let (magic, version, sc) = unsafe {
|
||||
(
|
||||
addr_of!((*hdr).magic).read(),
|
||||
addr_of!((*hdr).version).read(),
|
||||
addr_of!((*hdr).slot_count).read(),
|
||||
)
|
||||
};
|
||||
if magic != MAGIC {
|
||||
return Err(invalid("shared byte-bucket file has wrong magic"));
|
||||
}
|
||||
if version != VERSION {
|
||||
return Err(invalid("shared byte-bucket file version mismatch"));
|
||||
}
|
||||
if sc as usize != our_slot_count {
|
||||
return Err(invalid(
|
||||
"shared byte-bucket max_ips disagrees with the existing file",
|
||||
));
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
Err(_) => return Err(invalid("shared byte-bucket init_state corrupt")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn slot_count(&self) -> usize {
|
||||
self.slot_count
|
||||
}
|
||||
|
||||
fn slot_ptr(&self, i: usize) -> *mut Slot {
|
||||
// Slots begin right after the header; `base` is page-aligned so the
|
||||
// 8-aligned header + slots stay naturally aligned.
|
||||
unsafe { (self.base.add(HEADER_SIZE) as *mut Slot).add(i) }
|
||||
}
|
||||
|
||||
/// Find `key`'s slot (or insert it, full), returning its held lock guard.
|
||||
/// `None` (→ fail open) if the key is over-long, a probed slot's lock cannot
|
||||
/// be taken, or the table is full.
|
||||
fn acquire(&self, key: &[u8], cap: f64) -> Option<SlotGuard> {
|
||||
if key.is_empty() || key.len() > KEY_LEN {
|
||||
return None;
|
||||
}
|
||||
let mask = self.slot_count - 1;
|
||||
let home = (fnv1a(key) as usize) & mask;
|
||||
for step in 0..self.slot_count {
|
||||
let idx = (home + step) & mask;
|
||||
let guard = SlotGuard::lock(self.slot_ptr(idx))?;
|
||||
match guard.state() {
|
||||
EMPTY => {
|
||||
// First empty in the probe chain: claim it. The empty-check
|
||||
// is under the lock, so a racing insert of the same key waits
|
||||
// here and then matches instead of double-inserting.
|
||||
guard.claim(key, cap);
|
||||
return Some(guard);
|
||||
}
|
||||
_ if guard.key_matches(key) => return Some(guard),
|
||||
// Occupied by a different key: release and probe on.
|
||||
_ => drop(guard),
|
||||
}
|
||||
}
|
||||
// Table full: no matching or empty slot within a full sweep.
|
||||
None
|
||||
}
|
||||
|
||||
/// Refill `key`'s bucket and return its current balance in bytes. Creates the
|
||||
/// bucket full on first sight. `None` → fail open.
|
||||
pub fn tokens_after_refill(&self, key: &[u8], cap: f64) -> Option<f64> {
|
||||
let guard = self.acquire(key, cap)?;
|
||||
guard.refill(cap);
|
||||
Some(guard.tokens())
|
||||
}
|
||||
|
||||
/// Refill `key`'s bucket and subtract `amount` bytes (may go transiently
|
||||
/// negative; it recovers on the next refill). Best-effort: a fail-open
|
||||
/// `acquire` just skips the charge.
|
||||
pub fn spend(&self, key: &[u8], cap: f64, amount: f64) {
|
||||
if let Some(guard) = self.acquire(key, cap) {
|
||||
guard.refill(cap);
|
||||
guard.set_tokens(guard.tokens() - amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::atomic::AtomicU32 as TestCtr;
|
||||
|
||||
/// A unique temp path with best-effort cleanup on drop.
|
||||
struct TmpFile(String);
|
||||
impl TmpFile {
|
||||
fn new(tag: &str) -> TmpFile {
|
||||
static CTR: TestCtr = TestCtr::new(0);
|
||||
let n = CTR.fetch_add(1, Ordering::Relaxed);
|
||||
let p = std::env::temp_dir()
|
||||
.join(format!("ng-shm-{}-{}-{}", tag, std::process::id(), n))
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
let _ = std::fs::remove_file(&p);
|
||||
TmpFile(p)
|
||||
}
|
||||
}
|
||||
impl Drop for TmpFile {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_file(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Find `n` distinct keys that all hash to the same home slot in a table of
|
||||
/// `slot_count`, to force probing/collisions deterministically.
|
||||
fn colliding_keys(slot_count: usize, n: usize) -> Vec<Vec<u8>> {
|
||||
let mask = slot_count - 1;
|
||||
let target = 0usize;
|
||||
let mut out = Vec::new();
|
||||
let mut i = 0u64;
|
||||
while out.len() < n {
|
||||
let k = format!("collide-{}", i).into_bytes();
|
||||
if (fnv1a(&k) as usize) & mask == target {
|
||||
out.push(k);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn anonymous_creates_full_bucket_and_spends() {
|
||||
let m = SharedByteBuckets::anonymous(16).unwrap();
|
||||
let cap = 1000.0;
|
||||
// First sight: a full bucket.
|
||||
assert_eq!(m.tokens_after_refill(b"1.2.3.4", cap), Some(cap));
|
||||
m.spend(b"1.2.3.4", cap, 600.0);
|
||||
let t = m.tokens_after_refill(b"1.2.3.4", cap).unwrap();
|
||||
assert!((t - 400.0).abs() < 1.0, "expected ~400, got {t}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spend_can_go_transiently_negative() {
|
||||
let m = SharedByteBuckets::anonymous(16).unwrap();
|
||||
let cap = 100.0;
|
||||
m.tokens_after_refill(b"9.9.9.9", cap);
|
||||
m.spend(b"9.9.9.9", cap, 250.0);
|
||||
let t = m.tokens_after_refill(b"9.9.9.9", cap).unwrap();
|
||||
assert!(t < 0.0, "overspend should leave a negative balance, got {t}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refill_recovers_over_elapsed_time() {
|
||||
let m = SharedByteBuckets::anonymous(16).unwrap();
|
||||
let cap = 6000.0;
|
||||
m.tokens_after_refill(b"5.5.5.5", cap);
|
||||
// Drain, then backdate last_refill by 30s: half a minute => half a cap.
|
||||
{
|
||||
let g = m.acquire(b"5.5.5.5", cap).unwrap();
|
||||
g.set_tokens(0.0);
|
||||
g.set_last_refill(monotonic_ns() - 30 * 1_000_000_000);
|
||||
}
|
||||
let t = m.tokens_after_refill(b"5.5.5.5", cap).unwrap();
|
||||
assert!(
|
||||
(t - cap / 2.0).abs() < cap * 0.02,
|
||||
"expected ~half cap ({}), got {t}",
|
||||
cap / 2.0
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refill_clamps_at_one_minute() {
|
||||
let m = SharedByteBuckets::anonymous(16).unwrap();
|
||||
let cap = 1000.0;
|
||||
m.tokens_after_refill(b"1.1.1.1", cap);
|
||||
{
|
||||
let g = m.acquire(b"1.1.1.1", cap).unwrap();
|
||||
g.set_tokens(0.0);
|
||||
// A long time ago: refill is clamped to one minute => full, not more.
|
||||
g.set_last_refill(monotonic_ns() - 600 * 1_000_000_000);
|
||||
}
|
||||
assert_eq!(m.tokens_after_refill(b"1.1.1.1", cap), Some(cap));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn future_timestamp_resets_slot() {
|
||||
let m = SharedByteBuckets::anonymous(16).unwrap();
|
||||
let cap = 1000.0;
|
||||
m.tokens_after_refill(b"2.2.2.2", cap);
|
||||
{
|
||||
let g = m.acquire(b"2.2.2.2", cap).unwrap();
|
||||
g.set_tokens(0.0);
|
||||
// Stale-from-prior-boot: a last_refill in the future resets to full.
|
||||
g.set_last_refill(monotonic_ns() + 100 * 1_000_000_000);
|
||||
}
|
||||
assert_eq!(m.tokens_after_refill(b"2.2.2.2", cap), Some(cap));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn probing_keeps_colliding_keys_distinct() {
|
||||
// A 4-slot table; three keys sharing a home slot must land in distinct
|
||||
// slots (linear probing) and keep independent balances.
|
||||
let m = SharedByteBuckets::anonymous(4).unwrap();
|
||||
assert_eq!(m.slot_count(), 4);
|
||||
let cap = 1000.0;
|
||||
let keys = colliding_keys(4, 3);
|
||||
for k in &keys {
|
||||
m.tokens_after_refill(k, cap);
|
||||
}
|
||||
m.spend(&keys[0], cap, 100.0);
|
||||
m.spend(&keys[1], cap, 200.0);
|
||||
// keys[2] untouched, keys[0]/[1] independently charged.
|
||||
assert!((m.tokens_after_refill(&keys[0], cap).unwrap() - 900.0).abs() < 1.0);
|
||||
assert!((m.tokens_after_refill(&keys[1], cap).unwrap() - 800.0).abs() < 1.0);
|
||||
assert!((m.tokens_after_refill(&keys[2], cap).unwrap() - 1000.0).abs() < 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_table_fails_open() {
|
||||
// Two slots, three distinct keys: the third has nowhere to go => None
|
||||
// (fail open), never clobbering an existing bucket.
|
||||
let m = SharedByteBuckets::anonymous(2).unwrap();
|
||||
assert_eq!(m.slot_count(), 2);
|
||||
let cap = 1000.0;
|
||||
assert!(m.tokens_after_refill(b"a", cap).is_some());
|
||||
assert!(m.tokens_after_refill(b"b", cap).is_some());
|
||||
assert_eq!(m.tokens_after_refill(b"c", cap), None);
|
||||
// A key already present still resolves even when the table is full.
|
||||
assert!(m.tokens_after_refill(b"a", cap).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlong_key_fails_open() {
|
||||
let m = SharedByteBuckets::anonymous(16).unwrap();
|
||||
let long = vec![b'x'; KEY_LEN + 1];
|
||||
assert_eq!(m.tokens_after_refill(&long, 1000.0), None);
|
||||
m.spend(&long, 1000.0, 10.0); // no-op, must not panic
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_views_over_one_file_share_state() {
|
||||
// The crux: two SharedByteBuckets over ONE file model two processes.
|
||||
// A spend through view A is visible through view B — the whole point.
|
||||
let tf = TmpFile::new("share");
|
||||
let a = SharedByteBuckets::open(&tf.0, 64).unwrap();
|
||||
let b = SharedByteBuckets::open(&tf.0, 64).unwrap(); // attaches to A's file
|
||||
let cap = 1000.0;
|
||||
a.tokens_after_refill(b"1.2.3.4", cap); // A creates a full bucket
|
||||
a.spend(b"1.2.3.4", cap, 700.0); // A: 300 left
|
||||
let seen_by_b = b.tokens_after_refill(b"1.2.3.4", cap).unwrap();
|
||||
assert!(
|
||||
(seen_by_b - 300.0).abs() < 1.0,
|
||||
"view B must see view A's spend; got {seen_by_b}"
|
||||
);
|
||||
// And a spend through B is visible through A.
|
||||
b.spend(b"1.2.3.4", cap, 100.0);
|
||||
let seen_by_a = a.tokens_after_refill(b"1.2.3.4", cap).unwrap();
|
||||
assert!((seen_by_a - 200.0).abs() < 1.0, "got {seen_by_a}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reopen_reuses_existing_buckets() {
|
||||
// Survive-restart: drop the map and reopen the same file; the bucket's
|
||||
// balance persists (as it would across a strfry restart on tmpfs).
|
||||
let tf = TmpFile::new("reopen");
|
||||
let cap = 1000.0;
|
||||
{
|
||||
let a = SharedByteBuckets::open(&tf.0, 64).unwrap();
|
||||
a.tokens_after_refill(b"7.7.7.7", cap);
|
||||
a.spend(b"7.7.7.7", cap, 400.0);
|
||||
}
|
||||
let b = SharedByteBuckets::open(&tf.0, 64).unwrap();
|
||||
let t = b.tokens_after_refill(b"7.7.7.7", cap).unwrap();
|
||||
assert!((t - 600.0).abs() < 1.0, "expected persisted ~600, got {t}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attach_with_mismatched_capacity_fails() {
|
||||
let tf = TmpFile::new("mismatch");
|
||||
let _a = SharedByteBuckets::open(&tf.0, 64).unwrap();
|
||||
// A second process configured for a different max_ips must not silently
|
||||
// shard against a different-sized table.
|
||||
assert!(SharedByteBuckets::open(&tf.0, 128).is_err());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user