Documentation
¶
Overview ¶
Package netutil pkg/netutil/copy.go c0-com-util
Package netutil pkg/netutil/net.go c0-com-util
The TinyGo-safe (pure) helpers. Network-interface enumeration + the ipinfo.io HTTP probe live in net_native.go (//go:build !tinygo); their TinyGo stubs are in net_tinygo.go.
Package netutil pkg/netutil/net_linux.go c0-com-util
Package netutil pkg/netutil/net_native.go c0-com-util
Network-interface enumeration + the ipinfo.io HTTP probe. Split out of net.go behind //go:build !tinygo because TinyGo's net.Interface has no Addrs() method and net/http doesn't compile on the TinyGo wasm (js) target. A TinyGo dmsg client needs none of these (it has no host NICs to enumerate); the stubs in net_tinygo.go satisfy any transitive linker reference. See docs/design/tinygo-dmsg-client.md.
Package netutil pkg/netutil/porter.go c0-com-util
Package netutil pkg/netutil/retrier.go c0-com-util
Index ¶
- Constants
- Variables
- func CopyReadWriteCloser(conn1, conn2 io.ReadWriteCloser) error
- func DefaultNetworkInterface() (string, error)
- func DefaultNetworkInterfaceIPs() ([]net.IP, error)
- func ExtractPort(addr net.Addr) (uint16, error)
- func HasPublicIP() (bool, error)
- func IsPublicIP(IP net.IP) bool
- func IsVirtualInterface(name string) bool
- func LocalAddresses() ([]string, error)
- func LocalNetworkInterfaceIPs() ([]net.IP, error)
- func LocalProtocol() bool
- func NetworkInterfaceIPs(name string) ([]net.IP, error)
- type EphemeralDiagEntry
- type EphemeralDiagResult
- type Porter
- func (p *Porter) CloseAll(log logrus.FieldLogger)
- func (p *Porter) Count() int
- func (p *Porter) EphemeralDiag() EphemeralDiagResult
- func (p *Porter) PortValue(port uint16) (interface{}, bool)
- func (p *Porter) RangePortValues(fn func(port uint16, v interface{}) (next bool))
- func (p *Porter) RangePortValuesAndChildren(fn func(port uint16, v PorterValue) (next bool))
- func (p *Porter) Reserve(port uint16, v interface{}) (bool, func())
- func (p *Porter) ReserveChild(port, subPort uint16, v interface{}) (bool, func())
- func (p *Porter) ReserveEphemeral(ctx context.Context, v interface{}) (uint16, func(), error)
- func (p *Porter) ResetEphemeral() int
- func (p *Porter) SweepStaleEphemeral(maxAge time.Duration) int
- type PorterValue
- type Retrier
- type RetryFunc
Constants ¶
const ( DefaultInitBackoff = time.Second DefaultMaxBackoff = time.Second * 20 DefaultTries = int64(0) DefaultFactor = float64(1.3) )
Default values for retrier.
const ( // PorterMinEphemeral is the default minimum ephemeral port. PorterMinEphemeral = uint16(49152) )
Variables ¶
var ErrEphemeralPortSpace = errors.New("ephemeral port space exhausted")
ErrEphemeralPortSpace is returned when no ephemeral ports are available.
var (
ErrMaximumRetriesReached = errors.New("maximum retries attempted without success")
)
Package errors
Functions ¶
func CopyReadWriteCloser ¶
func CopyReadWriteCloser(conn1, conn2 io.ReadWriteCloser) error
CopyReadWriteCloser copies reads and writes between two connections. It returns when either direction encounters an error (including idle timeout).
func DefaultNetworkInterface ¶
DefaultNetworkInterface fetches default network interface name: the interface of each IPv4 default route, one a line, as `ip r` lists them.
Where the routing table cannot be read, as by an Android app, it is the interface the kernel routes a packet to the internet through, then the first usable interface. When none qualifies (airplane mode) it returns "" without error, which callers handle as unknown.
func DefaultNetworkInterfaceIPs ¶
DefaultNetworkInterfaceIPs returns IP addresses for the default network interface
func ExtractPort ¶
ExtractPort returns port of the given UDP or TCP address
func HasPublicIP ¶
HasPublicIP returns true if this machine has at least one publically available IP address
func IsPublicIP ¶
IsPublicIP returns true if the provided IP is public. Obtained from: https://stackoverflow.com/questions/41670155/get-public-ip-in-golang
func IsVirtualInterface ¶
IsVirtualInterface returns true for Docker bridges, veth pairs, and other virtual interfaces that shouldn't be registered with the address resolver.
func LocalAddresses ¶
LocalAddresses returns a list of all local addresses
func LocalNetworkInterfaceIPs ¶
LocalNetworkInterfaceIPs gets IPs of all local interfaces.
func LocalProtocol ¶
func LocalProtocol() bool
LocalProtocol check a condition to use dmsghttp or direct url
Types ¶
type EphemeralDiagEntry ¶
type EphemeralDiagEntry struct {
Port uint16 `json:"port"`
ValueType string `json:"value_type"` // e.g. "*dmsg.Stream", "<nil>"
ValueInfo string `json:"value_info,omitempty"`
Children int `json:"children,omitempty"`
}
EphemeralDiag returns diagnostic information about ephemeral port reservations. For each ephemeral port, it reports whether the stored value is nil, its type, and (for Stringer values) a short string representation. This helps identify what is holding ephemeral ports when the porter approaches exhaustion.
type EphemeralDiagResult ¶
type EphemeralDiagResult struct {
Total int `json:"total"`
Ephemeral int `json:"ephemeral"`
Listeners int `json:"listeners"`
NilValues int `json:"nil_values"`
TotalReserved uint64 `json:"total_reserved"` // monotonic counter
TotalFreed uint64 `json:"total_freed"` // monotonic counter
LeakRate string `json:"leak_rate"` // current - freed as percentage
TypeCount map[string]int `json:"type_count"`
AgeBucket map[string]int `json:"age_bucket"` // "0-30s", "30s-2m", "2m-5m", "5m-30m", "30m+"
CallerCount map[string]int `json:"caller_count,omitempty"` // caller file:line → count (for entries >2min)
Sample []EphemeralDiagEntry `json:"sample,omitempty"` // first 20 entries
}
type Porter ¶
Porter reserves ports.
func (*Porter) CloseAll ¶
func (p *Porter) CloseAll(log logrus.FieldLogger)
CloseAll closes all contained variables that implement io.Closer
func (*Porter) EphemeralDiag ¶
func (p *Porter) EphemeralDiag() EphemeralDiagResult
func (*Porter) RangePortValues ¶
RangePortValues ranges all ports that are currently reserved.
func (*Porter) RangePortValuesAndChildren ¶
func (p *Porter) RangePortValuesAndChildren(fn func(port uint16, v PorterValue) (next bool))
RangePortValuesAndChildren ranges port values and it's contained children.
func (*Porter) Reserve ¶
Reserve a given port. It returns a boolean informing whether the port is reserved, and a function to clear the reservation.
func (*Porter) ReserveChild ¶
ReserveChild reserves a child.
func (*Porter) ReserveEphemeral ¶
ReserveEphemeral reserves a new ephemeral port. It returns the reserved ephemeral port, a function to clear the reservation and an error (if any). Returns ErrEphemeralPortSpace if all ephemeral ports (minEph through 65535) are occupied.
func (*Porter) ResetEphemeral ¶
ResetEphemeral removes all ephemeral port reservations (ports >= minEph), freeing the port space. Well-known ports (listeners) are preserved. Use this to recover from ephemeral port exhaustion without restarting.
func (*Porter) SweepStaleEphemeral ¶
SweepStaleEphemeral closes and frees ephemeral port entries older than maxAge. It calls Close() on entries whose Value implements io.Closer, then deletes the porter entry. Returns the number of entries swept.
This is a safety net for the DMSG stream lifecycle: if the normal cleanup paths (Stream.Read auto-release, explicit Close, session reaping) miss an entry, the sweeper catches it before the 16K port space is exhausted.
type PorterValue ¶
type PorterValue struct {
Value interface{}
Children map[uint16]interface{}
}
PorterValue associates a port value alongside it's children.
type Retrier ¶
type Retrier struct {
// contains filtered or unexported fields
}
Retrier holds a configuration for how retries should be performed
func NewDefaultRetrier ¶
func NewDefaultRetrier(log logrus.FieldLogger) *Retrier
NewDefaultRetrier creates a retrier with default values.
func NewRetrier ¶
func NewRetrier(log logrus.FieldLogger, initBO, maxBO time.Duration, tries int64, factor float64) *Retrier
NewRetrier returns a retrier that is ready to call Do() method
func (*Retrier) Do ¶
Do takes a RetryFunc and attempts to execute it. If it fails with an error it will be retried a maximum of given times with an initBO until it returns nil or an error that is whitelisted
func (*Retrier) WithErrWhitelist ¶
WithErrWhitelist sets a list of errors into the retrier, if the RetryFunc provided to Do() fails with one of them it will return inmediatelly with such error. Calling this function is not thread-safe, and is advised to only use it when initializing the Retrier