netwatch

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: AGPL-3.0 Imports: 5 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 netwatch notices when the network under this computer's connections may have changed: an interface or an address came or went (another Wi-Fi network, a VPN turned on or off, a cable plugged in), or the computer woke from sleep. A connection on the old path then often hangs until a long TCP timeout; told of the change, the app can check its connections at once.

It polls, which works the same on every system: the addresses of the interfaces that are up, every few seconds, and the wall clock, which jumps by much more than the poll interval across a sleep. A burst of changes (an interface going down, then up with a new address) is told once it has settled.

Index

Constants

View Source
const (
	// DefaultEvery is how often the network is read.
	DefaultEvery = 3 * time.Second
	// DefaultSettle is how long the network must stay the same after a
	// change before the change is told.
	DefaultSettle = 2 * time.Second
	// DefaultMaxWait is the longest a burst of changes delays the news.
	DefaultMaxWait = 15 * time.Second
	// DefaultGap is how far the wall clock may move between two reads
	// before the computer is taken to have slept.
	DefaultGap = 20 * time.Second
)

Defaults of a Watcher's zero fields.

Variables

This section is empty.

Functions

func Addresses

func Addresses() (string, error)

Addresses is the state of this computer's network: the addresses of its interfaces that are up and running, by interface, in order. The loopback and link-local addresses, which come and go with interfaces no connection to a server uses (and on some systems flap all the time), are left out.

func Watch

func Watch(ctx context.Context, changed func())

Watch calls changed, on its own goroutine, each time the network changes, until ctx ends. It blocks.

Types

type Detector

type Detector struct {
	Settle, MaxWait, Gap time.Duration
	// contains filtered or unexported fields
}

Detector tells, from reads of the network's state taken one after the other, when it changed: the state differs from the last one read, or the wall clock jumped between two reads by more than Gap. A change is told once nothing else changed for Settle, or MaxWait after the first change of a burst, whichever comes first. Its zero value is ready, with the defaults; it is not safe for concurrent use.

func (*Detector) Observe

func (d *Detector) Observe(now time.Time, state string, ok bool) bool

Observe takes a read at now: state is the network's, unless ok is false (it could not be read; the clock still counts). It reports whether a change is to be told now.

func (*Detector) Pending

func (d *Detector) Pending() bool

Pending reports a change seen and not told yet.

type Watcher

type Watcher struct {
	// Every is how often the network is read; 0 is DefaultEvery.
	Every time.Duration
	// State reads the network's state; nil is Addresses.
	State func() (string, error)
	// Now is the clock; nil is time.Now.
	Now func() time.Time
	// Detector decides when a change is told.
	Detector Detector
}

Watcher reads the network every Every and calls changed as its Detector says.

func (*Watcher) Run

func (w *Watcher) Run(ctx context.Context, changed func())

Run is Watch with w's settings.

Jump to

Keyboard shortcuts

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