netutil

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: Apache-2.0 Imports: 9 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAccecptClosed     = &net.OpError{Op: "accept", Err: fmt.Errorf("use of closed network connection")}
	ErrNoEnoughListeners = errors.New("must supply at least two listeners")
)

Functions

This section is empty.

Types

type Addr

type Addr struct {
	*net.IPNet
}

Addr represents a network end point address.

func (Addr) IPAddr

func (addr Addr) IPAddr() string

IPAddr returns the string form of the IP address ip. It returns one of 4 forms:

  • "<nil>", if ip has length 0
  • dotted decimal ("192.0.2.1"), if ip is an IPv4 or IP4-mapped IPv6 address
  • IPv6 ("2001:db8::1"), if ip is a valid IPv6 address
  • the hexadecimal form of ip, without punctuation, if no other cases appl

func (Addr) IsIPv4

func (addr Addr) IsIPv4() bool

IsIPv4 returns true if the ip is not an IPv4 address

func (Addr) IsIPv6

func (addr Addr) IsIPv6() bool

IsIPv6 returns true if the ip is not an IPv6 address

func (Addr) IsLoopback

func (addr Addr) IsLoopback() bool

IsLoopback reports whether ip is a loopback address.

func (Addr) MaskSize

func (addr Addr) MaskSize() int

MaskSize returns the number of leading ones

type AddrList

type AddrList []net.Addr

func (AddrList) Len

func (s AddrList) Len() int

func (AddrList) Less

func (s AddrList) Less(i, j int) bool

func (AddrList) Swap

func (s AddrList) Swap(i, j int)

type AddrSlice

type AddrSlice []Addr

AddrSlice reprecents a list of ip addresses

func (AddrSlice) Contains

func (addrs AddrSlice) Contains(ip string) bool

Contains checks if the ip is in the collection

type AggregatedListener

type AggregatedListener interface {
	net.Listener

	// Addrs returns all listeners' network addresses.
	Addrs() []net.Addr

	// TCPAddrs returns all tcp listeners' network addresses.
	TCPAddrs() []*net.TCPAddr

	// UnixAddrs returns all unix listeners' network addresses.
	UnixAddrs() []*net.UnixAddr

	// AcceptTCP accepts the next tcp incoming call and returns the new
	// tcp connection.
	AcceptTCP() (*net.TCPConn, error)

	// AcceptUnix accepts the next unix incoming call and returns the new
	// unix connection.
	AcceptUnix() (*net.UnixConn, error)
}

AggregatedListener is a listener aggregated by other listeners in order to satisfy net.Listener interface.

It makes http server can accept connections from several listeners in the meanwhile.

func NewAggregatedListener

func NewAggregatedListener(listeners ...net.Listener) (AggregatedListener, error)

NewAggregatedListener aggregate all input listeners into one to satisfy net.Listener interface.

Must supply at least two listeners.

It takes the first listener as major to expose the address and accepts all listeners on background to get network connections.

type BalancedDialer

type BalancedDialer interface {
	// DialContext connects to the address on the named network with
	// client side load balance.
	//
	// Balanceable networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
	// "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), other networks will not be
	// balanced.
	DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

BalancedDialer

func NewBalancedDialer

func NewBalancedDialer(opt Options) BalancedDialer

type Balancer

type Balancer interface {
	Balance(ctx context.Context, addrList []net.Addr) []net.Addr
}

Balancer resort the addresses coming from resolver and put the first priority address to the head

type BalancerBuilder

type BalancerBuilder interface {
	Build(host string, addrList []net.Addr) Balancer
}

BalancerBuilder build a new balancer instance

type Interface

type Interface struct {
	net.Interface
}

Interface represents the local network interface

func InterfaceByName

func InterfaceByName(name string) (*Interface, error)

InterfaceByName returns the local network interface specified by name.

func (*Interface) Addrs

func (dev *Interface) Addrs() (AddrSlice, error)

Addrs returns a list of unicast interface addresses for a specific interface.

func (*Interface) IsLoopback

func (dev *Interface) IsLoopback() bool

IsLoopback returns true if the net interface is lookback

type InterfaceSlice

type InterfaceSlice []Interface

InterfaceSlice reprecents a list of net interfaces

func Interfaces

func Interfaces() (InterfaceSlice, error)

Interfaces returns a slice containing the local network interfaces

func InterfacesByIP

func InterfacesByIP(ip string) (InterfaceSlice, error)

InterfacesByIP returns the local network interfaces that is using the specified IP address.

func InterfacesByLoopback

func InterfacesByLoopback() (InterfaceSlice, error)

InterfacesByLoopback returns a list of loopback network interfaces.

func (InterfaceSlice) Contains

func (ifaces InterfaceSlice) Contains(name string) bool

Contains checks if the net interface is in the collection

func (InterfaceSlice) Filter

func (ifaces InterfaceSlice) Filter(filterFunc func(iface Interface) bool) InterfaceSlice

Filter filters some interfaces by filsterFunc if it returns true

func (InterfaceSlice) Get

func (ifaces InterfaceSlice) Get(name string) *Interface

Get returns net interface device if it exists in the collection

func (InterfaceSlice) One

func (ifaces InterfaceSlice) One() *Interface

One returns the first net interface in the list It returns nil if there is no element in the slice

type Options

type Options struct {
	// BalancerBuilder build a client side load balancer
	BalancerBuilder BalancerBuilder
	// custom resolver, If not set, net.DefaultResolver will be used
	Resolver Resolver
	// contains filtered or unexported fields
}

type Resolver

type Resolver interface {
	LookupIPAddr(ctx context.Context, host string) (addrs []net.IPAddr, err error)
	LookupPort(ctx context.Context, network, service string) (port int, err error)
}

A Resolver looks up names and numbers. It is an interface that represents net.Resolver

type TCPListener

type TCPListener interface {
	net.Listener
	// AcceptTCP accepts the next tcp incoming call and returns the new
	// tcp connection.
	AcceptTCP() (*net.TCPConn, error)
}

TCPListener represent a tcp listener

type UnixListener

type UnixListener interface {
	net.Listener

	// AcceptUnix accepts the next unix incoming call and returns the new
	// unix connection.
	AcceptUnix() (*net.UnixConn, error)
}

UnixListener represent a unix listener

Jump to

Keyboard shortcuts

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