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 ¶
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 ¶
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.
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.
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.