server

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: Apache-2.0 Imports: 12 Imported by: 20

Documentation

Overview

Package server implements a network server.

Package server implements a network server.

Index

Examples

Constants

View Source
const TLSHandshakeFirstByte = 0x16

TLSHandshakeFirstByte is the first byte of a tls connection handshake

Variables

This section is empty.

Functions

This section is empty.

Types

type Configuration

type Configuration struct {
	// Server listening address.
	ListenAddress string `yaml:"listenAddress" validate:"nonzero"`

	// Retry mechanism configuration.
	Retry retry.Configuration `yaml:"retry"`

	// Whether keep alives are enabled on connections.
	KeepAliveEnabled *bool `yaml:"keepAliveEnabled"`

	// KeepAlive period.
	KeepAlivePeriod *time.Duration `yaml:"keepAlivePeriod"`

	// TLS configuration
	TLS *TLSConfiguration `yaml:"tls"`
}

Configuration configs a server.

func (Configuration) NewOptions

func (c Configuration) NewOptions(iOpts instrument.Options) Options

NewOptions creates server options.

func (Configuration) NewServer

func (c Configuration) NewServer(handler Handler, iOpts instrument.Options) StoppableServer

NewServer creates a new server.

type Handler

type Handler interface {
	// Handle handles the data received on the connection, this function
	// should be blocking until the connection is closed or received error.
	Handle(conn net.Conn)

	// Close closes the handler.
	Close()
}

Handler can handle the data received on connection. It's used in Server once a connection was established.

type Options

type Options interface {
	// SetInstrumentOptions sets the instrument options
	SetInstrumentOptions(value instrument.Options) Options

	// InstrumentOptions returns the instrument options
	InstrumentOptions() instrument.Options

	// SetRetryOptions sets the retry options
	SetRetryOptions(value retry.Options) Options

	// RetryOptions returns the retry options
	RetryOptions() retry.Options

	// SetTCPConnectionKeepAlive sets the keep alive state for tcp connections.
	SetTCPConnectionKeepAlive(value bool) Options

	// TCPConnectionKeepAlive returns the keep alive state for tcp connections.
	TCPConnectionKeepAlive() bool

	// SetTCPConnectionKeepAlivePeriod sets the keep alive period for tcp connections.
	// NB(xichen): on Linux this modifies both the idle time (i.e,. the time when the
	// last packet is sent from the client and when the first keepAlive probe is sent)
	// as well as the interval between keepAlive probes.
	SetTCPConnectionKeepAlivePeriod(value time.Duration) Options

	// TCPConnectionKeepAlivePeriod returns the keep alive period for tcp connections.
	TCPConnectionKeepAlivePeriod() time.Duration

	// SetListenerOptions sets the listener options for the server.
	SetListenerOptions(value xnet.ListenerOptions) Options

	// ListenerOptions sets the listener options for the server.
	ListenerOptions() xnet.ListenerOptions

	// SetTLSOptions sets the tls options for the server
	SetTLSOptions(value xtls.Options) Options

	// TLSOptions returns the tls options for the server
	TLSOptions() xtls.Options
}

Options provide a set of server options

func NewOptions

func NewOptions() Options

NewOptions creates a new set of server options

type Server

type Server interface {
	// ListenAndServe forever listens to new incoming connections and
	// handles data from those connections.
	ListenAndServe() error

	// Serve accepts and handles incoming connections on the listener l forever.
	Serve(l net.Listener) error

	// Close closes the server.
	Close()
}

Server is a server capable of listening to incoming traffic and closing itself when it's shut down.

Example
package main

import (
	"log"
	"net"

	xserver "github.com/m3db/m3/src/x/server"
)

type simpleHandler struct{}

func (h *simpleHandler) Handle(conn net.Conn) { conn.Close() }
func (h *simpleHandler) Close()               {}

func main() {
	var (
		address = ":0"
		handler = &simpleHandler{}
		opts    = xserver.NewOptions()
	)

	s := xserver.NewServer(address, handler, opts)

	if err := s.ListenAndServe(); err != nil {
		log.Fatal(err)
	}

	// Block indefintely so server can run.
	select {}
}

func NewServer

func NewServer(address string, handler Handler, opts Options) Server

NewServer creates a new server.

type StoppableServer added in v1.6.0

type StoppableServer interface {
	Server
	Stop()
}

StoppableServer is essentially a Server with a Stop method that can be invoked to first stop the server from accepting new connections. This basically means that the server will stop listening on the address it was started with. The server will continue to serve existing connections until they are closed or the server is closed in which case a Close() should be called.

func NewStoppableServer added in v1.6.0

func NewStoppableServer(
	address string,
	handler Handler,
	opts Options,
) StoppableServer

NewStoppableServer creates a new stoppable server.

type TLSConfiguration added in v1.6.0

type TLSConfiguration struct {
	// Mode is the tls server mode
	// disabled - allows plaintext connections only
	// permissive - allows both plaintext and TLS connections
	// enforced - allows TLS connections only
	Mode string `yaml:"mode" validate:"nonzero,regexp=^(disabled|permissive|enforced)$"`

	// CertFile path to a server certificate file
	CertFile string `yaml:"certFile"`

	// KeyFile path to a server key file
	KeyFile string `yaml:"keyFile"`

	// ClientCAFile path to a CA file for verifying clients
	ClientCAFile string `yaml:"clientCAFile"`

	// CertificatesTTL is a time duration certificates are stored in memory
	CertificatesTTL time.Duration `yaml:"certificatesTTL"`

	// MutualTLSEnabled sets mTLS
	MutualTLSEnabled bool `yaml:"mTLSEnabled"`
}

TLSConfiguration configs a tls server

func (TLSConfiguration) NewOptions added in v1.6.0

func (c TLSConfiguration) NewOptions() xtls.Options

NewOptions creates TLS options

Jump to

Keyboard shortcuts

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