network

package
v1.6.1-rc1 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllAddresses

func AllAddresses() ([]net.IP, error)

func GetFreePort

func GetFreePort() (int, error)

GetFreePort returns an available port using the global port allocator. The returned port is guaranteed to not be reallocated by this package for the duration of the TTL (default 5 seconds).

func GetNetworkAddress

func GetNetworkAddress(requested AddressType, getAddresses AddressLister) ([]string, error)

GetNetworkAddress returns a list of network addresses of the requested type, sourcing the addresses from the provided AddressLister. It is expected that network.AddAddresses() will be the default address lister. The result is a list of strings representing the network addresses in the order they were returned by the AddressLister.

func IsPortOpen added in v1.3.0

func IsPortOpen(port int) bool

IsPortOpen checks if a specific port is available for use by attempting to create a TCP listener on that port. It returns true if the port is available, false otherwise. The caller should note that the port's availability may change immediately after this check returns.

Types

type AddressLister

type AddressLister func() ([]net.IP, error)

type AddressType

type AddressType int
const (
	PrivateAddress AddressType = iota
	PublicAddress
	LoopbackAddress
	LinkLocal
	Multicast
	Any
)

func AddressTypeFromString

func AddressTypeFromString(t string) (AddressType, bool)

func (AddressType) String

func (a AddressType) String() string

type PortAllocator added in v1.5.2

type PortAllocator struct {
	// contains filtered or unexported fields
}

PortAllocator manages thread-safe allocation of network ports with a time-based reservation system. Once a port is allocated, it won't be reallocated until after the TTL expires, helping prevent race conditions in concurrent port allocation scenarios.

func NewPortAllocator added in v1.5.2

func NewPortAllocator(ttl time.Duration, maxAttempts uint) *PortAllocator

NewPortAllocator creates a new PortAllocator instance. ttl determines how long a port remains reserved after allocation. maxAttempts limits how many times we'll try to find an unreserved port before giving up.

func (*PortAllocator) GetFreePort added in v1.5.2

func (pa *PortAllocator) GetFreePort() (int, error)

GetFreePort returns an available port and reserves it for the duration of the TTL. If a port is already reserved but its TTL has expired, it may be returned if it's still available on the system. Returns error if unable to find an available port after maxAttempts tries.

Jump to

Keyboard shortcuts

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