net2

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2021 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

net2 is a collection of functions meant to supplement the capabilities provided by the standard "net" package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLocalIPs

func GetLocalIPs() ([]*net.IP, error)

This returns the list of local ip addresses which other hosts can connect to (NOTE: Loopback ip is ignored). Also resolves Hostname to an address and adds it to the list too, so IPs from /etc/hosts can work too.

func GetPort

func GetPort(addr net.Addr) (int, error)

Returns the port information.

func IsLocalhost

func IsLocalhost(host string) bool

Given a host string, return true if the host is an ip (v4/v6) localhost.

func IsLocalhostIp

func IsLocalhostIp(ipStr string) bool

func LookupValidAddrs

func LookupValidAddrs() (map[string]bool, error)

func MyHostname

func MyHostname() string

Like os.Hostname but caches first successful result, making it cheap to call it over and over. It will also crash whole process if fetching Hostname fails!

func MyIp4

func MyIp4() *net.IPAddr

Resolves `MyHostname()` to an Ip4 address. Caches first successful result, making it cheap to call it over and over. It will also crash whole process if resolving the IP fails!

func MyIp6

func MyIp6() *net.IPAddr

Resolves `MyHostname()` to an Ip6 address. Caches first successful result, making it cheap to call it over and over. It will also crash whole process if resolving the IP fails!

func ResolveIP4s

func ResolveIP4s(addrs []string) ([]string, error)

Resolves hostnames in addresses to actual IP4 addresses. Skips all invalid addresses and all addresses that can't be resolved. `addrs` are assumed to be of form: ["<hostname>:<port>", ...] Returns an error in addition to resolved addresses if not all resolutions succeed.

Types

type ConnectionOptions

type ConnectionOptions struct {
	// The maximum number of connections that can be active per host at any
	// given time (A non-positive value indicates the number of connections
	// is unbounded).
	MaxActiveConnections int32

	// The maximum number of idle connections per host that are kept alive by
	// the connection pool.
	MaxIdleConnections uint32

	// The maximum amount of time an idle connection can alive (if specified).
	MaxIdleTime *time.Duration

	// This limits the number of concurrent Dial calls (there's no limit when
	// DialMaxConcurrency is non-positive).
	DialMaxConcurrency int

	// Dial specifies the dial function for creating network connections.
	// If Dial is nil, net.DialTimeout is used, with timeout set to 1 second.
	Dial func(network string, address string) (net.Conn, error)

	// This specifies the now time function.  When the function is non-nil, the
	// connection pool will use the specified function instead of time.Now to
	// generate the current time.
	NowFunc func() time.Time

	// This specifies the timeout for any Read() operation.
	// Note that setting this to 0 (i.e. not setting it) will make
	// read operations block indefinitely.
	ReadTimeout time.Duration

	// This specifies the timeout for any Write() operation.
	// Note that setting this to 0 (i.e. not setting it) will make
	// write operations block indefinitely.
	WriteTimeout time.Duration
}

type ConnectionPool

type ConnectionPool interface {
	// This returns the number of active connections that are on loan.
	NumActive() int32

	// This returns the highest number of active connections for the entire
	// lifetime of the pool.
	ActiveHighWaterMark() int32

	// This returns the number of idle connections that are in the pool.
	NumIdle() int

	// This associates (network, address) to the connection pool; afterwhich,
	// the user can get connections to (network, address).
	Register(network string, address string) error

	// This dissociate (network, address) from the connection pool;
	// afterwhich, the user can no longer get connections to
	// (network, address).
	Unregister(network string, address string) error

	// This returns the list of registered (network, address) entries.
	ListRegistered() []NetworkAddress

	// This gets an active connection from the connection pool.  The connection
	// will remain active until one of the following is called:
	//  1. conn.ReleaseConnection()
	//  2. conn.DiscardConnection()
	//  3. pool.Release(conn)
	//  4. pool.Discard(conn)
	Get(network string, address string) (ManagedConn, error)

	// This releases an active connection back to the connection pool.
	Release(conn ManagedConn) error

	// This discards an active connection from the connection pool.
	Discard(conn ManagedConn) error

	// Enter the connection pool into lame duck mode.  The connection pool
	// will no longer return connections, and all idle connections are closed
	// immediately (including active connections that are released back to the
	// pool afterward).
	EnterLameDuckMode()
}

A generic interface for managed connection pool. All connection pool implementations must be threadsafe.

func NewMultiConnectionPool

func NewMultiConnectionPool(options ConnectionOptions) ConnectionPool

This returns a connection pool that manages multiple (network, address) entries. The connections to each (network, address) entry acts independently. For example ("tcp", "localhost:11211") could act as memcache shard 0 and ("tcp", "localhost:11212") could act as memcache shard 1.

func NewSimpleConnectionPool

func NewSimpleConnectionPool(options ConnectionOptions) ConnectionPool

This returns a connection pool where all connections are connected to the same (network, address)

type ManagedConn

type ManagedConn interface {
	net.Conn

	// This returns the original (network, address) entry used for creating
	// the connection.
	Key() NetworkAddress

	// This returns the underlying net.Conn implementation.
	RawConn() net.Conn

	// This returns the connection pool which owns this connection.
	Owner() ConnectionPool

	// This indictes a user is done with the connection and releases the
	// connection back to the connection pool.
	ReleaseConnection() error

	// This indicates the connection is an invalid state, and that the
	// connection should be discarded from the connection pool.
	DiscardConnection() error
}

A connection managed by a connection pool. NOTE: SetDeadline, SetReadDeadline and SetWriteDeadline are disabled for managed connections. (The deadlines are set by the connection pool).

func NewManagedConn

func NewManagedConn(
	network string,
	address string,
	handle resource_pool.ManagedHandle,
	pool ConnectionPool,
	options ConnectionOptions) ManagedConn

This creates a managed connection wrapper.

type NetworkAddress

type NetworkAddress struct {
	Network string
	Address string
}

Dial's arguments.

Directories

Path Synopsis
http2 is a collection of functions meant to supplement the capabilities provided by the standard "net/http" package.
http2 is a collection of functions meant to supplement the capabilities provided by the standard "net/http" package.
test_utils
Utility functions for testing net2/http2
Utility functions for testing net2/http2

Jump to

Keyboard shortcuts

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