servers

package
v0.9.7 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: BSD-3-Clause Imports: 5 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Servers)

Option is a function that sets optional parameters for Servers.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the Logger instance for logging messages.

func WithRecoverFunc

func WithRecoverFunc(recover func()) Option

WithRecoverFunc sets a function that will be used to recover from panic inside a goroutune that servers are serving requests.

type Server

type Server interface {
	// Close should stop server from serving all existing requests
	// and stop accepting new ones.
	// The listener provided in Serve method must stop listening.
	Close() error
	// Shutdown should gracefully stop server. All existing requests
	// should be processed within a deadline provided by the context.
	// No new requests should be accepted.
	// The listener provided in Serve method must stop listening.
	Shutdown(ctx context.Context) error
}

Server defines required methods for a type that can be added to the Servers. In addition to this methods, a Server should implement TCPServer or UDPServer to be able to serve requests.

type Servers

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

Servers holds a list of servers and their options. It provides a simple way to construct server group with Add method, to start them with Serve method, and stop them with Close or Shutdown methods.

func New

func New(opts ...Option) (s *Servers)

New creates a new instance of Servers with applied options.

func (*Servers) Add

func (s *Servers) Add(name, address string, srv Server)

Add adds a new server instance by a custom name and with address to listen to.

func (*Servers) Close

func (s *Servers) Close()

Close stops all servers, by calling Close method on each of them.

func (*Servers) Serve

func (s *Servers) Serve() (err error)

Serve starts all added servers. New new servers must be added after this methid is called.

func (*Servers) Shutdown

func (s *Servers) Shutdown(ctx context.Context)

Shutdown gracefully stops all servers, by calling Shutdown method on each of them.

func (*Servers) TCPAddr added in v0.2.0

func (s *Servers) TCPAddr(name string) (a *net.TCPAddr)

TCPAddr returns a TCP address of the listener that a server with a specific name is using. If there are more servers with the same name, the address of the first started server is returned.

func (*Servers) UDPAddr added in v0.2.0

func (s *Servers) UDPAddr(name string) (a *net.UDPAddr)

UDPAddr returns a UDP address of the listener that a server with a specific name is using. If there are more servers with the same name, the address of the first started server is returned.

type TCPServer added in v0.2.0

type TCPServer interface {
	// Serve should start server responding to requests.
	// The listener is initialized and already listening.
	ServeTCP(ln net.Listener) error
}

TCPServer defines methods for a server that accepts requests over TCP listener.

type UDPServer added in v0.2.0

type UDPServer interface {
	ServeUDP(conn *net.UDPConn) error
}

UDPServer defines methods for a server that accepts requests over UDP listener.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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