Skip to main content

Module adaptive

Module adaptive 

Source
Expand description

Adaptive concurrency + throughput control (pure control logic).

This module is the brain and the gate of snapdir’s adaptive transfer tuning, but it deliberately performs no network or real I/O and reads no real clock or system samplers itself — every external signal (CPU, RSS, elapsed time, per-op outcomes) is injected. That keeps the controller fully deterministic and unit-testable; wiring it into the live transfer loops (and feeding it snapdir_core::resources samples + a real monotonic clock) is a later gate.

Three pieces:

  • AdaptiveGate — a resizable concurrency permit pool shared by both transfer backends. It exposes an async acquire (tokio semaphore) for the futures path and a zero-dependency blocking acquire_blocking (mutex + condvar) for the rayon path; set_limit retunes both live.
  • AdaptiveController — the control law (slow-start → AIMD, latency gradient, congestion backoff with cooldown, CPU/memory guardrails, periodic re-probing) that turns injected op samples + metrics into a Decision (next concurrency limit + target byte-rate).
  • Supporting value types: AdaptivePolicy, OpSample, OpResult, Decision.

Structs§

AdaptiveController
The adaptive control brain.
AdaptiveGate
A resizable concurrency permit pool shared by both transfer backends.
AdaptivePolicy
The controller’s tuning policy (its view of config; the TransferConfig integration is a later gate).
BlockingGatePermit
RAII guard for a blocking permit acquired from an AdaptiveGate. Releases the permit (and notifies one waiter) on drop.
ControllerDriver
Shared, live bridge between a pure AdaptiveController and the running transfer backends.
Decision
The controller’s output for one tick: the next concurrency limit and an optional target byte-rate.
GatePermit
RAII guard for an async permit acquired from an AdaptiveGate. Releases the permit on drop — unless the gate carries outstanding shrink debt, in which case this permit is forgotten to pay that debt down (so a shrink that happened while this permit was in flight takes effect exactly).
OpSample
One sampled transfer operation: bytes moved, observed latency, and outcome.

Enums§

OpResult
Outcome class of a single transfer operation, fed to AdaptiveController::record_op.

Functions§

p95_object_size
Computes the 95th-percentile of a set of object sizes (the memory-budget denominator the controller’s tick expects). An empty set yields 0 (memory guardrail disabled). The slice is cloned + sorted locally so the caller’s data is untouched.

Type Aliases§

MonoTime
Injected monotonic timestamp. The controller never reads a real clock; the caller passes a monotonically non-decreasing value (real code: Instant::now(); tests: a fake counter). Stored as nanoseconds since an arbitrary epoch so the type is trivially Copy and deterministic.