Documentation
¶
Overview ¶
Package tcplisten provides customizable TCP net.Listener with various performance-related options:
SO_REUSEPORT. This option allows linear scaling server performance on multi-CPU servers. See https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/ for details.
TCP_DEFER_ACCEPT. This option expects the server reads from the accepted connection before writing to them.
TCP_FASTOPEN. See https://lwn.net/Articles/508865/ for details.
The package is derived from https://github.com/kavu/go_reuseport .
Index ¶
Constants ¶
const ( IPV4 string = "tcp4" IPV6 string = "tcp6" )
Variables ¶
This section is empty.
Functions ¶
func CreateListener ¶ added in v1.5.0
CreateListener creates a TCP or UNIX listener from an address. A TCP address can omit the tcp:// prefix. UNIX addresses require unix://. Filesystem socket permissions and ownership use the operating system defaults.
func CreateListenerWithOptions ¶ added in v1.6.0
func CreateListenerWithOptions(address string, options *UnixSocketOptions) (net.Listener, error)
CreateListenerWithOptions creates a listener with optional UNIX socket attributes. It validates the options before it changes the filesystem. It closes the listener if an attribute change fails. Attributes are set after listen, so clients can connect before setup completes. Parent directories must restrict initial access and prevent path replacement.
Types ¶
type Config ¶
type Config struct {
// ReusePort enables SO_REUSEPORT.
ReusePort bool
// DeferAccept enables TCP_DEFER_ACCEPT.
DeferAccept bool
// FastOpen enables TCP_FASTOPEN.
FastOpen bool
// Backlog is the maximum number of pending TCP connections the listener
// may queue before passing them to Accept.
// See man 2 listen for details.
//
// By default system-level backlog value is used.
Backlog int
}
Config provides options to enable on the returned listener.
func (*Config) NewListener ¶
NewListener returns TCP listener with options set in the Config.
The function may be called many times for creating distinct listeners with the given config.
Only tcp4 and tcp6 networks are supported.
type UnixSocketOptions ¶ added in v1.6.0
type UnixSocketOptions struct {
// Mode is an octal string from "0000" through "0777". Empty means no change.
Mode string `mapstructure:"mode"`
// UID sets the socket owner. Nil means no change. Zero is a valid ID.
UID *int `mapstructure:"uid"`
// GID sets the socket group. Nil means no change. Zero is a valid ID.
GID *int `mapstructure:"gid"`
}
UnixSocketOptions sets attributes on a filesystem UNIX socket. Omitted attributes keep their operating system defaults.
func (*UnixSocketOptions) Validate ¶ added in v1.6.0
func (o *UnixSocketOptions) Validate(address string) error
Validate checks the options and address without filesystem access. A nil receiver is valid for every address. Non-nil options require a filesystem UNIX socket on Linux, macOS, or FreeBSD.