networkstate

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: AGPL-3.0 Imports: 10 Imported by: 1 module, 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 networkstate freezes address and egress facts for a connectivity generation.

Index

Constants

View Source
const DefaultDebounce = 500 * time.Millisecond
View Source
const DefaultPollInterval = time.Second
View Source
const ResumeGap = 10 * time.Second

Variables

This section is empty.

Functions

This section is empty.

Types

type Address

type Address struct {
	IP             netip.Addr
	InterfaceIndex int
	InterfaceName  string
	AdapterID      string
	Class          string
	Tentative      bool
	Deprecated     bool
	PreferredUntil time.Time
	ValidUntil     time.Time
}

type Monitor

type Monitor struct {
	// contains filtered or unexported fields
}

func NewMonitor

func NewMonitor(source Source, debounce time.Duration) *Monitor

func (*Monitor) Current

func (m *Monitor) Current() Snapshot

func (*Monitor) Poll

func (m *Monitor) Poll(ctx context.Context, now time.Time) (Snapshot, bool, error)

Poll detects resume by an unexpectedly long observation gap. Platform route and address facts are read together and never applied to an existing snapshot.

func (*Monitor) ResetPollGap added in v0.1.6

func (m *Monitor) ResetPollGap()

ResetPollGap clears the prior poll timestamp when a watcher pauses polling while idle so resuming later is not misclassified as an OS sleep.

type Observer

type Observer struct {
	Debounce time.Duration
	// contains filtered or unexported fields
}

func (*Observer) Observe

func (o *Observer) Observe(state State, now time.Time) (Snapshot, bool)

type Route

type Route struct {
	InterfaceIndex int
	Gateway        netip.Addr
	Family         int
	Metric         uint32
}

type Snapshot

type Snapshot struct {
	// contains filtered or unexported fields
}

func (Snapshot) Addresses

func (s Snapshot) Addresses() []Address

func (Snapshot) GenerationID

func (s Snapshot) GenerationID() uint64

func (Snapshot) LocalAddresses

func (s Snapshot) LocalAddresses() []netip.Addr

func (Snapshot) Routes

func (s Snapshot) Routes() []Route

type Source

type Source interface {
	Snapshot(context.Context) (State, error)
}

type State

type State struct {
	Addresses      []Address
	Routes         []Route
	ResumeSequence uint64
}

type SystemSource

type SystemSource struct{}

func (SystemSource) Snapshot

func (SystemSource) Snapshot(ctx context.Context) (State, error)

type Watcher added in v0.1.6

type Watcher struct {
	// contains filtered or unexported fields
}

Watcher owns one host-wide polling cadence and fans out immutable generation transitions to all subscribers. Polling runs only while at least one subscriber is active; Snapshot and Poll reuse recent observations so concurrent callers never multiply OS queries, while still surfacing expired snapshots and persistent background sampling failures.

func NewWatcher added in v0.1.6

func NewWatcher(config WatcherConfig) *Watcher

func (*Watcher) Close added in v0.1.6

func (w *Watcher) Close()

func (*Watcher) Current added in v0.1.6

func (w *Watcher) Current() Snapshot

func (*Watcher) Poll added in v0.1.6

func (w *Watcher) Poll(ctx context.Context, now time.Time) (Snapshot, bool, error)

Poll satisfies pull-based consumers without triggering redundant OS queries while the cached snapshot is fresh. Concurrent cache misses wait on either the in-flight sample or their own context cancellation; non-cancellation sampling errors and samples captured at or after the caller's timestamp are shared with that round's waiters, while caller cancellation stays isolated and older samples recheck snapshot freshness.

func (*Watcher) Snapshot added in v0.1.6

func (w *Watcher) Snapshot(ctx context.Context) (Snapshot, error)

func (*Watcher) Step added in v0.1.6

func (w *Watcher) Step(ctx context.Context) (Snapshot, bool, error)

Step forces one poll against the underlying monitor and broadcasts any true generation transition (after the initial baseline) to active subscribers.

func (*Watcher) Subscribe added in v0.1.6

func (w *Watcher) Subscribe() (<-chan Snapshot, func())

Subscribe registers for true network generation changes (never the initial 0 -> 1 baseline). The shared polling loop starts on the first subscriber and stops once the last subscriber cancels.

func (*Watcher) Watch added in v0.1.6

func (w *Watcher) Watch(ctx context.Context, onChange func(Snapshot))

Watch subscribes until ctx is canceled, invoking onChange for each subsequent network generation transition.

type WatcherConfig added in v0.1.6

type WatcherConfig struct {
	Source   Source
	Debounce time.Duration
	Interval time.Duration
	Now      func() time.Time
}

Jump to

Keyboard shortcuts

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