Help us shape the future of Go! Take the 2026 Go Developer Survey

netx

package
v1.27.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package netx provides a TCP server that runs a Handler per connection, plus connection middleware and wrappers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler interface {
	Handle(ctx context.Context, conn net.Conn)
}

func LoggingHandler added in v1.26.0

func LoggingHandler(next Handler) Handler

LoggingHandler records the conversation; next gets the recorder via LoggingConnFromContext.

func MaxBytesHandler added in v1.26.0

func MaxBytesHandler(next Handler, maxBytes int64) Handler

MaxBytesHandler limits the total number of bytes read from the connection.

func RateLimitHandler added in v1.26.0

func RateLimitHandler(next Handler, allow func(net.Addr) bool) Handler

RateLimitHandler drops connections for which allow returns false.

func TimeoutHandler added in v1.26.0

func TimeoutHandler(next Handler, idleTimeout time.Duration) Handler

TimeoutHandler fails reads and writes after idleTimeout of inactivity.

type HandlerFunc

type HandlerFunc func(context.Context, net.Conn)

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(ctx context.Context, conn net.Conn)

type LoggingConn

type LoggingConn struct {
	net.Conn

	// Data is the ordered conversation log: each Read/Write call appends one
	// message, so reads and writes are interleaved in call order.
	Data [][]byte

	// onClose is called when connection is closed.
	// Must be used to get final conversation log.
	OnClose func()
	// contains filtered or unexported fields
}

LoggingConn wraps net.Conn to save conversation log.

func LoggingConnFromContext added in v1.26.0

func LoggingConnFromContext(ctx context.Context) *LoggingConn

LoggingConnFromContext returns the recorder set by LoggingHandler, or nil.

func NewLoggingConn

func NewLoggingConn(conn net.Conn) *LoggingConn

NewLoggingConn wraps net.Conn and adds logging.

func (*LoggingConn) Close

func (c *LoggingConn) Close() error

Close overwrites net.Conn Close method to call onClose function.

func (*LoggingConn) Read

func (c *LoggingConn) Read(b []byte) (int, error)

Read overwrites net.Conn Read method to be able to save data to log.

func (*LoggingConn) Upgrade added in v1.26.0

func (c *LoggingConn) Upgrade(conn net.Conn)

Upgrade replaces the underlying connection, e.g. with TLS on top of it, keeping the log.

func (*LoggingConn) Write

func (c *LoggingConn) Write(b []byte) (int, error)

Write overwrites net.Conn Write method to be able to save data to log.

type LoggingListener

type LoggingListener struct {
	net.Listener
}

LoggingListener is net.Listener wrapper that returns wraps net.Conn with LoggingConn.

func (*LoggingListener) Accept

func (l *LoggingListener) Accept() (net.Conn, error)

type MaxBytesConn

type MaxBytesConn struct {
	net.Conn
	// contains filtered or unexported fields
}

func (*MaxBytesConn) Read

func (c *MaxBytesConn) Read(b []byte) (int, error)

type Option added in v1.26.0

type Option func(*options)

func WithNotifyStarted added in v1.26.0

func WithNotifyStarted(f func()) Option

WithNotifyStarted sets a function called once the server is listening.

func WithTLSConfig added in v1.26.0

func WithTLSConfig(cfg *tls.Config) Option

WithTLSConfig makes the server accept TLS connections.

type RateLimitListener added in v1.26.0

type RateLimitListener struct {
	net.Listener
	Allow func(net.Addr) bool
}

RateLimitListener closes accepted connections for which Allow returns false.

func (*RateLimitListener) Accept added in v1.26.0

func (l *RateLimitListener) Accept() (net.Conn, error)

type Server

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

func New added in v1.26.0

func New(addr string, handler Handler, opts ...Option) *Server

New creates a Server, panics on invalid arguments.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe listens on the server address and serves connections.

func (*Server) Serve added in v1.26.0

func (s *Server) Serve(l net.Listener) error

Serve accepts connections on l until it is closed.

type TimeoutConn

type TimeoutConn struct {
	net.Conn
	// contains filtered or unexported fields
}

func (*TimeoutConn) Read

func (c *TimeoutConn) Read(b []byte) (int, error)

func (*TimeoutConn) Write

func (c *TimeoutConn) Write(b []byte) (int, error)

Jump to

Keyboard shortcuts

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