shaper

package
v0.141.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 25 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package shaper is agentlab's bandwidth shaper (docs/bandwidth.md): it lets the registry mirror's downloads take what the person's uplink leaves idle and give it back within seconds. One CAKE qdisc on the host side of the mirror's veth caps everything the mirror downloads from gsoci, and a controller sets its bandwidth every tick from the uplink's latency: while the latency to a few public reflectors stays at its baseline the mirror may take more, when it rises (the link's queue is filling, whoever fills it) the mirror yields.

Index

Constants

View Source
const (
	ModeAuto  = "auto"
	ModePause = "pause"
	ModeOff   = "off"
)

The modes a person sets for the machine (`agentlab shaper auto|max|pause| off`): one shaper serves every lab, so the override lives with it, in the mode file of its state directory, not in a lab's agentlab.yaml.

View Source
const (
	StateFile = "state.json"
	ModeFile  = "mode"
)

The shaper's state directory: the shaper publishes what it does there (StateFile), and `agentlab shaper auto|max|pause|off` leaves the person's mode there (ModeFile). On the host it is agentlab's state folder's `shaper/`, mounted into the shaper's container.

View Source
const StaleAfter = 10 * time.Second

StaleAfter is how old the published state may be before a reader calls the shaper gone: it publishes every second.

Variables

View Source
var DefaultReflectors = []netip.Addr{
	netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("9.9.9.9"),
	netip.MustParseAddr("8.8.8.8"), netip.MustParseAddr("208.67.222.222"),
}

DefaultReflectors are four public resolvers on separate networks.

View Source
var ErrProofFailed = errors.New("the namespace proof failed")

ErrProofFailed is a proof that ran and measured a criterion missed: the run said which.

Functions

func Cleanup

func Cleanup(pid int, logger *log.Logger) error

Cleanup takes CAKE off the veth of the mirror whose process is pid: what a shaper that was killed left behind.

func FetchBulk

func FetchBulk(ctx context.Context, w io.Writer, from string, streams int, start time.Time, duration time.Duration) error

FetchBulk opens streams connections to from at start, reads from them for the given duration and writes one Sample per second as a JSON line.

func FormatRate

func FormatRate(bps float64) string

FormatRate prints a rate for people; 0 means unlimited.

func ParseRate

func ParseRate(s string) (float64, error)

ParseRate reads a rate in bits per second: a number with an optional unit bit, kbit, mbit or gbit (k, m and g for short), case-insensitive. Byte units are refused on purpose: tc's "mbps" means megabytes.

func Proof

func Proof(ctx context.Context, opts ProofOptions) error

Proof runs the namespace proof: it starts this agentlab again as root of a new user namespace with network and mount namespaces of its own, which builds the topology and runs it there.

func ProofInNamespace

func ProofInNamespace(ctx context.Context, opts ProofOptions) error

ProofInNamespace is the proof inside its namespaces.

func RateArg

func RateArg(bps float64) string

RateArg prints a rate the way ParseRate reads it, in the largest unit that keeps it whole: 5mbit, 1500kbit.

func Run

func Run(ctx context.Context, cfg Config, logger *log.Logger) error

Run is the shaper: it finds the mirror's veth, puts CAKE on it and sets its bandwidth every tick until ctx ends, then takes CAKE off again — the veth back to what docker made it, the mirror unlimited.

func ServeBulk

func ServeBulk(ctx context.Context, listen string) error

ServeBulk accepts connections on listen and writes to each until it goes away or ctx ends.

func WriteMode

func WriteMode(dir string, m Mode) error

WriteMode leaves the person's mode for the shaper.

Types

type Config

type Config struct {
	// MirrorPID is a process in the mirror's network namespace: PID 1 of
	// the mirror's PID namespace, which the shaper's container joins.
	MirrorPID int
	// Reflectors are the IPv4 hosts the latency is measured against.
	Reflectors []netip.Addr
	// StateDir is where the shaper publishes its state and reads the
	// person's mode; "" publishes nothing.
	StateDir string
	// Interval is the control tick and the probe interval; Target the
	// queue above the baseline at which the mirror backs off.
	Interval, Target time.Duration
	// Min and Max bound auto's cap, Start is where it starts on a network,
	// Pause what `agentlab shaper pause` holds the mirror at; bits per
	// second.
	Min, Max, Start, Pause float64
}

Config is the shaper's configuration, from `agentlab shaper run`'s flags.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig is the shaper as agentlab runs it.

func (Config) Check

func (c Config) Check() error

Check refuses a configuration the controller cannot run.

type Mode

type Mode struct {
	// Kind is ModeAuto, ModePause or ModeOff.
	Kind string
	// Max is auto's cap below the shaper's own ceiling; 0 is none.
	Max float64
}

Mode is what the person asked for.

func ParseMode

func ParseMode(s string) (Mode, error)

ParseMode reads a mode: auto [<max>], pause or off; empty is auto.

func (Mode) String

func (m Mode) String() string

String is the mode as the mode file holds it: "auto", "auto 5mbit", "pause", "off".

type ProofOptions

type ProofOptions struct {
	// Binary is this agentlab: the shaper and the traffic run it.
	Binary string
	// Baseline also runs the topology without the shaper, for comparison.
	Baseline bool
	// Out is where the run reports.
	Out io.Writer
}

ProofOptions is one run of the proof.

type ReflectorState

type ReflectorState struct {
	Addr   string  `json:"addr"`
	BaseMs float64 `json:"baseMs"` // 0 = no baseline on this network yet
	LastMs float64 `json:"lastMs"` // 0 = no answer yet
	Lost   int     `json:"lost"`   // probes lost in a row
}

ReflectorState is one latency reflector.

type Sample

type Sample struct {
	T   int     `json:"t"`
	Bps float64 `json:"bps"`
}

Sample is one second of a fetch: the second since the proof's start and the bits per second received in it.

type State

type State struct {
	// Mode is the person's mode (auto, auto <max>, pause, off).
	Mode string `json:"mode"`
	// Device is the mirror's host-side veth the shaper works on; "" while it
	// finds none.
	Device string `json:"device,omitempty"`
	// Shaping is whether CAKE is in place on Device.
	Shaping bool `json:"shaping"`
	// CapBps is the mirror's cap in bits per second, 0 unlimited.
	CapBps float64 `json:"capBps"`
	// MirrorBps is what the mirror downloaded over the last second.
	MirrorBps float64 `json:"mirrorBps"`
	// DelayMs is the link's queue as the latency probes see it, over the
	// lowest baseline; DelayKnown false while no probe answered.
	DelayMs    float64 `json:"delayMs"`
	DelayKnown bool    `json:"delayKnown"`
	// TargetMs is the queue at which the mirror backs off.
	TargetMs float64 `json:"targetMs"`
	// Reason is why the cap is where it is.
	Reason string `json:"reason"`
	// Network is the host's network (the default route), whose change
	// starts the baselines over.
	Network    string           `json:"network"`
	Reflectors []ReflectorState `json:"reflectors"`
	// Problem is what keeps the shaper from shaping, "" when nothing does.
	Problem string    `json:"problem,omitempty"`
	Updated time.Time `json:"updated"`
}

State is what the shaper publishes.

func ReadState

func ReadState(dir string) (State, error)

ReadState reads the state the shaper published in dir.

func (State) Describe

func (st State) Describe() string

Describe is the state for people, `agentlab shaper status`.

func (State) Summary

func (st State) Summary() string

Summary is the state in one line, for `agentlab status`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL