net

package module
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: MIT Imports: 6 Imported by: 0

README

darvaza.org/x/net

Go Reference Go Report Card codecov Socket Badge

Overview

The net package provides advanced networking utilities that extend Go's standard net package. It includes sophisticated port binding mechanisms, automatic reconnection clients, and platform-specific socket control.

Packages

bind

Advanced TCP/UDP port binding with retry logic, multi-interface support, and socket option control.

cfg := &bind.Config{
    Interfaces: []string{"lo", "eth0"},
    Port: 8080,
    PortAttempts: 4,
    ReusePort: true,
    KeepAlive: 30 * time.Second,
}

listeners, err := bind.Bind(cfg)

Features:

  • Multi-interface and multi-address binding.
  • Automatic port retry with configurable attempts.
  • Socket option control (SO_REUSEADDR, SO_REUSEPORT).
  • Buffer size configuration for UDP.
  • Connection upgrading capabilities.
reconnect

Generic reconnecting TCP client with automatic retry and lifecycle management.

cfg := &reconnect.Config{
    Address: "server:9000",
    DialTimeout: 5 * time.Second,
    RetryWait: 1 * time.Second,
    RetryBackoff: true,
}

client := reconnect.NewClient(cfg,
    reconnect.WithLogger(logger),
    reconnect.WithOnConnect(onConnect),
    reconnect.WithOnError(onError),
)

// Start with automatic reconnection
err := client.Spawn(ctx)

Features:

  • Automatic connection retry with backoff.
  • Session management with lifecycle callbacks.
  • Context-based cancellation.
  • Thread-safe connection handling.
  • Configurable timeouts and retry strategies.

Features

  • Platform Abstraction: Socket control operations work across Unix/Linux and Windows.
  • Graceful Degradation: Features degrade safely on unsupported platforms.
  • Context Integration: Full context.Context support throughout.
  • Thread Safety: Concurrent-safe operations in reconnect client.

Installation

go get darvaza.org/x/net

Examples

Advanced Port Binding
// Custom listener with socket options
control := bind.ControlFunc(func(fd uintptr) error {
    // Set custom socket options
    return bind.SetReusePort(fd, true)
})

ln, err := bind.ListenTCP("tcp", addr, control)
Connection Lifecycle Management
client := reconnect.NewClient(cfg,
    reconnect.WithOnConnect(func(ctx context.Context, conn net.Conn) error {
        // Initialize connection
        return nil
    }),
    reconnect.WithOnSession(func(ctx context.Context) error {
        // Handle active session
        return nil
    }),
    reconnect.WithOnDisconnect(func(ctx context.Context, conn net.Conn) error {
        // Cleanup on disconnect
        return nil
    }),
)

Dependencies

Platform Support

  • Unix/Linux: Full socket control support.
  • Windows: Adapted control operations.
  • Platform-specific implementations use build tags.

Development

For development guidelines, architecture notes, and AI agent instructions, see AGENTS.md.

Licence

This project is licensed under the MIT Licence. See LICENCE.txt for details.

Documentation

Overview

Package net provides generic network helpers and proxies to some useful standard types and functions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JoinAllHostPorts

func JoinAllHostPorts(addresses []string, ports []uint16) ([]string, error)

JoinAllHostPorts combines a list of addresses and a list of ports, validating the provided IP addresses or name in the process

func JoinHostPort

func JoinHostPort(host string, port uint16) (string, error)

JoinHostPort combines a given host address and a port, validating the provided IP address or name in the process

func SplitHostPort

func SplitHostPort(hostport string) (string, uint16, error)

SplitHostPort splits a network address into host and port, validating the host and port in the process

Types

type Addr

type Addr = net.Addr

Addr is an alias of the standard net.Addr type

type Conn

type Conn = net.Conn

Conn is an alias of the standard net.Conn type

type Dialer added in v0.2.0

type Dialer interface {
	DialContext(ctx context.Context, network string, address string) (net.Conn, error)
}

Dialer establishes a TCP connection to a remote.

type IP

type IP = net.IP

IP is an alias of the standard net.IP type

type Listener

type Listener = net.Listener

Listener is an alias of the standard net.Listener type

type TCPAddr added in v0.2.4

type TCPAddr = net.TCPAddr

TCPAddr is an alias of the standard net.TCPAddr type

type TCPConn added in v0.2.4

type TCPConn = net.TCPConn

TCPConn is an alias of the standard net.TCPConn type

type TCPListener added in v0.2.4

type TCPListener = net.TCPListener

TCPListener is an alias of the standard net.TCPListener type

Directories

Path Synopsis
Package bind provides mechanisms to facilitate listening TCP and UDP ports
Package bind provides mechanisms to facilitate listening TCP and UDP ports
Package reconnect implements a generic retrying network client.
Package reconnect implements a generic retrying network client.

Jump to

Keyboard shortcuts

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