Expand description
Bounded parallelism budget (--max-concurrency, Semaphore, Rayon, join_bounded).
Bounded parallelism and concurrency for the one-shot CLI.
§Workload classification (rules_rust_paralelismo)
| Class | Paths | Tool |
|---|---|---|
| I/O-bound | HTTP scrape/crawl/batch, CDP fan-out, robots | Tokio + Arc<Semaphore> / JoinSet / [join_bounded] |
| CPU-bound | Structural scan (sg), multi-file text match | Rayon par_iter |
| Mista | Browser session (CDP I/O + light DOM parse) | Multi-thread Tokio; no Rayon on async workers |
| Subprocess | Chrome residual | Existing residual kill path (no unbounded fork) |
§Formula (permits)
auto = min(
available_parallelism(),
max(1, (free_ram_mb * 50%) / ram_per_task_mb),
HARD_CAP
)
effective = override_if_set_else(auto)- ram_per_task_mb: 64 for HTTP I/O tasks (conservative floor).
Measure with
/usr/bin/time -v→ “Maximum resident set size” viascripts/rss-baseline.sh(doctor offline / single scrape). Revalidate when reqwest/scraper/chromiumoxide jump materially. - 50% safety margin on free RAM so concurrent tasks cannot OOM the host.
- HARD_CAP prevents FD / Chrome overwhelm on large hosts.
- Override: global CLI
--max-concurrency=N(0= auto). - One-shot: auto budget is cached once (
OnceLock); long daemon rebalance is N/A (BORN→EXECUTE→FINALIZE→DIE). Crawl/batch respect cancel token mid-run.
§Product law
One-shot CLI (BORN→EXECUTE→FINALIZE→DIE). No daemon fleets, no remote
metrics of available_permits, no systemd-run default (ops N/A).
Every fan-out must use [effective_limit] or an explicit local clamp
derived from it. Gate of record: Arc<Semaphore> + acquire_owned.
Enums§
- Workload
Class - Workload class for documentation and call-site comments.
Constants§
- CPU_
MAP_ THRESHOLD - Below this length,
map_cpustays sequential (rule: never parallelize when cost ≪ coordination overhead). Measured trade-off for one-shot CLI filters. - HARD_
CAP - Hard ceiling for any fan-out (FD budget + Chrome CDP politeness).
- MIN_
CONCURRENCY - Floor when auto-detection fails.
- RAM_
PER_ IO_ TASK_ MB - Conservative RSS budget per concurrent HTTP/CDP task (MiB).
Functions§
- browser_
max_ blocking_ threads - Cap for Tokio
max_blocking_threadson the browser runtime. - browser_
worker_ threads - Browser Tokio worker threads: enough for CDP event fan-out, capped.
- budget_
report - Snapshot of budget for doctor /
--jsondiagnostics. - command_
workload_ matrix - Per-command parallelism posture (agent discovery / doctor).
- compute_
auto_ budget - Auto budget:
min(cpus, ram_budget, HARD_CAP). - cpu_
count - CPU count used in the formula (
available_parallelism, min 1). - create_
dir_ all_ blocking create_dir_allon the blocking pool.- effective_
limit - Effective concurrency for I/O fan-out (and Rayon thread hint).
- effective_
limit_ capped - Same as
effective_limitbut capped for a specific subsystem. - filter_
cpu - Parallel filter with the same threshold rule as
map_cpu(PAR-84). - free_
ram_ mb - Free / available RAM in MiB when the platform exposes it.
- install_
limit - Install CLI override. Call once after clap parse.
- install_
rayon_ pool_ once - Run a CPU-bound closure on a Rayon pool sized to the process budget.
- io_
semaphore Arc<Semaphore>gate witheffective_limitpermits.- join_
bounded - Run a list of futures with bounded concurrency via
Arc<Semaphore>. - join_
bounded_ ordered - Like
join_boundedbut preserves input order via indexed futures. - map_cpu
- CPU map over a slice: sequential when
items.len() < CPU_MAP_THRESHOLD, else Rayon underinstall_rayon_pool_once(pool sized to budget). - map_
cpu_ owned - Like
map_cpubut for owned items that are consumed (into_par_iter). - rayon_
threads - Suggested Rayon thread count (respects
RAYON_NUM_THREADSvia Rayon itself when using the global pool; this value is for explicitThreadPoolBuilder). - read_
bytes_ blocking - Read a file fully on the blocking pool.
- read_
to_ string_ blocking - Read a UTF-8 file on the blocking pool (PAR-77: never
fs::read_to_stringon async worker). - rename_
blocking renameon the blocking pool (PAR-80: state rotation must not pin async workers).- resolve_
permits - Resolve permits for a fan-out:
0→ process budget; else clamp to hard cap. - semaphore_
with - Semaphore with an explicit permit count (already clamped by caller).
- sort_
by_ cpu - In-place sort with comparator; Rayon when large (PAR-94).
- sort_
by_ key_ cpu - In-place sort by key with the same threshold as
sort_cpu(PAR-94). - sort_
cpu - In-place sort with Rayon when
items.len() >= CPU_MAP_THRESHOLD(PAR-94). - walk_
threads - Walk / Rayon thread hint: budget capped by CPUs (respects
--max-concurrency). - write_
bytes_ blocking - Write bytes on the Tokio blocking pool (docsrs: never pin async workers with
std::fsfor non-trivial payloads). - write_
bytes_ sync - Sync write helper for outer CLI dispatch (no active Tokio worker). Prefer
write_bytes_blockingwhen insideasync fn/block_on_*.