fastTCP

package module
v0.0.0-...-970c676 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2017 License: MIT Imports: 7 Imported by: 1

README

Build Status GoDoc Go Report

fastTCP

A highly optimised TCP listener/server system for golang taking inspiration from the performance tweaks in tcplisten and the go-blocking server in volley.

Performance tunings

The default standard library net.Listener listens for new TCP connections on one thread. However on Linux kernel versions >= 3.9 the SO_REUSEPORT port flag can be set to allow multiple threads to serve connections simultaneously. This is done with the help of this project.

This alternative TCP listener also supports use of the TCP_DEFER_ACCEPT flag which can be used to aid performance on servers with a high rate of new connections. This flag causes a delay in accepting new TCP connections until data is available from the client. For this reason this option should not be used unless the client writes to the connection first. See here for details.

Another performance option supported is the TCP_FASTOPEN flag which will set up the socket to send data before the ACK is received from the. Client. See here for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Printer

type Printer interface {
	Printf(format string, v ...interface{})
	Println(v ...interface{})
}

Printer is a simple one method interface to allow a caller to provide a custom logger to this package.

var Logger Printer

Logger is a custom logger to use for this package. Logger is nil by default.

type Server

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

Server is the high performance TCP server. When built by The NewServer function the server is not started and a call to Server.ListenTCP is needed. Server.Stop can be called to halt all of the listeners and shut down the server.

func NewServer

func NewServer(laddr string, handler func(io.ReadWriter), options TCPOptions) *Server

NewServer returns a new Server instance configured to serve on a given local address with performance tweaks as defined in the provided TCPOptions.

The Server will call the provided handler function for each new TCP connection passing the underlying os.File as an argument.

The syntax of laddr is "host:port", like "127.0.0.1:8080". If host is omitted, as in ":8080", all available interfaces are used instead of just the interface with the given host address.

The Server will attempt to open multiple tcp listeners (one per CPU core) using the SO_REUSEPORT socket option if the OS supports it. See http://man7.org/linux/man-pages/man7/socket.7.html for details.

If the OS doesn't support this socket option, or fails to setup the socket like this for some other reason, the Server will degrade to using the standard library net.Listener implementation.

func (*Server) ListenTCP

func (s *Server) ListenTCP() error

ListenTCP starts the TCP server listeners and starts to process new connections

func (*Server) Stop

func (s *Server) Stop()

Stop closes all the active listeners to shutdown the server

type TCPOptions

type TCPOptions struct {
	DeferAccept bool
	FastOpen    bool
	IPv6        bool
}

TCPOptions is used to configure optional performance tweaks to the TCP socket listener.

DeferAccept corresponds to the TCP_DEFER_ACCEPT flag. If true the listener will set up the socket for this behaviour.See http://man7.org/linux/man-pages/man7/tcp.7.html for details.

Do not use the DeferAccept flag unless the server reads from the client before writing or it will cause the thread to hang.

FastOpen corresponds to the TCP_FASTOPEN flag. If true the listener will set up the socket for this behaviour. See https://lwn.net/Articles/508865/ for details.

IPv6 sets if the listener uses IPv6 or IPv4

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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