netstate

package
v0.77.1 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

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

func (s *State) IsOnline() bool

IsOnline reports whether the OS reports at least one usable network. On a nil receiver — no State injected — it reports online.

func (*State) Set

func (s *State) Set(online bool)

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.

func (*State) Wait

func (s *State) Wait(ctx context.Context) (bool, error)

Wait blocks while the network is offline. It reports whether it had to wait, so callers can reset their backoff after an outage. It returns early with the context error when ctx is done. On a nil receiver — no State injected — it returns immediately.

Jump to

Keyboard shortcuts

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