server

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2021 License: Apache-2.0 Imports: 9 Imported by: 20

Documentation

Overview

Package server implements a network server.

Index

Examples

Constants

This section is empty.

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"`
}

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) Server

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
}

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 {}
}
Output:

func NewServer

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

NewServer creates a new server.

Jump to

Keyboard shortcuts

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