Files
noteguard/Cargo.toml
T
William Casarin 0e082512bd 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
2026-09-09 11:17:22 -07:00

38 lines
1.3 KiB
TOML

[package]
name = "noteguard"
version = "0.1.0"
edition = "2021"
[features]
forwarder = ["tokio-tungstenite", "tokio", "futures-util"]
# nscript: run user-submitted WASM note filters (Phase 0 spike)
nscript = ["wasmtime"]
# cloudflare: escalate BanTracker IP-bans to a Cloudflare WAF IP list (edge block)
cloudflare = ["ureq"]
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["raw_value"] }
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 }
futures-util = { version = "0.3.30", optional = true }
log = "0.4.22"
env_logger = "0.11.3"
# nscript deps
wasmtime = { version = "48", optional = true, default-features = false, features = ["cranelift", "runtime", "std"] }
# cloudflare deps — blocking HTTP client for the edge-ban worker
ureq = { version = "2", optional = true, features = ["json"] }