Documentation
¶
Overview ¶
Package netstate tracks OS-reported network availability for the client.
A State instance is owned by the platform integration (e.g. the Android or iOS bindings, fed from ConnectivityManager callbacks or NWPathMonitor) and is injected into the connection retry loops (management, signal, relay, peer guards and the top-level connect loop), which consult it to avoid burning CPU and battery on reconnect attempts while the device has no network at all (e.g. airplane mode), and to reset their backoff as soon as the network returns.
Consumers hold a *State that may be nil — every non-mobile platform leaves it unset. The read methods are safe on a nil receiver: they report online and never block, so consumers behave as if this package did not exist.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type State ¶
type State struct {
// contains filtered or unexported fields
}
State holds the OS-reported network availability. The zero value is not usable; create instances with New.
func New ¶
func New() *State
New creates a State that starts online. Platforms without network tracking pass a nil *State instead: the read methods treat nil as always online and never block, so consumers need no nil guards.
func (*State) Changed ¶
func (s *State) Changed() <-chan struct{}
Changed returns a channel closed on the next availability transition, for callers that already own a select loop and cannot block in Wait. Re-read it after every fire: each transition installs a fresh channel. On a nil receiver — no State injected — it returns nil, which blocks forever in a select, so the caller simply never observes a transition.
func (*State) IsOnline ¶
IsOnline reports whether the OS reports at least one usable network. On a nil receiver — no State injected — it reports online.
func (*State) Set ¶
Set records whether the OS reports any usable network. Transitions wake up all Wait callers immediately. Unlike the read methods, Set is not nil-safe: it is only for the platform owner that created the State with New.