Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
filetransfer-impl.hh File Reference
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <limits>
#include <optional>
#include <random>

Go to the source code of this file.

Classes

struct  nix::RetryDelayParams

Functions

constexpr uint32_t nix::clampedExponential (uint32_t base, uint32_t attempt, uint32_t ceil)
constexpr uint32_t nix::saturateMs (std::chrono::milliseconds d) noexcept
std::chrono::milliseconds nix::computeRetryDelayMs (const RetryDelayParams &p, std::mt19937 &rng)

Detailed Description

Implementation details of filetransfer.cc exposed for unit testing. Not part of the public libstore API.

Function Documentation

◆ clampedExponential()

uint32_t nix::clampedExponential ( uint32_t base,
uint32_t attempt,
uint32_t ceil )
constexpr

Clamped exponential growth: base * 2^(attempt-1), capped at ceil. Shift is clamped at 31 and the intermediate is widened to uint64_t so the shift cannot overflow uint32_t.

◆ computeRetryDelayMs()

std::chrono::milliseconds nix::computeRetryDelayMs ( const RetryDelayParams & p,
std::mt19937 & rng )

Compute the delay before the next retry attempt.

Uses exponential backoff with optional full jitter. When a server-provided Retry-After is present, jitter spreads above it so that concurrent clients don't all retry at the same instant: sleep = random(floor, floor + backoff) where floor = retryAfter (or 0) and backoff = min(ceilMs, base * 2^(attempt-1)). ceilMs caps the backoff growth, not the server-provided floor.

Parameters
rngrandom number generator (unused if p.jitter is false)

◆ saturateMs()

uint32_t nix::saturateMs ( std::chrono::milliseconds d)
constexprnoexcept

Saturating conversion: chrono duration → uint32_t milliseconds. Negative inputs clamp to 0; values > UINT32_MAX clamp to UINT32_MAX.