netutil

package
v1.130.0-cluster Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2025 License: Apache-2.0 Imports: 21 Imported by: 33

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Dialer = &net.Dialer{
	Timeout:   30 * time.Second,
	KeepAlive: 30 * time.Second,
	DualStack: TCP6Enabled(),
}

Dialer is default network dialer.

View Source
var Resolver resolver

Resolver is default DNS resolver.

Functions

func DialMaybeSRV added in v1.97.7

func DialMaybeSRV(ctx context.Context, network, addr string) (net.Conn, error)

DialMaybeSRV dials the given addr.

The addr may be either the usual TCP address or srv+host form, where host is SRV addr. If the addr has srv+host form, then the host is resolved with SRV into randomly chosen TCP address for the connection.

func EnableIPv6 added in v1.102.23

func EnableIPv6()

EnableIPv6 enables IPv6 for dialing and listening.

func GetServerTLSConfig added in v1.77.0

func GetServerTLSConfig(tlsCertFile, tlsKeyFile, tlsMinVersion string, tlsCipherSuites []string) (*tls.Config, error)

GetServerTLSConfig returns TLS config for the server.

func GetTCPNetwork added in v1.56.0

func GetTCPNetwork() string

GetTCPNetwork returns current tcp network.

func GetUDPNetwork added in v1.56.0

func GetUDPNetwork() string

GetUDPNetwork returns current udp network.

func InitConcurrentDialLimit

func InitConcurrentDialLimit(concurrentRequestLimit int)

InitConcurrentDialLimit initiates the concurrentDialLimit with value between 8 and 64 based on the given concurrentRequestLimit. It must be called before NewConnPool call.

func IsErrMissingPort added in v1.102.23

func IsErrMissingPort(err error) bool

IsErrMissingPort checks if the given error is due to a missing port in the address. It is expected to be used to validate error returned by net.SplitHostPort See https://github.com/golang/go/blob/ed08d2ad0928c0fc77cc2053863616ffb58c5aac/src/net/ipsock.go#L167

func IsTrivialNetworkError added in v1.87.0

func IsTrivialNetworkError(err error) bool

IsTrivialNetworkError returns true if the err can be ignored during logging.

func NewStatDialFunc added in v1.97.7

func NewStatDialFunc(metricPrefix string) func(ctx context.Context, network, addr string) (net.Conn, error)

NewStatDialFunc returns dialer function that supports DNS SRV records and registers stats metrics for conns.

func NewStatDialFuncWithDial added in v1.97.7

func NewStatDialFuncWithDial(metricPrefix string, dialFunc func(ctx context.Context, network, addr string) (net.Conn, error)) func(ctx context.Context, network, addr string) (net.Conn, error)

NewStatDialFuncWithDial returns dialer function that registers stats metrics for conns.

func NormalizeAddr added in v1.102.23

func NormalizeAddr(addr string, defaultPort int) (string, error)

NormalizeAddr normalizes the given addr by adding defaultPort if it is missing. It returns the normalized address in the form "host:port". It is expected that addr is in the form "host" or "host:port".

func ParseGroupAddr

func ParseGroupAddr(addr string) (string, string)

ParseGroupAddr parses `groupID/addrX` addr and returns (groupID, addrX).

If addr doesn't contain `groupID/` prefix, then ("", addr) is returned.

func ParseTLSVersion added in v1.82.0

func ParseTLSVersion(s string) (uint16, error)

ParseTLSVersion returns tls version from the given string s.

func TCP6Enabled added in v1.34.0

func TCP6Enabled() bool

TCP6Enabled returns true if dialing and listening for IPv4 TCP is enabled.

Types

type ConnPool

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

ConnPool is a connection pool with ZSTD-compressed connections.

func NewConnPool

func NewConnPool(ms *metrics.Set, name, addr string, handshakeFunc handshake.Func, compressionLevel int, dialTimeout, userTimeout time.Duration) *ConnPool

NewConnPool creates a new connection pool for the given addr.

Name is used in metrics registered at ms. handshakeFunc is used for handshaking after the connection establishing. The compression is disabled if compressionLevel <= 0.

Call ConnPool.MustStop when the returned ConnPool is no longer needed.

func (*ConnPool) Addr

func (cp *ConnPool) Addr() string

Addr returns the address where connections are established.

func (*ConnPool) Get

func (cp *ConnPool) Get() (*handshake.BufferedConn, error)

Get returns free connection from the pool.

func (*ConnPool) MustStop

func (cp *ConnPool) MustStop()

MustStop frees up resources occupied by cp.

ConnPool.Get() immediately returns an error after MustStop call. ConnPool.Put() immediately closes the connection returned to the pool.

func (*ConnPool) Put

func (cp *ConnPool) Put(bc *handshake.BufferedConn)

Put puts bc back to the pool.

Do not put broken and closed connections to the pool!

type TCPDialer

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

TCPDialer is used for dialing the addr passed to NewTCPDialer.

It also gathers various stats for dialed connections.

func NewTCPDialer

func NewTCPDialer(ms *metrics.Set, name, addr string, dialTimeout, userTimeout time.Duration) *TCPDialer

NewTCPDialer returns new dialer for dialing the given addr.

The name is used in metric tags for the returned dialer. The name must be unique among dialers.

func (*TCPDialer) Addr

func (d *TCPDialer) Addr() string

Addr returns the address the dialer dials to.

func (*TCPDialer) Dial

func (d *TCPDialer) Dial() (net.Conn, error)

Dial dials the addr passed to NewTCPDialer.

type TCPListener

type TCPListener struct {
	net.Listener
	// contains filtered or unexported fields
}

TCPListener listens for the addr passed to NewTCPListener.

It also gathers various stats for the accepted connections.

func NewTCPListener

func NewTCPListener(name, addr string, useProxyProtocol bool, tlsConfig *tls.Config) (*TCPListener, error)

NewTCPListener returns new TCP listener for the given addr and optional tlsConfig.

name is used for metrics. Each listener in the program must have a distinct name.

If useProxyProtocol is set to true, then the returned listener accepts TCP connections via proxy protocol. See https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt

func (*TCPListener) Accept

func (ln *TCPListener) Accept() (net.Conn, error)

Accept accepts connections from the addr passed to NewTCPListener.

Jump to

Keyboard shortcuts

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